我正在尝试使用cvblob
检测对象。所以我使用cvRenderBlob()
方法。程序编译成功但在运行时它返回一个未处理的异常。当我打破它时,箭头会指向 cvblob.cpp 文件的CvLabel *labels = (CvLabel *)imgLabel->imageData + imgLabel_offset + (blob->miny * stepLbl);
方法定义中的cvRenderBlob()
语句。但是,如果我使用cvRenderBlobs()
方法,它的工作正常。我只需要检测一个最大的blob。有人请帮我处理这个例外。
这是我的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);
答案 0 :(得分:1)
首先,我想指出你实际上是在使用常规的c语法。 C ++使用Mat类。我一直在根据图片中的绿色物体进行一些斑点提取。一旦阈值正确,这意味着我们有一个“二进制”图像,背景/前景。我用
findContours() //this function expects quite a bit, read documentation
在结构分析的documentation中更清楚地描述。它将为您提供图像中所有斑点的轮廓。在处理另一个矢量的矢量中,该矢量处理图像中的点;像这样
vector<vector<Point>> contours;
我也需要找到最大的blob,尽管我的方法在某种程度上可能有问题,但我不需要它有所不同。我用
minAreaRect() // expects a set of points (contained by the vector or mat classes
还在结构分析下进行了描述 然后访问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 ++语法,或从基本算法中获得灵感。
享受。