OpenCV

时间:2016-02-16 21:16:19

标签: c++ opencv image-processing

我正在尝试编写一个程序来消除一些连接的组件并保留其余组件。 但是,在代码中的某个时刻,程序退出并显示错误消息“Segmentation fault(core dumped)”。

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

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

请帮助!

还有一件事,程序正确读取图像,但没有按照代码显示原始图像。相反,它会打印一条消息“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
    }
}

1 个答案:

答案 0 :(得分:1)

改变这个:

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的那一列