[英]Using ROI from camera feed as Template for cvMatchTemplate
這是我之前成功編寫的即時重寫代碼。
它假設使用網絡攝像頭的roi並將其與cvMatchTemplate匹配其他網絡攝像頭框架...我取出了軌跡欄和窗口以使其遵循指導原則,但在原始版本中,您可以移動軌跡欄以選擇框架的一部分在左上方的窗口和左下方的窗口中,您看到了模板,這是其外觀的圖片:
http://i983.photobucket.com/albums/ae313/edmoney777/Screenshotfrom2013-10-21112021_zpsae11e3f0.png
這是我正在嘗試將src的深度更改為32F的錯誤,沒有運氣...閱讀templmatch.cpp行384錯誤mssg給了我但沒有幫助
OpenCV Error: Assertion failed (result.size() == cv::Size(std::abs
(img.cols - templ.cols) + 1, std::abs(img.rows - templ.rows) + 1)
&& result.type() == CV_32F) in cvMatchTemplat
我是opencv的新手,可以使用一點幫助來調試下面的代碼
#include <cv.h>
#include <highgui.h>
using namespace std;
int main(){
CvCapture* capture =0;
capture = cvCaptureFromCAM(0);
if(!capture){
printf("Capture failure\n");
return -1;
}
IplImage* frame=0;
double width=640.0;
double height=480.0;
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height);
while(true){
frame = cvQueryFrame(capture);
if(!frame) break;
frame=cvCloneImage(frame);
IplImage *src, *templ, *ftmp[6]; // ftmp will hold results
int i;
CvRect roi;
int rectx = 0;
int recty = 0;
int rectwidth = frame->width /10;
int rectheight = frame->height /10;
IplImage* img = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
// Read in the source image to be searched
src = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
roi=cvRect(rectx, recty, rectwidth, rectheight);
img = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
src = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
cvCopy(frame, img);
cvSetImageROI(frame, roi);
cvShowImage( "b", img );
cvReleaseImage(&img);
// Allocate Output Images:
int iwidth = src->width - frame->width + 1;
int iheight = src->height - frame->height + 1;
for(i = 0; i < 6; ++i){
ftmp[i]= cvCreateImage( cvSize( iwidth, iheight ), 32, 1 );
}
// Do the matching of the template with the image
for( i = 0; i < 6; ++i ){
cvMatchTemplate( src, frame, ftmp[i], i );
cvNormalize( ftmp[i], ftmp[i], 1, 0, CV_MINMAX );
}
// DISPLAY
cvReleaseImage(&src);
cvResetImageROI(frame);
cvReleaseImage(&frame);
//Wait 50mS
int c = cvWaitKey(10);
//If 'ESC' is pressed, break the loop
if((char)c==27 ) break;
}
cvDestroyAllWindows() ;
cvReleaseCapture(&capture);
return 0;
}
我是OpenCV的新手,真的不知道如何處理此錯誤消息。 任何人的想法/指針怎么辦? 非常感謝您的幫助! 干杯,
在對圖像的選定ROI執行模板匹配時,必須根據ROI的大小創建輸出圖像。
// Allocate Output Images:
int iwidth = src->width - rectwidth + 1;
int iheight = src->height - rectheight + 1;
您的代碼包含可能導致程序崩潰的內存泄漏,例如,在每次迭代中都分配了ftmp
所有6個映像,但沒有在任何地方釋放它們。 在迭代結束時釋放圖像,或者在while
循環之前僅創建它們一次。
另外,OpenCV文檔明確聲明不要修改cvQueryFrame
返回的cvQueryFrame
。 因此,您可以考慮刪除cvReleaseImage(&frame);
。 查看此答案以獲取詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.