opencv帮我看看怎么这样呢,轮廓无法读出来

我最近呢,正在做运动识别跟踪程序,用opencv想着把运动目标提取出来加上轮廓求质心呢,结果有错误。

#include"cv.h"
#include"highgui.h"
#include "cxcore.h"


#include <D:\opencv\opencv\build\include\opencv2\legacy\legacy.hpp>
using namespace std;
using namespace cv;
IplImage* doCanny(IplImage* in,
double lowThresh,
double highThresh,
double aperture);
int main(int argc,char** argv)
{
cvNamedWindow("trace",CV_WINDOW_AUTOSIZE);
cvNamedWindow("fore",CV_WINDOW_AUTOSIZE);
CvCapture* capture=cvCreateFileCapture("bike.avi");
IplImage* frame1;
IplImage* curr=NULL;//use to store the first image 
IplImage* next=NULL;//use to store the second image
IplImage* fore=NULL;//use to store the diversity between the first and the second image
IplImage* result=NULL;
IplImage* result1=NULL;
IplImage* result2=NULL;
IplImage* img2=NULL;
IplImage* img3=NULL;
double fps=cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
int frames=(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT);
int flag=0;
    while(frames>60)
{
//first  should  get the first image from the video 
  frame1=cvQueryFrame(capture);
   curr=cvQueryFrame(capture);
   flag++;
if(flag==1)
{
next=cvCreateImage(cvGetSize(curr),8,3);
fore=cvCreateImage(cvGetSize(curr),8,3);
result=cvCreateImage(cvGetSize(curr),8,3);
result1=cvCreateImage(cvGetSize(curr),8,3);
result2=cvCreateImage(cvGetSize(curr),8,3);

cvCopyImage(curr,next);//set the next is current iamge  
}
else
{
cvAbsDiff(curr,next,fore);
               //增加对于差图的处理
cvSmooth(fore,result,CV_GAUSSIAN,3,3);//delete some noise
 
CvMemStorage* storage=cvCreateMemStorage(0);//apply new store space
CvSeq* comp=0;
cvPyrSegmentation(result,result1,storage,&comp,4,200,50);
int n_comp=comp->total;//get the connected area number,we hope is 1,if not equals 1 we will use the Dilate
     img3=cvCreateImage(cvGetSize(curr),8,3);
                  cvThreshold(result1,result2, 60, 255, CV_THRESH_BINARY);
 
    cvDilate(result2,img3,NULL,2);
 
     int mode=CV_RETR_EXTERNAL;
 cvFindContours(img3,storage,&comp,sizeof(CvContour),mode,CV_CHAIN_APPROX_SIMPLE);//一到这里就出问题了,编译无错误,运行出现错误了
 int i;
 for(i=1;comp!=0;comp->h_next)
 {//get the area
 double area=fabs(cvContourArea(comp,CV_WHOLE_SEQ));
 CvScalar color = CV_RGB(i, rand()&255, rand()&255);
                    cvDrawContours(img3, comp, color, color, -1, CV_FILLED, 8 );
                     i++; // label递增
 }
//  img3=doCanny(img2,10.0,100,3);//make a canny detect
               cvShowImage("trace",frame1);
  cvShowImage("fore",img3);
       char s=cvWaitKey(33);
   if(s==27) break;
frames--;
    }
}
           cvReleaseCapture(&capture);   
           
                 cvDestroyWindow("fore");
            cvDestroyWindow("trace");

              
return(0);
}




要判断两个轮廓是否相交,可以利用OpenCV提供的函数cv2.intersectConvexConvex()来实现。该函数可以判断两个凸多边形是否相交。如果两个轮廓都是凸多边形,则可以先使用cv2.convexHull()函数将它们转换为凸多边形,再利用cv2.intersectConvexConvex()函数进行判断。 以下是一个判断两个轮廓是否相交的示例代码: ```python import cv2 # 读取两个轮廓 contour1 = cv2.imread('contour1.jpg') contour2 = cv2.imread('contour2.jpg') # 将轮廓转换为灰度图像 gray1 = cv2.cvtColor(contour1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(contour2, cv2.COLOR_BGR2GRAY) # 对灰度图像进行二值化处理 ret1, thresh1 = cv2.threshold(gray1, 127, 255, cv2.THRESH_BINARY) ret2, thresh2 = cv2.threshold(gray2, 127, 255, cv2.THRESH_BINARY) # 查找轮廓 contours1, hierarchy1 = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contours2, hierarchy2 = cv2.findContours(thresh2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 将轮廓转换为凸多边形 hull1 = cv2.convexHull(contours1[0]) hull2 = cv2.convexHull(contours2[0]) # 判断两个凸多边形是否相交 retval, intersection = cv2.intersectConvexConvex(hull1, hull2) if retval: print('两个轮廓相交') else: print('两个轮廓不相交') ``` 在以上代码中,我们首先读取了两个轮廓图像,并将它们转换为灰度图像。接着对灰度图像进行了二值化处理,并使用cv2.findContours()函数查找轮廓。由于cv2.intersectConvexConvex()函数要求输入的轮廓为凸多边形,因此我们使用cv2.convexHull()函数将轮廓转换为凸多边形。最后,利用cv2.intersectConvexConvex()函数判断两个凸多边形是否相交。如果返回值retval为True,则表示两个凸多边形相交;否则,表示两个凸多边形不相交。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天使之一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值