2011-09-21 75 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++語法,或者從基本算法中獲得靈感。

享受。