OpenCV 对白色像素进行分组

发布于 2024-08-24 13:52:43 字数 521 浏览 4 评论 0原文

我已经完成了艰苦的工作,将 MacBook 上的 iSight 摄像头变成了红外摄像头,对其进行了转换,设置了阈值等。现在得到的图像如下所示:

替代文本

我现在的问题是;我需要通过对白色像素进行分组来了解图像上有多少斑点。我不想使用 cvBlob/cvBlobsLib,我宁愿只使用 OpenCV 中已有的内容。

可以循环遍历像素并通过检查(阈值)触摸白色像素来对它们进行分组,但我猜测 OpenCV 可能有一种非常简单的方法可以做到这一点?

我猜我不能使用 cvFindContours 因为这将检索一个大数组中的所有白色像素,而不是将它们分成“组”。有人可以推荐吗? (请注意,这些不是圆圈,只是小型红外 LED 发出的光)

提前非常感谢!
汤姆德

I've done the hard work, turning my iSight camera on my MacBook into an infrared camera, converted it, set the threshold etc.. and now have an image that looks something like this:

alt text

My problem is now; I need to know how many blobs are on my image by grouping the white pixels. I don't want to use cvBlob/cvBlobsLib, I'd rather just use what is already in OpenCV.

I can loop through the pixels and group them by checking for (thresholded) touching white pixels, but I'm guessing there is probably a really easy way of doing this from OpenCV?

I'm guessing I can't use cvFindContours as this will retrieve all the white pixels in one big array, rather than separating them into "groups". Could anyone recommend? (Note these are not circles, just the light emitted from little IR LEDs)

Many Thanks in advance!

tommed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱々樱花落 2024-08-31 13:52:43

循环遍历图像寻找白色像素。当您遇到这种情况时,您可以使用 cvFloodFill 并将该像素作为种子。然后增加每个区域的填充值,以便每个区域具有不同的颜色。这称为标签。

Loop through the image looking for white pixels. When you encounter one you use cvFloodFill with that pixel as seed. Then increment the fill value for every region so that each region has a different color. This is called labeling.

巷雨优美回忆 2024-08-31 13:52:43

是的,您可以使用 cvFindContours() 来完成。它返回指向找到的第一个序列的指针。使用该指针,您可以遍历找到的所有序列。

    // your image converted to grayscale
    IplImage* grayImg = LoadImage(...);

    // image for drawing contours onto
    IplImage* colorImg = cvCreateImage(cvGetSize(grayImg), 8, 3);

    // memory where cvFindContours() can find memory in which to record the contours
    CvMemStorage* memStorage = cvCreateMemStorage(0);

    // find the contours on image *grayImg*
    CvSeq* contours = 0;
    cvFindContours(grayImg, memStorage, &contours);

    // traverse through and draw contours
    for(CvSeq* c = contours; c != NULL; c = c->h_next) 
    {
         cvCvtColor( grayImg, colorImg, CV_GRAY2BGR );
         cvDrawContours(
                        colorImg,
                        c,
                        CVX_RED,
                        CVX_BLUE,
                        0, // Try different values of max_level, and see what happens
                        2,
                        8
         );
    }

除了这个方法之外,我建议您看看cvBlobscvBlobsLib。后者作为官方斑点检测库集成在 OpenCV 2.0 中。

Yes, you can do it with cvFindContours(). It returns the pointer to the first sequence that was found. Using that pointer you can traverse through all of the sequences found.

    // your image converted to grayscale
    IplImage* grayImg = LoadImage(...);

    // image for drawing contours onto
    IplImage* colorImg = cvCreateImage(cvGetSize(grayImg), 8, 3);

    // memory where cvFindContours() can find memory in which to record the contours
    CvMemStorage* memStorage = cvCreateMemStorage(0);

    // find the contours on image *grayImg*
    CvSeq* contours = 0;
    cvFindContours(grayImg, memStorage, &contours);

    // traverse through and draw contours
    for(CvSeq* c = contours; c != NULL; c = c->h_next) 
    {
         cvCvtColor( grayImg, colorImg, CV_GRAY2BGR );
         cvDrawContours(
                        colorImg,
                        c,
                        CVX_RED,
                        CVX_BLUE,
                        0, // Try different values of max_level, and see what happens
                        2,
                        8
         );
    }

Besides this method, I would advise you to take a look at cvBlobs or cvBlobsLib. Latter one is integrated in OpenCV 2.0 as official blob detection lib.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文