opencv将视频canny后显示播放一卡一卡的,是不是需要的canny时间太长了,怎么解决
发布网友
发布时间:2022-04-26 07:54
我来回答
共1个回答
热心网友
时间:2023-10-08 23:34
显示卡的话可能是申请的内存没有释放,处理过程中申请的内存使用后要释放;
下面是我的代码,VS2010+OpenCV2.3和VC6.0+OpenCV1.0下都能运行通过,没有一卡一卡现象。
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
#include "cxcore.h"
#include "windows.h"
int main()
{
CvCapture *capture = NULL;
IplImage *frame = NULL;
char *AviFileName = "D:\\AVI\\22.avi";//视频的目录
char *AviSavePath = "D:\\截图\\";//图片保存的位置, 注意“截图”文件夹需要手动创建
const int jiange = 1;//间隔 X 帧保存一次图片
capture = cvCaptureFromAVI(AviFileName);
//获取视频信息
cvQueryFrame(capture);
int frameH = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
int frameW = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
int numFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
printf("\tvideo height : %d\n\tvideo width : %d\n\tfps : %d\n\tframe numbers : %d\n", frameH, frameW, fps, numFrames);
if(!capture)
{
printf("Could not initialize capturing...\n");
return 0;
}
cvNamedWindow("AVI player",1);
int count_tmp = 0;//计数总帧数
char tmpfile[100]={'\0'};
while(true)
{
if(cvGrabFrame(capture))
{
if (count_tmp % jiange == 0)
{
frame=cvRetrieveFrame(capture);
IplImage* img_Canny = cvCreateImage(cvGetSize(frame),frame->depth,1);
IplImage *dst_gray = cvCreateImage(cvGetSize(frame),frame->depth,1);
cvCvtColor(frame,dst_gray,CV_RGB2GRAY);//将RGB三通道图像转化为单通道灰度图
cvCanny(dst_gray,img_Canny,60,200,3);//canny算法检测边缘
cvShowImage("AVI player",img_Canny);//显示当前帧
sprintf(tmpfile,"%s\\%d.jpg",AviSavePath,count_tmp);//使用帧号作为图片名
// if(count_tmp==90)
cvReleaseImage(&img_Canny);
cvReleaseImage(&dst_gray);
cvSaveImage(tmpfile,frame);
}
if(cvWaitKey(10)>=0) //延时
break;
++count_tmp;
}
else
{
break;
}
}
cvReleaseCapture(&capture);
cvDestroyWindow("AVI player");
printf("总帧数:%d\n",count_tmp);
}