【问题标题】:(Opencv) Geting contour at the IplImage(Opencv) 在 IplImage 处获取轮廓
【发布时间】:2014-04-01 18:32:17
【问题描述】:

我正在做一个运动检测项目,我想问我是否想在相机中获取图像,是我需要使用 IplImage 而不是 mat 图像,我也想知道我可以使用格式定义轮廓如果图像定义为 IplImage,则类似于 vector 轮廓而不是 cvseq。这是一个查找轮廓程序

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

using namespace cv;
using namespace std;

CRect 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;
}

【问题讨论】:

  • 请让这个世界变得更美好,并停止使用过时的 c-api

标签: opencv contour iplimage


【解决方案1】:

是的,您可以使用Mat 使用以下代码从相机中捕获图像:

VideoCapture cap(0);
for(;;)
{
    Mat frame;
    cap >> frame;
}   

而且,是的,您可以使用 vector 来获取轮廓。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-24
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 2015-02-02
    • 2019-03-23
    • 1970-01-01
    相关资源
    最近更新 更多