Ask Your Question
0

How can I write pixel value in matrices?

asked 2013-08-15 10:03:21 -0600

Jack Chung gravatar image

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.

edit retag flag offensive close merge delete

Comments

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])

Moster gravatar imageMoster ( 2013-08-15 12:29:50 -0600 )edit

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.

Jack Chung gravatar imageJack Chung ( 2013-08-15 16:43:23 -0600 )edit

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.

Moster gravatar imageMoster ( 2013-08-16 00:50:24 -0600 )edit

1 answer

Sort by » oldest newest most voted
2

answered 2013-08-15 19:06:41 -0600

You try to print a pointer to a matrix (CvMat*) with printf. I think you want to print the pixel values? With the C++ version it's easier, but you could look at cvGet2D function to read pixel inside a loop. There is many code samples on internet and on OpenCV doc zone. But I write a c++ version of your code below. You should definitively used this as C version will be suppressed one day.

cv::Mat small, bin_img;
cv::Size size(20,20);
cv::Mat img = cv::imread( "Koala.jpg" );
if( !img.data )
{
    std::cerr << "Error, unable to load the image" << std::endl;
    return -1;
}
cv::resize( img, small, size );
cv::threshold( small, bin_img, 100, 255, CV_THRESH_BINARY);
// Efficient way of parsing images.
// See the doc below for all explanations
// and to see the non efficient way if it's more understandable for you.
int channels = bin_img.channels();
int nRows = bin_img.rows;
int nCols = bin_img.cols * channels;
if (bin_img.isContinuous())
{
    nCols *= nRows;
    nRows = 1;
}
int i,j;
uchar* p;
for( i = 0; i < nRows; ++i)
{
    p = bin_img.ptr<uchar>(i);
    for ( j = 0; j < nCols; ++j)
    {
        p[j] = 1;
    }
}
std::cout << img_bin << std::endl; // print a 20x20 array of 1.

Some resources needed to "understand" c++ version of OpenCV:

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-08-15 10:03:21 -0600

Seen: 2,811 times

Last updated: Aug 15 '13