Opencv模板匹配:数据损坏

时间:2014-07-21 23:05:32

标签: c++ opencv

嗨,这是模板匹配的代码。我正在做8种不同模板的匹配。 现在问题是我想无限期地运行该程序。但是当我试图插入一个无限循环时,我的程序运行一次,我得到了错误:

我在打印参考和模板图像的深度,宽度,类型和高度后观察到,在'for'循环的一个完整循环之后,模板的参数被更改。 我认为这是数据损坏的问题。我对这个问题感到无助。 请帮我。

avenger@ubuntu:~/opencv/opencv-2.4.9/finalproject/templet_matching$ ./temp 
init done 
opengl support available 
Referece Image Mat Width:960
Referece Image Mat Hight:720
Referece Image Mat depth:0
Referece Image Mat type:16

Saved new_ref008.jpg

在for循环的第一个周期中模板一个数据

Template Matched 
Referece Template Mat Width:190
Referece  Template Mat Hight:195
Referece  Template Mat depth:0
Referece  Template Mat type:16

Template Matched 
Template Matched 
Template Matched 
Template Matched 
Template Matched 
Template Matched 
Template Matched 

Referece Image Mat Width:800
Referece Image Mat Hight:600
Referece Image Mat depth:0
Referece Image Mat type:16 

for循环1个循环后的模板一个数据

Referece Template Mat Width:611
Referece  Template Mat Hight:406
Referece  Template Mat depth:5
Referece  Template Mat type:5



OpenCV Error: Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F) &&     img.type() == templ.type()) in matchTemplate, file /home/avenger/opencv/opencv-2.4.9/modules/imgproc/src/templmatch.cpp, line 249
terminate called after throwing an instance of 'cv::Exception'
what():  /home/avenger/opencv/opencv-2.4.9/modules/imgproc/src/templmatch.cpp:249:    error: (-215) (img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type() in function matchTemplate

Aborted (core dumped)

这是我的代码:相机未打开,因为我没有连接它。

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include<opencv2/opencv.hpp>
#include"camera.h"
extern "C"
{
#include"serial.h"
}
using namespace cv;
int temp_count=0;
unsigned char byte;
unsigned char data_byte[8];


int main(void)
{
cv::Mat image = cv::imread("ref.jpg",1);  
cv::Mat ref_image;
image.copyTo(ref_image);


cv:: Mat des[7];   // Array for template matching result (destination) 
cv::Mat ref_temp[7]; // Array for templates or ROI in Reference image
ref_temp[0] = cv:: Mat(ref_image, cv::Rect(167, 85, 433, 455));
ref_temp[1] = cv:: Mat(ref_image, cv::Rect(550,85, 433, 455));
ref_temp[2] = cv:: Mat(ref_image, cv::Rect(1042,85,433, 455));
ref_temp[3] = cv:: Mat(ref_image, cv::Rect(1528,85,433, 455));
ref_temp[4] = cv:: Mat(ref_image, cv::Rect(65, 1010, 423, 442));
ref_temp[5] = cv:: Mat(ref_image, cv::Rect(548, 1010, 423, 442));
ref_temp[6] = cv:: Mat(ref_image, cv::Rect(1025, 1010, 423, 442));
ref_temp[7] = cv:: Mat(ref_image, cv::Rect(1529, 1010, 423, 442));
namedWindow("Match Result", CV_WINDOW_KEEPRATIO );
while(1){
         capture_image(ref_image);// Capture new frame from camera and copy it to ref_image                         
           for(temp_count=0;temp_count<8;temp_count++)
             {

              cv::matchTemplate(ref_image, ref_temp[temp_count], des[temp_count], CV_TM_CCORR_NORMED);



               while (true)
                {
                double minval, maxval;
                    Point minloc, maxloc;    
                            cv::minMaxLoc(des[temp_count], &minval, &maxval, &minloc, &maxloc);
                    if (maxval >= 0.8){
                        cv::rectangle(ref_image,maxloc,cv::Point(maxloc.x + ref_temp[temp_count].cols, maxloc.y+ref_temp[temp_count].rows),CV_RGB  (0,255,0),2,8);                      
                                                                                                                    data_byte[temp_count]=1;                                
                        std::cout <<  "Template Matched " << std::endl ;

                        break;                  
                        }
                    else
                        std::cout <<  "Template NOT Matched" << std::endl ;
                            data_byte[temp_count]=0;
                        break;


                }   
       cout << "after match" << endl;
       cout << "Referece Image Mat Width:" << ref_temp[temp_count].cols << endl;
       cout << "Referece Image Mat Hight:" << ref_temp[temp_count].rows << endl;
       cout << "Referece Image Mat depth:" << ref_temp[temp_count].depth() << endl;
       cout << "Referece Image Mat type:" << ref_temp[temp_count].type() << endl;   
     //cout << "after match" << endl;
     cout << "Referece Template Mat Width:" << ref_temp[temp_count].cols << endl;
     cout << "Referece  Template Mat Hight:" << ref_temp[temp_count].rows << endl;
     cout << "Referece  Template Mat depth:" << ref_temp[temp_count].depth() << endl;
     cout << "Referece  Template Mat type:" << ref_temp[temp_count].type() << endl;


 }
// Here I am creating one byte of data as per the result of template match
 byte |= (data_byte[0])|(data_byte[1]<<1)|(data_byte[2]<<2)|(data_byte[3]<<3)|
                (data_byte[4]<<4)|(data_byte[5]<<5)|(data_byte[6]<<6)|(data_byte[7]<<7);  


 namedWindow("Reference Image", CV_WINDOW_KEEPRATIO );
 cv::imshow("Reference Image",image);
 cv::imshow("Match Result",ref_image);
}
return 0;

}

0 个答案:

没有答案