#Segment fault in OpenCV imread after using 2D image array changed into Mat

47 messages · Page 1 of 1 (latest)

hazy ocean
#

Hi guys, so I have wrote a program, such that it will read an image into Mat object and then change it into 2D arrays, which then gets constructed back into Mat.

So in the end, the Mat that was reconstructed seems to not able to be read within imread, and instead returns a segmentation fault error. What should I do in this case?

fringe tuskBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

vagrant bison
#

Use a debugger to figure out where the error occurred. Trace where the memory you tried to access comes from. Notice that you either didn't allocate any or not enough. Fix it.

hazy ocean
# vagrant bison Use a debugger to figure out where the error occurred. Trace where the memory yo...
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7180a73 in cv::opt_AVX2::cvtScale64f8u(unsigned char const*, unsigned long, unsigned char const*, unsigned long, unsigned char*, unsigned long, cv::Size_<int>, void*) () from /usr/local/lib/libopencv_core.so.407
(gdb) bt
#0  0x00007ffff7180a73 in cv::opt_AVX2::cvtScale64f8u(unsigned char const*, unsigned long, unsigned char const*, unsigned long, unsigned char*, unsigned long, cv::Size_<int>, void*) ()
    at /usr/local/lib/libopencv_core.so.407
#1  0x00007ffff6f0629e in cv::Mat::convertTo(cv::_OutputArray const&, int, double, double) const ()
    at /usr/local/lib/libopencv_core.so.407
#2  0x00007ffff7f9e9d4 in cvImageWidgetSetImage(_CvImageWidget*, void const*) ()
    at /usr/local/lib/libopencv_highgui.so.407
#3  0x00007ffff7fa5e58 in cv::impl::GTKWindow::imshow(cv::_InputArray const&) ()
    at /usr/local/lib/libopencv_highgui.so.407
#4  0x00007ffff7f9ab09 in cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&) () at /usr/local/lib/libopencv_highgui.so.407
#5  0x0000555555556c89 in main(int, char const**) (argc=1, argv=0x7fffffffdc78)
    at /home/kenny/01-University/Semester_6/Algorithm_Engineering/project/main.cpp:81
(gdb) 
#

got this from my debugger

#

seems like its a problem within opencv?

vagrant bison
#

No, assume that opencv is correct and the error is in your code. It crashed inside of opencv, but was called by main.cpp:81. I'd double-check the arguments in that line.

hazy ocean
#

its literally ```c++
imshow("Display window", A);

#

where A is ```c++
Mat A(img.rows, img.cols, CV_64F, flat_img);

vagrant bison
#

That narrows it down to A, whatever that is.

hazy ocean
#

and flat_img is just the same as the original Mat img, except in a flat array form

#

what I did was

#

I made a pipeline of

#

Mat -> 2D array -> flattened 1D array -> Mat

vagrant bison
#

Are you sure the size of flat_img is img.rows * img.cols * however many bytes CV_64F uses per pixel?

hazy ocean
#

first question yes

#

second one no o_o

#

wait, what do you mean by that? actually I just jerry rigged the code, and not sure what CV_64F is

vagrant bison
#

I'm fairly sure I only asked 1 question 🤔

hazy ocean
#

oh uh

#

flat_img is indeed img.rows * img.cols
however, im not sure what CV_64F mean

#

im using a single channel, grey image btw

vagrant bison
hazy ocean
#

so im guessing its only 8 bits unsigned char(whatever this mean? :o)

#

since the value is from 0-256

#

and btw I thought segmentation fault deals only with boundaries?

vagrant bison
#

CV_64F - 64-bit floating-point numbers

#

So you assume you need 1 byte per pixel but told it to use 8 byte per pixel which explains why it read past the end.

#

Maybe try CV_8U or something.

hazy ocean
#

I see!! very interesting

#

so its not about the array index itself, but the memory length?

vagrant bison
#

Well, when opencv indexes array index 1 it assumes it's 64 bit past the pointer.

hazy ocean
#

I got this as a result O.o

vagrant bison
#

So ... difficult to tell the difference between array index and memory length here.

vagrant bison
#

Maybe the format you read the data into is also not right?

hazy ocean
#

hmm... shouldn't be I think?

#

since its just simply a copy of itself

#

like the original image looks like this

#

so the whole code can be summarized like this

#
std::string image_path = samples::findFile("static/input_image_1.jpg");
Mat img = imread(image_path, IMREAD_GRAYSCALE);
// Allocate

    int **squares = new int *[img.rows];

    for (int x = 0; x < img.rows; ++x)
    {
        squares[x] = new int[img.cols];
    }
// Convert from Mat object to 2D Array

    for (int i = 0; i < img.rows; i++)
    {
        for (int j = 0; j < img.cols; j++)
        {
            int extracted = img.at<uchar>(i, j);
            std::cout << extracted << std::endl;
            squares[i][j] = extracted;
        }
    }
// Convert from 2D Array to Mat object
    int *flat_img = new int[img.rows*img.cols];

    int k = 0;
    for (int i = 0; i < img.rows; i++) {
        for (int j = 0; j < img.cols; j++) {
            flat_img[k] = squares[i][j];
            k++;
        }
    }

Mat A(img.rows, img.cols, CV_8U, flat_img);
imshow("Display window", img);
#

and if I replace img with A in imshow

#

it has the weird format effect thingy

vagrant bison
#

It's weird that you store unsigned chars in ints. There is also a chance you messed up the order. See if flat_img[k] = squares[j][i]; makes it better.

fringe tuskBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.