2014-04-01 5 views
0

모션 감지 프로젝트를하고 있는데 카메라로 이미지를 얻고 싶은지 물어보고 싶습니다. 매트 이미지 대신 IplImage를 사용해야합니다. 이미지가 IplImage로 정의되어 있으면 cvseq 대신 벡터 < 벡터> 윤곽선과 같은 형식을 사용하여 윤곽을 정의 할 수 있습니다. 예 당신이 윤곽을 얻기 위해 vector 일을 사용할 수 있습니다(Opencv) IplImage에서 윤곽 잡기

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

그리고,이는 찾기 형상 프로그램

#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; 
} 
+1

제발,이 세상을 더 좋은 곳으로 만들고, 오래된 c-api를 사용하여 그만 두십시오 * – berak

+0

당신의 도움에 감사드립니다 – user3223210

답변

2

예, 다음과 같은 코드를 사용하여 카메라에서 이미지를 캡처 할 수 Mat을 사용할 수 있습니다에게 있습니다 .