OpenCV 的分段错误(核心转储)

Segmentation fault (core dumped) with OpenCV

本文关键字:核心 转储 错误 分段 OpenCV      更新时间:2023-10-16

我正在尝试编写一个程序,以消除一些连接的组件并保留其余组件。但是,在代码中的某个点,程序退出并显示错误消息"分段错误(核心转储("。

我已经将错误缩小到语句:"destinationImage.at(行,列(= labeledImage.at(行,列(;"使用检查点,您将找到下面的代码。

我已经尝试了我找到的所有解决方案,尤其是这个,但没有运气。

请帮忙!

还有一件事,程序正确读取图像,但没有按照代码显示原始图像。相反,它会打印一条消息"init done提供OpenGL支持"。这正常吗?imshow的实现是否在程序结束时没有错误?

/* Goal is to find all related components, eliminate secondary objects*/
#include <opencv2/core/utility.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
//Declaring variables
Mat originalImage;
int conComponentsCount;
int primaryComponents;
//Declaring constants
const char* keys =
{
    "{@image|../data/sample.jpg|image for converting to a grayscale}"  
};
//Functions prototypes, used to be able to define functions AFTER the "main" function
Mat BinarizeImage (Mat &, int thresh);
int AverageCCArea(Mat & CCLabelsStats,int numOfLabels, int minCCSize);
bool ComponentIsIncludedCheck (int ccArea, int referenceCCArea);
//Program mainstream============================================
int main (int argc, const char **argv)
{
    //Waiting for user to enter the required path, default path is defined in "keys" string
    CommandLineParser parser(argc, argv, keys);
    string inputImage = parser.get<string>(0);
    //Reading original image
    //NOTE: the program MUST terminate or loop back if the image was not loaded; functions below use reference to matrices and references CANNOT be null or empty.
    originalImage = imread(inputImage.c_str(), IMREAD_GRAYSCALE);// or: imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE)
    cout << " 1) Loading image done!" << endl;//CHECKPOINT
    if (originalImage.empty())
    {
        cout << "Nothing was loaded!";
        return -1; //terminating program with error feedback
    }
    cout << " 2) Checking for null Image done!" << endl;//CHECKPOINT
    namedWindow("Original Image", 0);
    imshow("Original Image", originalImage);
    cout << " 3) Showing ORIGINAL image done!" << endl;//CHECKPOINT
    //Image Binarization; connectedcomponents function only accepts binary images.
    int threshold=100; //Value chosen empirically.
    Mat binImg = BinarizeImage(originalImage, threshold);
    cout << " 4) Binarizing image done!" << endl;//CHECKPOINT
    //Finding the number of connected components and generating the labeled image.
    Mat labeledImage; //Image with connected components labeled.
    Mat stats, centroids; //Statistics of connected image's components.
    conComponentsCount = connectedComponentsWithStats(binImg, labeledImage, stats, centroids, 4, CV_16U);
    cout << " 5) Connecting pixels done!" << endl;//CHECKPOINT
    //Creating a new matrix to include the final image (without secondary objects)
    Mat destinationImage(labeledImage.size(), CV_16U);
    //Calculating the average of the labeled image components areas
    int ccSizeIncluded = 1000;
    int avgComponentArea = AverageCCArea(stats, conComponentsCount, ccSizeIncluded);
    cout << " 6) Calculating components avg area done!" << endl;//CHECKPOINT
    //Criteria for component sizes
    for (int row = 0; row <= labeledImage.rows; row++)
    {
        cout << " 6a) Starting rows loop iteration # " << row+1 << " done!" << endl;//CHECKPOINT
        for (int column = 0; column <= labeledImage.cols; column++)
        {
            //Criteria for component sizes
            int labelValue = labeledImage.at<int>(row, column);
            if (ComponentIsIncludedCheck (stats.at<int>(labelValue, CC_STAT_AREA), avgComponentArea))
            {
                //Setting pixel value to the "destinationImage"
                destinationImage.at<int>(row, column) = labeledImage.at<int>(row, column);
                cout << " 6b) Setting pixel (" << row << "," << column << ") done!" << endl;//CHECKPOINT
            }
            else
                cout << " 6c) Pixel (" << row << "," << column << ") Skipped!" << endl;//CHECKPOINT
        }
        cout << " 6d) Row " << row << " done!" << endl;//CHECKPOINT
    }
    cout << " 7) Showing FINAL image done!" << endl;//CHECKPOINT
    namedWindow("Final Image", 0);
    imshow("Final Image", destinationImage);
    cout << " 8) Program done!" << endl;//CHECKPOINT
    waitKey (0);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++
Mat BinarizeImage (Mat & originalImg, int threshold=100) //default value of threshold of grey content.
{
    // Binarization of image to be used in connectedcomponents function. 
    Mat bw = threshold < 128 ? (originalImg < threshold) : (originalImg > threshold);
    return bw;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++
int AverageCCArea(Mat & CCLabelsStats,int numOfLabels, int minCCSize) //calculates the average area of connected components without components smaller than minCCSize pixels..... reference is used to improve performance, passing-by-reference does not require copying the matrix to this function.
{
    int average;
    for (int i=1; i<=numOfLabels; i++)
    {
        int sum = 0;
        int validComponentsCount = numOfLabels - 1;
        if (CCLabelsStats.at<int>(i, CC_STAT_AREA) >= minCCSize)
        {
            sum += CCLabelsStats.at<int>(i, CC_STAT_AREA);
        }
        else
        {
            validComponentsCount--;
        }
        average = sum / (validComponentsCount);
    }
    return average;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++
bool ComponentIsIncludedCheck (int ccArea, int referenceCCArea)
{
    if (ccArea >= referenceCCArea)
    {
        return true; //Component should be included in the destination image
    }
    else
    {
        return false; //Component should NOT be included in the destination image
    }
}

更改以下内容:

for (int row = 0; row <= labeledImage.rows; row++)

对此:

for (int row = 0; row < labeledImage.rows; row++)

而这个:

 for (int column = 0; column <= labeledImage.cols; column++)

对此:

 for (int column = 0; column < labeledImage.cols; column++)

有什么好处吗?

(请记住,在C++中,我们从 0 开始计数,因此,如果例如 labeledImage.cols == 10,最后一列是索引为 9 的列(