c++ - 如何在 meanShiftSegmentation() 输出上运行 findContours()?

标签 c++ opencv

我正在尝试使用 floodFill 将我非常缓慢的天真分割重写为更快的东西。我在一年前排除了 meanShiftFiltering,因为很难标记颜色然后找到它们的轮廓。

当前版本的 opencv 似乎有一个快速的新函数,可以使用均值偏移标记段:gpu::meanShiftSegmentation()。它生成如下图像:

meanShiftSegmentation() output
(来源:ekran.org)

所以在我看来,这非常接近能够生成轮廓。如何运行 findContours 来生成线段?

在我看来,这可以通过从图像中提取标记的颜色,然后测试图像中的哪些像素值与每个标签颜色匹配来制作适合 findContours 的 bool 图像来完成。这是我在下面所做的(但它有点慢,我觉得应该有更好的方法):

Mat image = imread("test.png");

...
// gpu operations on image resulting in gpuOpen
...

// Mean shift
TermCriteria iterations = TermCriteria(CV_TERMCRIT_ITER, 2, 0);
gpu::meanShiftSegmentation(gpuOpen, segments, 10, 20, 300, iterations);

// convert to greyscale (HSV image)
vector<Mat> channels;
split(segments, channels);

// get labels from histogram of image.
int size = 256;
labels = Mat(256, 1, CV_32SC1);
calcHist(&channels.at(2), 1, 0, Mat(), labels, 1, &size, 0);

// Loop through hist bins
for (int i=0; i<256; i++) {
    float count = labels.at<float>(i);

    // Does this bin represent a label in the image?
    if (count > 0) {
        // find areas of the image that match this label and findContours on the result.
        Mat label = Mat(channels.at(2).rows, channels.at(2).cols, CV_8UC1, Scalar::all(i)); // image filled with label colour.
        Mat boolImage = (channels.at(2) == label); // which pixels in labeled image are identical to this label?
        vector<vector<Point>> labelContours;
        findContours(boolImage, labelContours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
        // Loop through contours.
        for (int idx = 0; idx < labelContours.size(); idx++) {
            // get bounds for this contour.
            bounds = boundingRect(labelContours[idx]);

            // create ROI for bounds to extract this region
            Mat patchROI = image(bounds);
            Mat maskROI = boolImage(bounds);
        }
    }
}

这是最好的方法还是有更好的方法来获取标签颜色? meanShiftSegmentation 提供此信息似乎合乎逻辑? (颜色值 vector ,或每个标签的掩码 vector 等)

谢谢。

最佳答案

以下是执行此操作的另一种方法,不会丢弃 meanShiftSegmentation 结果中的颜色信息。我没有比较两者的性能。

// Loop through whole image, pixel and pixel and then use the colour to index an array of bools indicating presence.

vector<Scalar> colours;
vector<Scalar>::iterator colourIter;
vector< vector< vector<bool> > > colourSpace;
vector< vector< vector<bool> > >::iterator colourSpaceBIter; 
vector< vector<bool> >::iterator colourSpaceGIter;
vector<bool>::iterator colourSpaceRIter;

// Initialize 3D Vector
colourSpace.resize(256);
for (int i = 0; i < 256; i++) {
    colourSpace[i].resize(256);
    for (int j = 0; j < 256; j++) {
        colourSpace[i][j].resize(256);
    }
}

// Loop through pixels in the image (should be fastish, look into LUT for faster)
uchar r, g, b;
for (int i = 0; i < segments.rows; i++)
{
    Vec3b* pixel = segments.ptr<Vec3b>(i); // point to first pixel in row
    for (int j = 0; j < segments.cols; j++)
    {
        b = pixel[j][0];
        g = pixel[j][1];
        r = pixel[j][2];

        colourSpace[b][g][r] = true; // this colour is in the image.
        //cout << "BGR: " << int(b) << " " << int(g) << " " << int(r) << endl;
    }
}

// Get all the unique colours from colourSpace
// loop through colourSpace
int bi=0;
for (colourSpaceBIter = colourSpace.begin(); colourSpaceBIter != colourSpace.end(); colourSpaceBIter++) {
    int gi=0;
    for (colourSpaceGIter = colourSpaceBIter->begin(); colourSpaceGIter != colourSpaceBIter->end(); colourSpaceGIter++) {
        int ri=0;
        for (colourSpaceRIter = colourSpaceGIter->begin(); colourSpaceRIter != colourSpaceGIter->end(); colourSpaceRIter++) {
            if (*colourSpaceRIter)
                colours.push_back( Scalar(bi,gi,ri) );
            ri++;
        }
        gi++;
    }
    bi++;
}

// For each colour
int segmentCount = 0;
for (colourIter = colours.begin(); colourIter != colours.end(); colourIter++) {

    Mat label = Mat(segments.rows, segments.cols, CV_8UC3, *colourIter); // image filled with label colour.
    Mat boolImage = Mat(segments.rows, segments.cols, CV_8UC3);
    inRange(segments, *colourIter, *colourIter, boolImage); // which pixels in labeled image are identical to this label?

    vector<vector<Point> > labelContours;
    findContours(boolImage, labelContours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

    // Loop through contours.
    for (int idx = 0; idx < labelContours.size(); idx++) {
        // get bounds for this contour.
        Rect bounds = boundingRect(labelContours[idx]);
        float area = contourArea(labelContours[idx]);

        // Draw this contour on a new blank image
        Mat maskImage = Mat::zeros(boolImage.rows, boolImage.cols, boolImage.type());
        drawContours(maskImage, labelContours, idx, Scalar(255,255,255), CV_FILLED);

        Mat patchROI = frame(bounds);
        Mat maskROI = maskImage(bounds);
    }
    segmentCount++;
}

关于c++ - 如何在 meanShiftSegmentation() 输出上运行 findContours()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18477519/

相关文章:

c++ - 全局对象是否保证在所有线程本地存储对象被销毁后被销毁?

c++ - 映射/设置迭代器不兼容 - 检查对象是否在映射中

c++ - 使用 macports 安装 opencv 并在 qt (osx) 中使用

python - 如何用白色填充闭合轮廓区域

c++ - 如何将 wstring 转换为 u16string?

c++ - GLSL 像素着色器仅运行 0 个纹理单元

java - Android: Image Processing Library(替代openCV)

c++ - 如何获得连续两帧的平均输出?

c++ - C++中0到1之间的Box muller随机数

Python:对图像中的对象进行分类