C ++ - Convert Mat to array of integers in Opencv?

2

I'm doing an image manipulation project using OpenCV . I need to parse the pixels values 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'm trying to do this:

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 you try to display the array and compare its integer values with the image, the values do not match the pixels in the original image lines. Am I capturing the pixels values in the wrong way? Would anyone know the right way to do this? 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 is 674x35 in size, so I posted two zoomed images to show what I'm doing.

Whenprintingthevaluesofthelastlineoftheimage,Igetthefollowingvalues,however,theydonotmatchthepixelsoftheimage:

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

    
asked by anonymous 02.09.2016 / 21:20

1 answer

4

The iteration over the pixels of an image with Mat is done the way you already use it. Here is a sample 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, by reading the following image:

(expanded:)

Itresultsinthefollowing:

0000000000255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255

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

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

    
03.09.2016 / 00:29