Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Opencv模板匹配:数据损坏_C++_Opencv - Fatal编程技术网

C++ Opencv模板匹配:数据损坏

C++ Opencv模板匹配:数据损坏,c++,opencv,C++,Opencv,嗨,这是模板匹配的代码。我正在做8个不同的模板匹配。 现在的问题是我想无限期地运行这个程序。但当我试图插入无限循环时,我的程序运行了一次,我得到了错误: 我在打印参考和模板图像的深度、宽度、类型和高度后观察到,在一个完整的“for”循环后,模板的参数发生了变化。 我认为这是数据损坏的问题。在这个问题上我感到无助。 请帮帮我 avenger@ubuntu:~/opencv/opencv-2.4.9/finalproject/templet_matching$ ./temp init done

嗨,这是模板匹配的代码。我正在做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循环的第一个周期中模板一个数据

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

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

#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;

}

功能捕获图像应该做什么?它抛出的断言失败错误通常是因为映像不存在。cv::Mat ref_temp[7];//我们三个人中有一个不会计数。@a-Jays capture\u image功能是从相机中获取图像,并返回一帧作为参考图像。问题是,每当我试图实现while1循环时,我都会遇到这个错误。否则它会正常工作。@berak的评论很有道理,哈!您仍然没有更改cv::Mat ref_temp[7];至cv::材料参考温度[8];
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;