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

标签 opencv segmentation-fault runtime-error coredump

我遇到问题,尝试在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/

    相关文章:

    opencv - 如何将视频(帧)输入 GLSL 着色器

    java - 在没有 OpenCv 包管理器的情况下在 Eclipse 上创建 Android OpenCV 项目

    c++ - Visual Studio 2012 项目中的 tesseract Remove_Reference 模糊符号

    c++ - 使用 Pthreads 的 Hello World 内存损坏

    segmentation-fault - getAnalysis<DominatorTree>() 上的 LLVM 传递段错误

    mysql - 如何让 MySQL 在 SQL 中抛出条件运行时异常

    c++ - 调用vector.erase()函数时出现无效指针操作错误

    python - 在 Python 中使用 OpenCV 旋转图像时指定背景颜色

    java - java中的非法远程方法

    c - 如何制作 int val[100000000] 数组?