OpenCV处理视频的程序有点问题...
我现在用VS2008+OpenCV处理视频,把彩色视频灰度化,然后Canny边缘检测,最后把三个视频在同一个窗口中显示。
这个题是《学习OpenCV》第四章的课后习题第一题。
但是显示出来的三个画面全是边缘检测后的啊。是在找不出问题所在了,求大家指导~~
要把这些加入到项目的链接-输入-附加依赖项里面。opencv_core220d.lib opencv_highgui220d.lib opencv_imgproc220d.lib
代码:
#include "opencv\cv.h"#include "opencv\highgui.h"//Canny边缘检测算法IplImage* doCanny( IplImage* in, int lowThresh, int highThresh, int aperture ){ if(in->nChannels != 1)return 0; IplImage* out = cvCreateImage( cvGetSize(in), IPL_DEPTH_8U, 1); cvCanny(in,out,lowThresh,highThresh,aperture); return out;}int main(){ CvCapture* capture = cvCreateFileCapture("test.avi"); //把三个画面在一个窗口里面显示 cvNamedWindow("window_all"); cvNamedWindow("window"); cvNamedWindow("window_gray"); cvNamedWindow("window_Canny"); //视频的每一帧 IplImage* frame = NULL; frame = cvQueryFrame(capture); //视频帧图像的宽度和高度 int width = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH); int height = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT); //定义两种,是为了使三种图像的通道数一样,便于一起显示 //单通道灰度图像 IplImage* gray = cvCreateImage( cvSize(width, height), IPL_DEPTH_8U, 1); //3通道灰度图像 IplImage* frame_gray = cvCreateImage( cvSize(width, height), IPL_DEPTH_8U, 3); //单通道Canny边缘检测图像 IplImage* Canny = cvCreateImage( cvSize(width, height), IPL_DEPTH_8U, 1); //3通道Canny边缘检测图像 IplImage* frame_Canny = cvCreateImage( cvSize(width, height), IPL_DEPTH_8U, 3); //把3张图像放在一起显示 IplImage* frame_all = cvCreateImage( cvSize(width * 3,height), IPL_DEPTH_8U, 3); cvZero(frame_all); //定义三个图像头 IplImage* img1 = cvCreateImageHeader(cvSize(width, height),frame ->depth,3); IplImage* img2 = cvCreateImageHeader(cvSize(width, height),frame ->depth,3); IplImage* img3 = cvCreateImageHeader(cvSize(width, height),frame ->depth,3); img1 ->origin = frame ->origin; img1 ->widthStep = frame ->widthStep; img1->depth = frame->depth; img1 ->nChannels = 3; img2 ->origin = frame ->origin; img2 ->widthStep = frame ->widthStep; img2->depth = frame->depth; img2 ->nChannels = 3; img3 ->origin = frame ->origin; img3 ->widthStep = frame ->widthStep; img3->depth = frame->depth; img3 ->nChannels = 3; while(1){ frame = cvQueryFrame(capture); if(!frame){ return 1; } //将彩色图像转化为灰色图像 cvCvtColor(frame,gray,CV_RGB2GRAY); cvCvtColor(gray,frame_gray,CV_GRAY2BGR); Canny = doCanny(gray,10,100,3); cvCvtColor(Canny,frame_Canny,CV_GRAY2BGR); img1 ->imageData = frame_all ->imageData; cvCopy(frame,img1); img2 ->imageData = frame_all ->imageData + frame ->widthStep; cvCopy(frame_gray,img2); cvShowImage("window",img1); cvShowImage("window_gray",img2); img3 ->imageData = frame_all ->imageData + 2 * frame ->widthStep; cvCopy(frame_Canny,img3); cvShowImage("window_Canny",img3); cvShowImage("window_all",frame_all); if(cvWaitKey(33) == 27){//每秒显示30帧 break; } } cvReleaseImage(&frame); cvReleaseImage(&gray); cvReleaseImage(&frame_gray); cvReleaseImage(&Canny); cvReleaseImage(&frame_Canny); cvReleaseImage(&img1); cvReleaseImage(&img2); cvReleaseImage(&img3); cvReleaseCapture(&capture); cvDestroyWindow("window_all"); cvDestroyWindow("window"); cvDestroyWindow("window_gray"); cvDestroyWindow("window_Canny");}