gpt4 book ai didi

merge - (opencv) 将轮廓合并在一起

转载 作者:行者123 更新时间:2023-12-03 02:06:54 26 4
gpt4 key购买 nike

我正在做一个实时运动检测程序。我发现使用背景减法后,我的不同图像中出现了很多轮廓。我想问是否有任何方法可以将这些轮廓合并在一起或使更大的矩形包含所有轮廓?

现在我的案子已经完成了
http://singhgaganpreet.files.wordpress.com/2012/07/motioncolour.jpg
我的代码在这里

#include <iostream>
#include <OpenCV/cv.h>
#include <OPenCV/highgui.h>

using namespace cv;
using namespace std;

CvRect rect;
CvSeq* contours = 0;
CvMemStorage* storage = NULL;
CvCapture *cam;
IplImage *currentFrame, *currentFrame_grey, *differenceImg, *oldFrame_grey;

bool first = true;

int main(int argc, char* argv[])
{
//Create a new movie capture object.
cam = cvCaptureFromCAM(0);

//create storage for contours
storage = cvCreateMemStorage(0);

//capture current frame from webcam
currentFrame = cvQueryFrame(cam);

//Size of the image.
CvSize imgSize;
imgSize.width = currentFrame->width;
imgSize.height = currentFrame->height;

//Images to use in the program.
currentFrame_grey = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);

while(1)
{
currentFrame = cvQueryFrame( cam );
if( !currentFrame ) break;

//Convert the image to grayscale.
cvCvtColor(currentFrame,currentFrame_grey,CV_RGB2GRAY);

if(first) //Capturing Background for the first time
{
differenceImg = cvCloneImage(currentFrame_grey);
oldFrame_grey = cvCloneImage(currentFrame_grey);
cvConvertScale(currentFrame_grey, oldFrame_grey, 1.0, 0.0);
first = false;
continue;
}

//Minus the current frame from the moving average.
cvAbsDiff(oldFrame_grey,currentFrame_grey,differenceImg);

//bluring the differnece image
cvSmooth(differenceImg, differenceImg, CV_BLUR);

//apply threshold to discard small unwanted movements
cvThreshold(differenceImg, differenceImg, 25, 255, CV_THRESH_BINARY);

//find contours
cvFindContours( differenceImg, storage, &contours );

//draw bounding box around each contour
for(; contours!=0; contours = contours->h_next)
{
rect = cvBoundingRect(contours, 0); //extract bounding box for current contour

//drawing rectangle
cvRectangle(currentFrame,
cvPoint(rect.x, rect.y),
cvPoint(rect.x+rect.width, rect.y+rect.height),
cvScalar(0, 0, 255, 0),
2, 8, 0);
}

//display colour image with bounding box
cvShowImage("Output Image", currentFrame);

//display threshold image
cvShowImage("Difference image", differenceImg);

//New Background
cvConvertScale(currentFrame_grey, oldFrame_grey, 1.0, 0.0);

//clear memory and contours
cvClearMemStorage( storage );
contours = 0;

//press Esc to exit
char c = cvWaitKey(33);
if( c == 27 ) break;

}

// Destroy the image & movies objects
cvReleaseImage(&oldFrame_grey);
cvReleaseImage(&differenceImg);
cvReleaseImage(&currentFrame);
cvReleaseImage(&currentFrame_grey);
//cvReleaseCapture(&cam);

return 0;

}

最佳答案

你尝试过这个吗?

std::vector<cv::Point> points;
points.insert(points.end(), contour1.begin(), contour1.end());
points.insert(points.end(), contour2.begin(), contour2.end());
convexHull(cv::Mat(points), contour);
PS。对于某些应用程序,使用approxPoly()而不是convexHull()可能更好。两者都尝试一下。

PPS。尝试用高斯平滑生成的轮廓。它也会有帮助。

关于merge - (opencv) 将轮廓合并在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22801545/

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