(opencv)将轮廓合并在一起 [英] (opencv) merge contours together

查看:3237
本文介绍了(opencv)将轮廓合并在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行实时运动检测程序.使用背景减法后,我发现在不同的图像中有很多轮廓.我想问问有什么方法可以将这些轮廓合并在一起或使一个更大的矩形包含所有轮廓吗?

I am doing a real time motion detection program. I find that there are a lot of contour made in my different image after i used background subtraction method . i would like to ask is there any method that can merge these contour together or make a larger rect contain all the contours?

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

the case now i have been done
http://singhgaganpreet.files.wordpress.com/2012/07/motioncolour.jpg
My code is here

#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.对于某些应用程序,最好使用roxPoly()而不是convexHull().两者都尝试.

PS. For some applications, it may be better to use approxPoly() rather than convexHull(). Just try both.

PPS.尝试使用高斯平滑生成的轮廓.这也可能会有帮助.

PPS. Try smoothing the resulting contour with gaussian. It also can be helpful.

这篇关于(opencv)将轮廓合并在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆