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.
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:
The initial and final values of the line are completely wrong. Someone would know what might be happening?
Where is the statement /definition of "Mat" ? And the values that do not match, what are and what should be ?
– zentrunix
Hello, you’re on the first line 'Mat m'.
– Yuri Pires
@Joséx. I edited my post, I hope I have managed to explain my problem better, Thanks for the reply.
– Yuri Pires
"Mat" is neither part of the language nor the standard C++ library, it must be from Opencv then (which I don’t know)
– zentrunix
Mat is a type of array/container for images, it is from the opencv library. I am using opencv together with the c language++.
– Yuri Pires
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 value1
, that being >0 indicates 3 color channels). This can "explain" part of your problem.– Luiz Vieira
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.– Luiz Vieira
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).
– Luiz Vieira
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 thewidth
: 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.– Luiz Vieira
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.
– Yuri Pires
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.
– Yuri Pires
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.
– Luiz Vieira
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.– zentrunix