How can I write pixel value in matrices?
Hello,
How can I write a value in a matrice?
I already created a matrice, read a binary image and get the pixel value, now I would like to write the pixel value in the matrice.
I would appreciate it if someone could help me to set the pixel value in the matrice.
The code that I read a binary image and try to write the pixel value in the matrice is:
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
uchar binario, valor;
if(fp == NULL){
printf("\nNão encontrei arquivo\n");
exit(EXIT_FAILURE);
}
CvMat* polar = cvCreateMat(20, 172, CV_8UC1);
cvNamedWindow( "original", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "new", CV_WINDOW_AUTOSIZE );
IplImage* src = cvLoadImage("Koala.jpg", CV_LOAD_IMAGE_GRAYSCALE);
cvShowImage("original", src);
CvSize size = cvSize(40, 40);
IplImage* resize = cvCreateImage(size, src->depth, src->nChannels);
cvResize(src, resize, CV_INTER_LINEAR);
IplImage* img_binario = cvCreateImage(cvGetSize(resize), IPL_DEPTH_8U, 1);
cvThreshold(resize, img_binario, 100, 255, CV_THRESH_BINARY);
for(int i = 0; i < 20; i++){
//*((uchar*)CV_MAT_ELEM_PTR(*polar, i, 1)) = ((uchar *)(img_binario->imageData + i*img_binario->widthStep))[20];
cvSet2D(polar, i, 1, ((uchar *)(img_binario->imageData + i*img_binario->widthStep))[20]);
}
printf("Valor: %i", polar);
cvShowImage("new", img_binario);
cvWaitKey(0);
return 0;
}
Thank you.
Is there any special reason that you use the old C style and not c++? Btw, I only made a small test and used cvSetReal2D(test, 0, 0, (uchar) 1); This was working for me. The Mat contained a 1 at (0,0) then. Maybe it works better like this:
uchar* data = (uchar *)img_binario->imageData;
cvSetReal2D(polar, i, 1, data[ i * img_binario->widthStep + 20*img_binario->nChannels])
The reason to use the old C is because I don't know how to use c++. Well, I tried to use what you said but it still not working, it is showing weird numbers in the console instead of 0 and 1 from the binary image.
Obviously you only see bullshit in the console. What do you think printing a cvMat does? it gives back a pointer and you try to print it as an Int. Thats not really going to work.