【发布时间】:2011-01-25 16:47:39
【问题描述】:
我正在使用 OpenCV 进行学校项目。该程序的主要部分将是直方图的比较。将有一个直方图数据库,新的直方图将从实时视频馈送中创建,然后与数据库中的直方图进行比较。现在我只是想从视频源中正确创建直方图。 我的问题是程序在随机的时间间隔内崩溃或显着变慢。所以我的问题是如何防止程序崩溃或变慢? OpenCV 对我来说一直有点不稳定,所以我不确定这是我的代码有问题还是只是OpenCV 的本质。如果与我的代码有关,我认为该问题可能与帧速率有关(猜测/直觉)。我正在使用“cvWaitKey”来“调整”帧的加载速度,但“Learning OpenCV”一书中有关于“cvWaitKey”的说法
c = cvWaitKey(33); if( c == 27 ) 中断; 一旦我们显示了帧,我们就等待 33 毫秒。如果用户按下一个键,那么 c 将设置为该键的 ASCII 值;如果不是,则将其设置为 –1。如果用户点击 Esc 键(ASCII 27),然后我们将退出读取循环。否则,将经过 33 ms 并且 我们将再次执行循环。 值得注意的是,在这个简单的例子中,我们并没有明确控制 任何智能方式的视频速度。我们完全依赖于定时器 cvWaitKey() 来调整帧的加载速度。在更复杂的应用程序中,它会 明智地从 CvCapture 结构(来自 AVI)和 相应地行事!
您将在下面的代码(从here 修改)中看到,我的循环在开始下一次执行之前等待了 10 毫秒。通常程序运行时完全没有问题,但有时它会在不到一分钟或五分钟内崩溃,我真的无法检测到模式。 欢迎任何关于如何防止崩溃(或减速)的建议。 另外我应该补充一点,我正在使用 OpenCV 1.1(永远无法让 OpenCV 2.0 正常工作),我是使用 Visual Studio 2008,每次修改代码时都会创建一个 .MSI 安装程序包,也就是说,我不在 Visual Studio 中调试。依赖项是 cv110.dll、cxcore110.dll 和 highgui110.dll。我的代码如下:
// SLC (Histogram).cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <cxcore.h>
#include <cv.h>
#include <cvaux.h>
#include <highgui.h>
#include <stdio.h>
#include <sstream>
#include <iostream>
using namespace std;
int main(){
CvCapture* capture = cvCaptureFromCAM(0);
if(!cvQueryFrame(capture)){
cout<<"Video capture failed, please check the camera."<<endl;
}
else{
cout<<"Video camera capture successful!"<<endl;
};
CvSize sz = cvGetSize(cvQueryFrame(capture));
IplImage* image = cvCreateImage(sz, 8, 3);
IplImage* imgHistogram = 0;
IplImage* gray = 0;
CvHistogram* hist;
cvNamedWindow("Image Source",1);
cvNamedWindow("Histogram",1);
for(;;){
image = cvQueryFrame(capture);
//Size of the histogram -1D histogram
int bins = 256;
int hsize[] = {bins};
//Max and min value of the histogram
float max_value = 0, min_value = 0;
//Value and normalized value
float value;
int normalized;
//Ranges - grayscale 0 to 256
float xranges[] = {0, 256};
float* ranges[] = {xranges};
//Create an 8 bit single channel image to hold a grayscale version of the original picture
gray = cvCreateImage(cvGetSize(image), 8, 1);
cvCvtColor(image, gray, CV_BGR2GRAY);
//Planes to obtain the histogram, in this case just one
IplImage* planes[] = {gray};
//Get the histogram and some info about it
hist = cvCreateHist(1, hsize, CV_HIST_ARRAY, ranges,1);
cvCalcHist(planes, hist, 0, NULL);
cvGetMinMaxHistValue(hist, &min_value, &max_value);
printf("Minimum Histogram Value: %f, Maximum Histogram Value: %f\n", min_value, max_value);
//Create an 8 bits single channel image to hold the histogram and paint it white
imgHistogram = cvCreateImage(cvSize(bins, 50),8,3);
cvRectangle(imgHistogram, cvPoint(0,0), cvPoint(256,50), CV_RGB(255,255,255),-1);
//Draw the histogram
for(int i=0; i < bins; i++){
value = cvQueryHistValue_1D(hist, i);
normalized = cvRound(value*50/max_value);
cvLine(imgHistogram,cvPoint(i,50), cvPoint(i,50-normalized), CV_RGB(0,0,0));
}
cvFlip(image, NULL, 1);
cvShowImage("Image Source", image);
cvShowImage("Histogram", imgHistogram);
//Page 19 paragraph 3 of "Learning OpenCV" tells us why we DO NOT use "cvReleaseImage(&image)" in this section
cvReleaseImage(&imgHistogram);
cvReleaseImage(&gray);
cvReleaseHist(&hist);
char c = cvWaitKey(10);
//if ASCII key 27 (esc) is pressed then loop breaks
if(c==27) break;
}
cvReleaseImage(&image);
cvReleaseCapture(&capture);
cvDestroyAllWindows();
}
【问题讨论】:
标签: c++ crash opencv performance slowdown