2011-09-21 77 views
2

我试图检测使用cvblob的对象。所以我用cvRenderBlob()的方法。程序编译成功,但在运行时它返回一个未处理的异常。当我把它分解时,箭头指向语句中的cvRenderBlob()方法定义中的cvblob.cpp文件。但如果我使用cvRenderBlobs()方法它工作正常。我只需要检测一个最大的斑点。有人请帮我处理这个例外。 这里是我的VC++代码,OpenCV cvblob - 渲染斑点

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); 

回答

1

首先,我想指出的是,你实际上是使用常规的C语法。 C++使用Mat类。我一直在研究基于图片中绿色物体的斑点提取。一旦阈值正确,这意味着我们有一个“二进制”图像,背景/前景。我使用

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

在结构分析中更详细地描述了documentation。它会给你图像中所有斑点的轮廓。在处理另一个处理矢量的矢量中,处理图像中的点;像这样

vector<vector<Point>> contours; 

我太需要找到的最大的斑点,虽然我的方法可能是错误的在一定程度上,我不会需要它是不同的。我用

minAreaRect() // expects a set of points (contained by the vector or mat classes 

Descriped也受到结构分析 然后访问RECT

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++语法,或者从基本算法中获得灵感。

享受。