C++ - Convert Mat to Opencv integer array?

Asked

Viewed 1,039 times

2

I’m doing an image manipulation project using the Opencv. I need to analyze the values of pixels of a "binarized" image, for this I am trying to convert my Mat file image to a array of integers, more specifically an array of integers. I am trying to do as follows:

Mat m;
m = imread("C:/Users/Syn/Desktop/filtesteBranco.jpg");

int matriz[this->m.size().width][this->m.size().height]; 
int i, j; 

// inicializar matriz com zeros //
for (i = 0; i < (m.rows); i++){ 
    for (j = 0; j <(m.cols); j++) 
    {
        matriz[i][j] = 0;
    }
}

for (int x = 0; x < (m.rows); x++) // varredura da matriz
{
    for (int y = 0; y<(m.cols); y++)
    {
        matriz[x][y] = m.at<uchar>(x,y);    // capturando os pixels
    }
}     

However, when trying to display the array and compare its integer values to the image, the values do not match the appropriate pixels on the lines of the original image. I’m capturing the values of pixels the wrong way? Would anyone know the right way to do it? Thanks in advance.

EDIT:

Below is the test I’m doing, I added 3 white pixels at the beginning and end of the last line of the image. The image in question has size 674x35, so I posted two zoom images to show what I’m doing.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

When printing the values of the last line of the image, I got the following values, however, they do not match the pixels of the image:

inserir a descrição da imagem aqui

The initial and final values of the line are completely wrong. Someone would know what might be happening?

  • 1

    Where is the statement /definition of "Mat" ? And the values that do not match, what are and what should be ?

  • Hello, you’re on the first line 'Mat m'.

  • @Joséx. I edited my post, I hope I have managed to explain my problem better, Thanks for the reply.

  • "Mat" is neither part of the language nor the standard C++ library, it must be from Opencv then (which I don’t know)

  • Mat is a type of array/container for images, it is from the opencv library. I am using opencv together with the c language++.

  • You say your image is "binarized", but you’re reading it from a jpg and 3-channel color format (read the documentation from imread - the parameter flags has default value 1, that being >0 indicates 3 color channels). This can "explain" part of your problem.

  • Also, what you are doing is totally unnecessary. A Mat is already an array of pixels that you can (should) use in your processing. It makes no sense to convert to another similar data structure.

  • The other possible "part" of your problem may be that your image is not actually binarized. Where did you create it? If you happened to use an image editor and chose to draw a line instead of setting the value pixel by pixel, it is likely that the editor tried to "soften" the line design (read on anti-aliasing on Wikipedia), and so you have some values 2 and 1 (which, on a color scale from 0 to 255, are very close to black).

  • 1

    Another problem is that you are mixing size with Rows/cols (reversing in the statement of his matrix). The number of rows (Rows) is actually equal to height and not to the width: http://stackoverflow.com/a/32971309/2896619 It is curious not to have given an invalid access error to the memory area, since the image has dimensions with very different sizes.

  • I changed the value of the imread flag to 0, this solved part of my problem, there were still some noises in the image, some 255 were lost in the middle of some lines of the image, but in the image there is nothing noisy, I think you’re right, maybe it’s the image format as you said.

  • About the image being binarized, this example with jpg was to test in a simpler way another problem I’m having. I was taking a binarized image actually coming from Mat with Threshold applied, but when it comes to listing pixels, their values are wrong in much of the matrix. Is there any way to traverse the Mat matrix linearly as mat[i][j] and print its values, @Luizvieira? This way I could better identify what is happening in the image.

  • JPG is not very good for binarized images, because this format does not store the bit map but a compression. It will always generate garbage. Test with BMP.

  • 1

    By the way, your program is actually compiling ? This one int matriz[this->m.size().width][this->m.size().height] should not compile, because native arrays need to have constant dimensions, and it seems to me that "m->size" is not constant.

Show 8 more comments

1 answer

4


The iteration over the pixels of an image with Mat is made the way you already use it. Here is an example program:

#include "opencv2/core.hpp"
#include "opencv2/opencv.hpp"

using namespace cv;

int main(int argc, char**argv)
{
    Mat m;
    m = imread("C:/temp/teste.bmp", 0);

    for(int x = m.rows-1; x < (m.rows); x++)
    {
        for(int y = 0; y < (m.cols); y++)
        {
            printf("%d ", m.at<uchar>(x, y));
        }
    }

    return 0;
}

This program, reading the following image:

inserir a descrição da imagem aqui

(enlarged:)

inserir a descrição da imagem aqui

Results in the following:

0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255

But note that the image, being "binary", needs to be recorded as BMP (Bit Map), so that the pixel values are directly recorded and not any compressed format. Also note that the call from imread should read the image in a single channel (so the second parameter as 0).

If you use JPG, it can generate "pixels" with details missing due to the compression used:

inserir a descrição da imagem aqui

  • 1

    Thank you @Luizvieira. I checked the lines you had mentioned before also, the width and height order in my matrix statement, when reversing as you had said, the pixels were correct now. In fact I was confused at the time of declaring things. Thank you, I will use the direct Mat file to scan and work with the image, it seems much more efficient even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.