gpt4 book ai didi

visual-studio-2010 - OpenCV cvblob - 渲染 Blob

转载 作者:太空宇宙 更新时间:2023-11-03 21:48:20 27 4
gpt4 key购买 nike

我正在尝试使用 cvblob 检测对象。所以我使用 cvRenderBlob() 方法。程序编译成功,但在运行时返回未处理的异常。当我打破它时,箭头指向 CvLabel *labels = (CvLabel *)imgLabel->imageData + imgLabel_offset + (blob->miny * stepLbl); cvRenderBlob( ) cvblob.cpp 文件的方法定义。但是如果我使用 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);

最佳答案

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

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

documentation 中有更清楚的描述关于结构分析。它会给你图像中所有 Blob 的轮廓。在处理图像中处理点的另一个向量的向量中;像这样

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++ 语法,要么从基本算法中获得一些启发。

享受吧。

关于visual-studio-2010 - OpenCV cvblob - 渲染 Blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7505460/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com