C++ OpenCV 中的 Blob 提取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4641817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:01:16  来源:igfitidea点击:

Blob extraction in OpenCV

c++opencvblobcodeblocks

提问by amr

I'm using OpenCV to filter an image for certain colours, so I've got a binary image of the detected regions.

我正在使用 OpenCV 过滤某些颜色的图像,所以我得到了检测区域的二进制图像。

Now I want to erode those areas and then get rid of the smaller ones, and find the x,y coordinates of the largest 'blob'

现在我想侵蚀这些区域,然后去掉较小的区域,然后找到最大的“斑点”的 x,y 坐标

I was looking for recommendations as to what the best library would be to use? I've seen cvBlob and cvBlobsLib but I'm not too sure how to set them up. Do I want to compile them along with the project or do I want to compile and install them to the system (like I did with OpenCV)?

我正在寻找有关使用最佳库的建议?我见过 cvBlob 和 cvBlobsLib 但我不太确定如何设置它们。我是想将它们与项目一起编译还是想将它们编译并安装到系统中(就像我使用 OpenCV 所做的那样)?

I'm currently using the Code::Blocks IDE on Ubuntu (although that shouldn't restrict things)

我目前在 Ubuntu 上使用 Code::Blocks IDE(虽然这不应该限制事情)

采纳答案by Jason Newton

I'm late to the party, but I'd just like to chime in that there is a way to do connected components in opencv, it's just not mainlined yet.

我参加聚会迟到了,但我想补充一下,有一种方法可以在 opencv 中进行连接组件,只是还没有主流化。

Update: It is mainlined, it's just been stuck waiting for 3.0 to release for multiple years. Linky to documentation

更新:它是主线,它只是被困在等待 3.0 发布多年。 链接到文档

See http://code.opencv.org/issues/1236and http://code.opencv.org/attachments/467/opencv-connectedcomponents.patch

请参阅http://code.opencv.org/issues/1236http://code.opencv.org/attachments/467/opencv-connectedcomponents.patch

Disclaimer - I'm the author.

免责声明 - 我是作者。

回答by etarion

You can use findContoursto do that, see the opencv manualand a Tutorial to find connected components.

您可以使用它findContours来执行此操作,请参阅opencv 手册查找连接组件教程



Edit: Code from the tutorial (via Archive.org)

编辑:教程中的代码(通过 Archive.org)

#include <stdio.h>
#include <cv.h>
#include <highgui.h>

int main(int argc, char *argv[])
{
    IplImage *img, *cc_color; /*IplImage is an image in OpenCV*/
    CvMemStorage *mem;
    CvSeq *contours, *ptr;
    img = cvLoadImage(argv[1], 0); /* loads the image from the command line */
    cc_color = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 3);

    cvThreshold(img, img, 150, 255, CV_THRESH_BINARY);
    mem = cvCreateMemStorage(0);
    cvFindContours(img, mem, &contours, sizeof(CvContour), CV_RETR_CCOMP,
        CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));


    for (ptr = contours; ptr != NULL; ptr = ptr->h_next) {
        CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 );
        cvDrawContours(cc_color, ptr, color, CV_RGB(0,0,0), -1, CV_FILLED, 8, cvPoint(0,0));
    }

    cvSaveImage("result.png", cc_color);
    cvReleaseImage(&img);
    cvReleaseImage(&cc_color);
    return 0;
}

回答by Paul R

Unfortunately OpenCV doesn't have any connected component labelling functionality, which seems like a serious omission for a computer vision library. Anyway I had a similar requirement recently so I implemented my own CCL routine - there are a couple of different algorithms described on the CCL Wikipedia pageand they are both pretty simple to implement.

不幸的是,OpenCV 没有任何连接组件标记功能,这对于计算机视觉库来说似乎是一个严重的遗漏。无论如何,我最近有一个类似的要求,所以我实现了自己的 CCL 例程 - CCL 维基百科页面上描述了几种不同的算法,它们都非常易于实现。

回答by Jorge Vega Sánchez

I think the best and easy option to work with blobs with OpenCV is to use cvBlob library. Its a complemntary library with OpenCV a its so easy to use.

我认为使用 OpenCV 处理 blob 的最佳和最简单的选择是使用 cvBlob 库。它是 OpenCV 的补充库,非常易于使用。