Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(Opencv)在IplImage获取等高线_Opencv_Contour_Iplimage - Fatal编程技术网

(Opencv)在IplImage获取等高线

(Opencv)在IplImage获取等高线,opencv,contour,iplimage,Opencv,Contour,Iplimage,我正在做一个运动检测项目,我想问我是否想在相机中获得图像,我需要使用IplImage而不是mat图像,我还想知道如果图像被定义为IplImage,我是否可以使用像vector轮廓这样的格式来定义轮廓,而不是cvseq。这是一个查找轮廓的程序 #include <iostream> #include <OpenCV/cv.h> #include <OPenCV/highgui.h> using namespace cv; using namespace std

我正在做一个运动检测项目,我想问我是否想在相机中获得图像,我需要使用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;
}
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
正确无误;
CvSeq*等高线=0;
CvMemStorage*storage=NULL;
CvCapture*凸轮;
i图像*当前帧,*当前帧灰,*差异,*旧帧灰;
bool first=true;
int main(int argc,char*argv[])
{
//创建新的电影捕获对象。
cam=cvCaptureFromCAM(0);
//为等高线创建存储
存储=cvCreateMemStorage(0);
//从网络摄像头捕获当前帧
currentFrame=cvQueryFrame(cam);
//图像的大小。
CvSize imgSize;
imgSize.width=当前帧->宽度;
imgSize.height=当前帧->高度;
//要在程序中使用的图像。
currentFrame_Gray=cvCreateImage(imgSize,IPL_DEPTH_8U,1);
而(1)
{
currentFrame=cvQueryFrame(cam);
如果(!currentFrame)中断;
//将图像转换为灰度。
CVT颜色(currentFrame、currentFrame_Gray、CV_RGB2GRAY);
if(first)//第一次捕获背景
{
差值=cvCloneImage(当前帧灰);
oldFrame_灰色=cvCloneImage(当前Frame_灰色);
CVV转换标度(当前帧灰、旧帧灰、1.0、0.0);
第一个=假;
继续;
}
//从移动平均值减去当前帧。
cvAbsDiff(旧帧灰、当前帧灰、差分);
//模糊差分图像
cvSmooth(差分、差分、CV_模糊);
//应用阈值以丢弃不需要的小移动
cvThreshold(differenceImg,differenceImg,25255,CV_THRESH_二进制);
//寻找轮廓
cvFindContours(差异、存储和轮廓);
//围绕每个轮廓绘制边界框
对于(;轮廓!=0;轮廓=轮廓->h_下一步)
{
rect=cvBoundingRect(轮廓,0);//提取当前轮廓的边界框
//绘图矩形
cvRectangle(当前帧,
cvPoint(矩形x,矩形y),
cvPoint(矩形x+矩形宽度,矩形y+矩形高度),
cvScalar(0,0,255,0),
2, 8, 0);                 
}
//显示带边框的彩色图像
cvShowImage(“输出图像”,当前帧);
//显示阈值图像
cvShowImage(“差异图像”,differenceImg);
//新背景
CVV转换标度(当前帧灰、旧帧灰、1.0、0.0);
//清晰的记忆和轮廓
cvClearMemStorage(存储);
等高线=0;
//按Esc键退出
char c=cvWaitKey(33);
如果(c==27)断开;
}
//销毁图像和电影对象
cvReleaseImage(&oldFrame_灰);
cvReleaseImage(&differenceImg);
cvReleaseImage(¤tFrame);
cvReleaseImage(¤tFrame_灰);
//cvReleaseCapture&cam;
返回0;
}

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

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

是的,你可以使用
向量
得到轮廓

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