Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Trying to do some basic operations with images, code keeps crashing and I don't know why.

Hello. I'll start by saying that I'm just now starting to learn to use OpenCV libraries and I never was an expert on C++.

I want to create a smaller Mat element out of an image and do some simple processing to it. So I created a simple function with iterations to introduce elements of the original image in a 3x3 Mat element and return it, but the code keeps crashing, even when I don't crate a function, but use it outside. I haven't been able to identify a cause. I'd appreciate help making it clear to me as to why it doesn't work. Thanks beforehand, here's my very simple piece of code:

    #include <opencv2\opencv.hpp>

using namespace cv;
using namespace std;

cv::Mat Sort3x3(cv::Mat source, int y, int x) {
    int i=0, j=0;
    cv::Mat Wind9;
    Wind9 = (3,3,CV_8UC1, (0));


    while (i<2){
        j=0;
        while (j<2){
            Wind9.at<uchar>(i,j) = source.at<uchar>(y-1+i,x-1+j);
            j=j+1;
        }
        i = i+1;
    }

    return(Wind9);
}


int main( int argc, char** argv ) {

    Mat Source = imread("saltpepper1.png", IMREAD_GRAYSCALE);
    cv::Mat Wind9;
    int i=40,j=40; //Some random test numbers for a position in the image
    Wind9 = Sort3x3(Source, i, j);






       waitKey(0);

}