opencv cvblob -- 渲染白色以外的斑点

发布于 2024-11-30 11:03:07 字数 2157 浏览 2 评论 0原文

我正在尝试使用 cvblob 检测对象。不知何故,我的代码只标记白色物体。如何标记其他颜色的物体,例如一罐啤酒或一瓶水。

这是我的代码:

#include "highgui.h"
#include "cv.h"
#include "cvaux.h"
#include "iostream"
#include <stdio.h>
#include <ctype.h>

#include <cvblob.h>

using namespace cv;
using namespace std;
using namespace cvb;

int main(int argc, char** argv) {
    CvTracks tracks;

    cvNamedWindow("frame", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("frame", 50, 100);

    CvCapture* capture;

    IplImage* frame = 0;

    capture = cvCreateCameraCapture( 0 ); //capture frames from cam on index 0: /dev/video0/

    if (!capture) {
        return -1;
    }

    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 320);
    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 240);

    frame = cvQueryFrame(capture);

    while(frame) {
        IplImage *gray = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
        cvCvtColor(frame, gray, CV_BGR2GRAY);
        cvThreshold(gray, gray, 150, 255, CV_THRESH_BINARY);

        IplImage *labelImg=cvCreateImage(cvGetSize(gray), IPL_DEPTH_LABEL, 1);
        CvBlobs blobs;
        unsigned int result=cvLabel(gray, labelImg, blobs);

        cvFilterByArea(blobs, 500, 1000000);
//        cvRenderBlobs(labelImg, blobs, frame, frame, CV_BLOB_RENDER_BOUNDING_BOX);
        cvRenderBlobs(labelImg, blobs, frame, frame, CV_BLOB_RENDER_CENTROID);
        cvUpdateTracks(blobs, tracks, 200., 5);
        cvRenderTracks(tracks, frame, frame, CV_TRACK_RENDER_ID|CV_TRACK_RENDER_BOUNDING_BOX);


        for (CvBlobs::const_iterator it=blobs.begin(); it!=blobs.end(); ++it) {
            cout << "Blob #" << it->second->label << ": Area=" << it->second->area << ", Centroid=(" << it->second->centroid.x << ", " << it->second->centroid.y << ")" << endl;
        }

        cvShowImage("frame", frame);

        cout << "----------------------------" << endl;

        frame = cvQueryFrame(capture);

        char c = cvWaitKey(10);
        if(c==27)
            break;

    }
}

如有任何提示,我们将不胜感激。

米洛

I am trying to detect objects with cvblob. Somehow, my code only marks the white objects. How to mark objects of other colors, like a can of beer or a bottle of water.

Here is my code:

#include "highgui.h"
#include "cv.h"
#include "cvaux.h"
#include "iostream"
#include <stdio.h>
#include <ctype.h>

#include <cvblob.h>

using namespace cv;
using namespace std;
using namespace cvb;

int main(int argc, char** argv) {
    CvTracks tracks;

    cvNamedWindow("frame", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("frame", 50, 100);

    CvCapture* capture;

    IplImage* frame = 0;

    capture = cvCreateCameraCapture( 0 ); //capture frames from cam on index 0: /dev/video0/

    if (!capture) {
        return -1;
    }

    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 320);
    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 240);

    frame = cvQueryFrame(capture);

    while(frame) {
        IplImage *gray = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
        cvCvtColor(frame, gray, CV_BGR2GRAY);
        cvThreshold(gray, gray, 150, 255, CV_THRESH_BINARY);

        IplImage *labelImg=cvCreateImage(cvGetSize(gray), IPL_DEPTH_LABEL, 1);
        CvBlobs blobs;
        unsigned int result=cvLabel(gray, labelImg, blobs);

        cvFilterByArea(blobs, 500, 1000000);
//        cvRenderBlobs(labelImg, blobs, frame, frame, CV_BLOB_RENDER_BOUNDING_BOX);
        cvRenderBlobs(labelImg, blobs, frame, frame, CV_BLOB_RENDER_CENTROID);
        cvUpdateTracks(blobs, tracks, 200., 5);
        cvRenderTracks(tracks, frame, frame, CV_TRACK_RENDER_ID|CV_TRACK_RENDER_BOUNDING_BOX);


        for (CvBlobs::const_iterator it=blobs.begin(); it!=blobs.end(); ++it) {
            cout << "Blob #" << it->second->label << ": Area=" << it->second->area << ", Centroid=(" << it->second->centroid.x << ", " << it->second->centroid.y << ")" << endl;
        }

        cvShowImage("frame", frame);

        cout << "----------------------------" << endl;

        frame = cvQueryFrame(capture);

        char c = cvWaitKey(10);
        if(c==27)
            break;

    }
}

Any tip is appreciated.

Milo

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

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

发布评论

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

评论(2

-黛色若梦 2024-12-07 11:03:08

尝试添加:

“cvInRangeS(hsvframe,cvScalar(23,41,133),cvScalar(40,150,255),threshy);//黄色”

在过滤斑点之前。它是定义所需颜色阈值的 HSV(而不是 RGB)值范围。

希望有帮助。

Try adding:

"cvInRangeS(hsvframe,cvScalar(23,41,133),cvScalar(40,150,255),threshy);//for yellow"

Before Filtering the blobs. Its a range of HSV(instead of RGB) values that defines the threshold of the desire color.

Hope it helps.

遇见了你 2024-12-07 11:03:07

这是默认选项,如果不更改 cvblob 库中的源代码,则无法更改它。

如果你真的想改变这一点,那么简单,你可以创建相同方法的副本,添加一个新的输入变量(如 CvScalar)来选择输出颜色。太容易了。

方法 cvRenderBlob 将位于 cvcontour.cpp 中。

我对 cvblob 库进行了许多改进,在接下来的几个月中我将把它推给创建者。

That's the option by default and you cannot change it if you don't change the source code in cvblob library.

If you really want to change this is so easy, you can create a copy of the same method adding a new input var like CvScalar to select output color. It's so easy.

The method cvRenderBlob will be in cvcontour.cpp.

I've been made many improvement in cvblob library and in next months I will push it to the creator.

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