繁体   English   中英

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

[英]Segmentation fault (core dumped) with OpenCV

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

我将错误缩小为以下语句:“ destinationImage.at(row,column)= labeledImage.at(row,column);” 使用检查点,您将在下面找到代码。

我尝试了所有发现的解决方案,尤其是这一方法 ,但是没有运气。

请帮忙!

还有一件事,该程序可以正确读取图像,但不会按照代码显示原始图像。 而是打印一条消息“可以完成初始化的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的那一列)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM