我正在尝试使用cvblob检测对象。所以我使用cvRenderBlob()方法。程序编译成功,但在运行时返回未处理的异常。当我断开它时,箭头指向 cvblob.cpp 文件的CvLabel *labels = (CvLabel *)imgLabel->imageData + imgLabel_offset + (blob->miny * stepLbl);方法定义中的cvRenderBlob()语句。但是,如果我使用cvRenderBlobs()方法,效果很好。我只需要检测一个最大的斑点。请有人帮我处理这个异常。
这是我的VC++代码,

CvCapture* capture = 0;
IplImage* frame = 0;
int key = 0;
CvBlobs blobs;
CvBlob *blob;

capture = cvCaptureFromCAM(0);

if (!capture) {
    printf("Could not initialize capturing....\n");
    return 1;
}

int screenx = GetSystemMetrics(SM_CXSCREEN);
int screeny = GetSystemMetrics(SM_CYSCREEN);

while (key!='q') {
    frame = cvQueryFrame(capture);
    if (!frame) break;

    IplImage* imgHSV = cvCreateImage(cvGetSize(frame), 8, 3);
    cvCvtColor(frame, imgHSV, CV_BGR2HSV);

    IplImage* imgThreshed = cvCreateImage(cvGetSize(frame), 8, 1);
    cvInRangeS(imgHSV, cvScalar(61, 156, 205),cvScalar(161, 256, 305), imgThreshed); // for light blue color

    IplImage* imgThresh = imgThreshed;
    cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN, 9, 9);

    cvNamedWindow("Thresh");
    cvShowImage("Thresh", imgThresh);
    IplImage* labelImg = cvCreateImage(cvGetSize(imgHSV), IPL_DEPTH_LABEL, 1);
    unsigned int result = cvLabel(imgThresh, labelImg, blobs);

    blob = blobs[cvGreaterBlob(blobs)];
    cvRenderBlob(labelImg, blob, frame, frame);
    /*cvRenderBlobs(labelImg, blobs, frame, frame);*/
    /*cvFilterByArea(blobs, 60, 500);*/
    cvFilterByLabel(blobs, cvGreaterBlob(blobs));

    cvNamedWindow("Video");
    cvShowImage("Video", frame);
    key = cvWaitKey(1);
 }

cvDestroyWindow("Thresh");
cvDestroyWindow("Video");
cvReleaseCapture(&capture);

最佳答案

首先,我想指出您实际上是在使用常规c语法。 C++使用Mat类。我一直在基于图片中的绿色物体进行一些斑点提取。正确设定阈值后,即表示我们具有“二进制”图像,即背景/前景。我用

findContours() //this function expects quite a bit, read documentation

documentation中更清晰地描述了结构分析。它会为您提供图像中所有斑点的轮廓。在处理另一个 vector 的 vector 中,另一个 vector 在处理图像中的点;像这样
vector<vector<Point>> contours;

我也需要找到最大的斑点,尽管我的方法在某种程度上可能是错误的,但我并不需要与众不同。我用
minAreaRect() // expects a set of points (contained by the vector or mat classes

也在结构分析中描述
然后访问矩形的大小
int sizeOfObject = 0;
int idxBiggestObject = 0; //will track the biggest object

if(contours.size() != 0) //only runs code if there is any blobs / contours in the image
{
    for (int i = 0; i < contours.size(); i++) // runs i times where i is the amount of "blobs" in the image.
    {
        myVector = minAreaRect(contours[i])
        if(myVector.size.area > sizeOfObject)
        {
             sizeOfObject = myVector.size.area; //saves area to compare with further blobs
             idxBiggestObject = i; //saves index, so you know which is biggest, alternatively, .push_back into another vector
        }
    }
}

好的,我们实际上只测量一个旋转的边界框,但是在大多数情况下,它可以。我希望您要么切换到c++语法,要么从基本算法中获得启发。

请享用。

08-16 12:17