我遇到了一个问题,试图在 Ubuntu 18.04LTS 上使用 OpenCV 执行模板匹配
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main( int argc, char** argv )
{
int match_method =5;
string image_window = "Source Image";
string result_window = "Result window";
Mat img, templ, result;
/// Load image and template
img = imread("./RI2.jpg", IMREAD_GRAYSCALE );
templ = imread("./Pump2.jpg", IMREAD_GRAYSCALE );
/// Create windows
//namedWindow( image_window, WINDOW_AUTOSIZE );
//namedWindow( result_window, WINDOW_AUTOSIZE );
/// Source image to display
Mat img_display;
img.copyTo( img_display );
/// Create the result matrix
int result_cols = img.cols - templ.cols + 1;
int result_rows = img.rows - templ.rows + 1;
result.create( result_rows, result_cols, CV_32FC1 );
/// Do the Matching and Normalize
matchTemplate( img, templ, result, match_method );
normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
Mat resultgrey(result_rows, result_cols, CV_8UC1);
cout << "resultgrey.size().width: " << resultgrey.size().width << endl;
cout << "resultgrey.size().height: " << resultgrey.size().height << endl;
cout << "result.size().width: " << result.size().width << endl;
cout << "result.size().height: " << result.size().height << endl;
if( match_method == 0 || match_method == 1 )
{
for (int i=0; i<result.size().width; i++)
{
for (int j=0; j<result.size().height; j++)
{
if (result.at<float>(i,j)>=0.1)
{
resultgrey.at<int>(i,j)=0;
}
else
{
resultgrey.at<int>(i,j)=1;
}
}
}
}
else
{
for (int i=0; i<result.size().width; i++)
{
for (int j=0; j<result.size().height; j++)
{
if (result.at<float>(i,j)<=0.98)
{
resultgrey.at<int>(i,j)=0;
//cout << "0" << endl;
}
else
{
resultgrey.at<int>(i,j)=1;
//cout << "1" << endl;
}
}
}
}
cout << "3" << endl;
/// Localizing the objects
vector<Point> matchLoclist;
//cout << resultgrey << endl;
findNonZero(resultgrey, matchLoclist);
cout << "4" << endl;
if (matchLoclist.size() == 0)
{
cout << "no matches found" << endl;
return 0;
}
///Draw Rectangles on Pumps found in the scene
for (int i=0; i<matchLoclist.size(); i++)
{
//cout << "matchLoclist[i].x: "<<matchLoclist[i].x << endl << "matchLoclist[i].y: " << matchLoclist[i].y << endl;
rectangle( img_display, matchLoclist[i], Point( matchLoclist[i].x + templ.cols, matchLoclist[i].y + templ.rows ), Scalar::all(0), 2, 8, 0 );
rectangle( result, matchLoclist[i], Point( matchLoclist[i].x + templ.cols, matchLoclist[i].y + templ.rows ), Scalar::all(0), 2, 8, 0 );
}
imshow( image_window, img_display );
imshow( result_window, result );
waitKey(0);
return 0;
}
作为输出,我得到:
xxx@ubuntu:~/项目/Template_matching$ ./template_matching
结果灰色大小((.宽度: 1216
结果灰色大小((.高度: 723
结果大小((.宽度: 1216
结果大小((.高度: 723
分段故障(核心转储(
这发生在双 for 循环期间,其中 1 或 0 被写入"resultrgrey",因为我从未从下面的 cout 获得"3"作为输出
如果我拍摄不同的输入图片(尤其是较小的图片(,程序往往会在没有此错误的情况下运行。
我感谢任何帮助或建议!
亚历克斯
由于 (1( 错误地指定数据类型和 (2( 将参数交换到.at
@rafix07,因此您在分配的缓冲区之外写入。
您创建 8 位矩阵(CV_8UC1
8 位(:
Mat resultgrey(result_rows, result_cols, CV_8UC1);
但尝试在 double-for 循环中为其元素分配 32 位值:
resultgrey.at<int>(i,j)=0;
模板方法cv::Mat::at
根据以下内容计算内存中第(i,j)
个元素的地址:
- 数据类型,在模板实例化中指定,
- 指向存储在
cv::Mat
实例中的数据启动的指针, - 和数据步幅(两条连续行最左侧像素之间的距离(以字节为单位(,也存储在
cv::Mat
实例中。
然后它返回对它的引用。为了速度,不执行任何检查,因此您有责任提交正确的参数。
在大多数现代平台上,int
的大小为 32 位,但可以有所不同。
通常,使用标头中的类型更安全stdint.h
这些类型具有显式长度并登录其名称:uint8_t
、int32_t
等
查看有关Mat::at
方法的参考资料
const _Tp& cv::Mat::at ( int i0, int i1 ) const Parameters i0 Index along the dimension 0 i1 Index along the dimension 1
第一个 dim 是行数,第二个 dim 是列数,因此您应该使用at
更改代码中的所有行
resultgrey.at<int>(i,j) // i means col, j means row
自
resultgrey.at<int>(j,i)