Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.