Ask Your Question
1

Accessing pixel value always return same value

asked 2012-12-03 22:03:40 -0600

NAYOSO gravatar image

Hello right now I'm trying to capture an grayscale image from the webcam and access it the value of every pixel of the image. At first I used this code

IplImage* processed_frame = (*custom_cb)(frame);  

      if(key==13)
      {
          IplImage *destination = cvCreateImage(cvSize(20,20),processed_frame->depth, processed_frame->nChannels);
          cvResize(processed_frame, destination);

          cvShowImage("frame resize", destination);

          mat=cvCreateMat(destination->height,destination->width,CV_32F );
          for(int i=0;i<mat.rows;i++)
            {
                for(int j=0;j<mat.cols;j++)
                {

                   printf( "(%.f) ",mat.at<float>(i,j) );
                }
              printf("\n");
            }
      }

I'm trying to convert the IplImage to mat and access every pixel of the mat but it always return the same value for every pixel then I'm trying to access the IplImage directly

IplImage* processed_frame = (*custom_cb)(frame);  

      if(key==13)
      {
          IplImage *destination = cvCreateImage(cvSize(20,20),processed_frame->depth, processed_frame->nChannels);
          cvResize(processed_frame, destination);
          for(int i=0;i<20;i++)
            {
                for(int j=0;j<20;j++)
                {

                   printf( "(%.f) ",CV_IMAGE_ELEM(destination,float,i,j) );
                }
              printf("\n");
            }
      }

it gives me various value for every pixel and every image I try to capture but I'm still not sure if I'm doing it right. What I want to ask is why the first code always returning same value and is the second code the right way to get the pixel value?

Thanks

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
4

answered 2012-12-04 00:12:37 -0600

Vladislav Vinogradov gravatar image

You don't convert IplImage to mat, you just create new Mat object

mat=cvCreateMat(destination->height, destination->width, CV_32F);

To convert from IplImage to Mat use Mat(IplImage*) constructor

mat = Mat(destination);
edit flag offensive delete link more

Comments

thank you Vladislav! it works perfectly!

NAYOSO gravatar imageNAYOSO ( 2012-12-04 06:18:57 -0600 )edit
1

answered 2012-12-04 03:15:16 -0600

Siegfried gravatar image

updated 2012-12-04 03:16:07 -0600

Hi, since Vladislav gave you the right answer, I only want to recommend you the OpenCV tutorial about "How to go through each and every pixel of an image?".

edit flag offensive delete link more

Comments

thank you siegfried!

NAYOSO gravatar imageNAYOSO ( 2012-12-04 06:19:16 -0600 )edit

Question Tools

Stats

Asked: 2012-12-03 22:03:40 -0600

Seen: 1,089 times

Last updated: Dec 04 '12