gpt4 book ai didi

opencv - 分割失败(核心转储)与OpenCV一起使用

转载 作者:行者123 更新时间:2023-12-02 17:07:54 25 4
gpt4 key购买 nike

我遇到问题,尝试在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:〜/ Projects / Template_matching $ ./template_matching

resultgrey.size()。width:1216

resultgrey.size()。height:723

result.size()。width:1216

result.size()。高度: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) -th元素的地址:
  • 数据类型,在模板实例化
  • 中指定
    指向数据开始的
  • 指针,存储在cv::Mat实例
  • 和数据跨度(两个连续行的最左边像素之间的字节距离)也存储在cv::Mat实例中。

  • 然后,它返回对其的引用。为了提高速度,不执行任何检查,因此,您有责任提交正确的参数。

    在大多数现代平台上, int的大小为32位,但可以有所不同。

    通常,使用 stdint.h header 中具有明确长度并键入其名称的类型更安全: uint8_tint32_t

    关于opencv - 分割失败(核心转储)与OpenCV一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50581201/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com