使用OpenCV在blob中创建矩形

时间:2016-01-20 09:41:01

标签: c++ image opencv image-processing opencv3.0

输入图片:

输出图片:

我在图像中有几个彩色斑点,我试图在每种颜色的最大斑点内创建矩形(或正方形 - 这似乎更容易)。我找到了the answer to how to create a rectangle that bounds a single largest blob,但我不确定如何找到一个简单地适合blob的方块。它不必是最大的,它必须大于某个区域,否则我只是不会包含它。我也看到了一些关于多边形的工作,但没有形成无定形的形状。

2 个答案:

答案 0 :(得分:1)

对于单个blob,问题可以表述为:find the largest rectangle containing only zeros in a matrix

要在blob中找到最大的面向轴的矩形,可以参考my other answer中的函数findMinRect。该代码是用here从Python中移植到C ++中的原始代码。

然后第二个问题是找到所有具有相同颜色的blob。这有点棘手,因为你的图像是jpeg,压缩会在边框附近产生很多人工色彩。所以我创建了一个png图像(如下所示),只是为了表明该算法有效。由您来提供没有压缩工件的图像。

然后,您只需为每种颜色创建一个遮罩,为此遮罩中的每个斑点找到连接的组件,并计算每个斑点的最小矩形。

初始图片:

enter image description here

这里我显示为每个blob找到的rects,除以颜色。然后,您可以只获取所需的矩形,每种颜色的最大矩形,或每种颜色的最大blob的矩形。

结果:

enter image description here

这里是代码:

#include <opencv2/opencv.hpp>
#include <algorithm>
#include <set>
using namespace std;
using namespace cv;

// https://stackoverflow.com/a/30418912/5008845
Rect findMinRect(const Mat1b& src)
{
    Mat1f W(src.rows, src.cols, float(0));
    Mat1f H(src.rows, src.cols, float(0));

    Rect maxRect(0, 0, 0, 0);
    float maxArea = 0.f;

    for (int r = 0; r < src.rows; ++r)
    {
        for (int c = 0; c < src.cols; ++c)
        {
            if (src(r, c) == 0)
            {
                H(r, c) = 1.f + ((r>0) ? H(r - 1, c) : 0);
                W(r, c) = 1.f + ((c>0) ? W(r, c - 1) : 0);
            }

            float minw = W(r, c);
            for (int h = 0; h < H(r, c); ++h)
            {
                minw = min(minw, W(r - h, c));
                float area = (h + 1) * minw;
                if (area > maxArea)
                {
                    maxArea = area;
                    maxRect = Rect(Point(c - minw + 1, r - h), Point(c + 1, r + 1));
                }
            }
        }
    }

    return maxRect;
}


struct lessVec3b
{
    bool operator()(const Vec3b& lhs, const Vec3b& rhs) {
        return (lhs[0] != rhs[0]) ? (lhs[0] < rhs[0]) : ((lhs[1] != rhs[1]) ? (lhs[1] < rhs[1]) : (lhs[2] < rhs[2]));
    }
};

int main()
{
    // Load image
    Mat3b img = imread("path_to_image");

    // Find unique colors
    set<Vec3b, lessVec3b> s(img.begin(), img.end());

    // Divide planes of original image
    vector<Mat1b> planes;
    split(img, planes);
    for (auto color : s)
    {
        // Create a mask with only pixels of the given color
        Mat1b mask(img.rows, img.cols, uchar(255));
        for (int i = 0; i < 3; ++i)
        {
            mask &= (planes[i] == color[i]);
        }

        // Find blobs
        vector<vector<Point>> contours;
        findContours(mask, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

        for (int i = 0; i < contours.size(); ++i)
        {
            // Create a mask for each single blob
            Mat1b maskSingleContour(img.rows, img.cols, uchar(0));
            drawContours(maskSingleContour, contours, i, Scalar(255), CV_FILLED);

            // Find minimum rect for each blob
            Rect box = findMinRect(~maskSingleContour);

            // Draw rect
            Scalar rectColor(color[1], color[2], color[0]);
            rectangle(img, box, rectColor, 2);
        }
    }

    imshow("Result", img);
    waitKey();

    return 0;
}

答案 1 :(得分:0)

您可以使用this code找到刻在任意形状内的最大正方形或矩形。虽然它是MATLAB而不是C ++ / OpenCV,但您可以轻松更改其源代码以满足您的需求。

要找到刻在凸多边形内的最大矩形,请查看here(带代码)。

相关问题