OpenCV Utility: Reading Image Pixels Value

Mar 5, 2010 | Tags: OpenCV | del.icio.us del.icio.us | digg Digg

This is a utility to read the pixel value of an image at the specified location. When the user's mouse clicks on the image, it reads the corresponding pixel and displays the RGB value. Useful for debugging.

Listing 1: readpixel.c

  1. #include "cv.h"
  2. #include "highgui.h"
  3.  
  4. void
  5. mouseHandler(int event, int x, int y, int flags, void* param)
  6. {
  7.     IplImage* img0, * img1;
  8.     CvFont    font;
  9.     uchar*    ptr;
  10.     char      label[20];
  11.  
  12.     img0 = (IplImage*) param;
  13.     img1 = cvCloneImage(img0);
  14.  
  15.     cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, .8, .8, 0, 1, 8);
  16.  
  17.     if (event == CV_EVENT_LBUTTONDOWN)
  18.     {
  19.         /* read pixel */
  20.         ptr = cvPtr2D(img1, y, x, NULL);
  21.  
  22.         /*
  23.          * display the BGR value
  24.          */
  25.         sprintf(label, "(%d, %d, %d)", ptr[0], ptr[1], ptr[2]);
  26.  
  27.         cvRectangle(
  28.             img1,
  29.             cvPoint(x, y - 12),
  30.             cvPoint(x + 100, y + 4),
  31.             CV_RGB(255, 0, 0),
  32.             CV_FILLED,
  33.             8, 0
  34.         );
  35.  
  36.         cvPutText(
  37.             img1,
  38.             label,
  39.             cvPoint(x, y),
  40.             &font,
  41.             CV_RGB(255, 255, 0)
  42.         );
  43.  
  44.         cvShowImage("img", img1);
  45.     }
  46. }
  47.  
  48. int
  49. main(int argc, char** argv)
  50. {
  51.     IplImage* img;
  52.  
  53.     /* usage: <prog_name> <image> */
  54.     if (argc != 2) {
  55.         printf("Usage: %s <image>\n", argv[0]);
  56.         return 1;
  57.     }
  58.  
  59.     /* load image */
  60.     img = cvLoadImage(argv[1], 1);
  61.  
  62.     /* always check */
  63.     assert(img);
  64.  
  65.     /* create a window and install mouse handler */
  66.     cvNamedWindow("img", 1);
  67.     cvSetMouseCallback("img", mouseHandler, (void*)img);
  68.  
  69.     cvShowImage("img", img);
  70.  
  71.     cvWaitKey(0);
  72.  
  73.     /* be tidy */
  74.     cvDestroyAllWindows();
  75.     cvReleaseImage(&img);
  76.  
  77.     return 0;
  78. }

Related Articles

Recommended Books

8 Comments

sumon on Mar 5, 2010:

Dear Nash,

Thanks a lot for opencv utility. Really its great useful for me, to check the RGB values at different position. Now I am trying to create a XLs file to show rgb values at different x, y position, for analysis and making graph. But yet i am not success. Therefore, if possible pls help me, so that i can use opencv utility with using mouse and xls file.

Thanking you
Sumon

swati A jaiyawala on Jul 13, 2010:

Respected sir,
I am about to start my final year project "Real time/fast implemantation license plate recognition" using opencv. Opencv is totaly new for me and i am working on it now-a-days and i am facing couple of problems with it. I know how to display images but i don't know where to read image pixels or read image in matrix form, another is how to work on that pixels. Second thing is that in opencv, how execution time is measured of that whole recognition process.

Waiting for your quick response

Sumon on Aug 21, 2010:

Dear Nash,

I have got the solution of detecting RGB and HSI values. Previously during image binarization I used RGB values for selecting threshold value. Now for further analysis, I need to threshold based on HSI. But yet i dont find any ways for binarization. Therefore, If possible, please let me know your advise in this regards.

Thanking you


Alex on Sep 1, 2010:

Thanks for this program! I find that it works nicely for the first few clicks, but it continually crashes after I try to find more than 3 RGB values. This problem may not affect other users, but it makes the program unusable for me as it manages to freeze my whole computer.

Just wanted to report this possible problem! I don't know how to fix it as I'm still a relative beginner to openCV.

Alex on Sep 1, 2010:

Hey, I found the problem. The mousehandler method is called every time the user even hovers the mouse over the image, which ends up being a good 20 times per second. This allocates a ton of memory, and after long enough, the program crashes. Often the problem is complicated by the program crashing while the mouse is on the window, in which even more space is allocated.

The solution is to declare the pointers to IplImages and such within the if(left mouse click) conditional block. That way the mouse checking method can be called many times, but memory doesn't get out of control.

Hope this helps other users!
Alex

Bndesh Kumar Singh on Nov 13, 2010:

Dear Nashruddin,

Thanks for your tutorials on OpenCV. You have made simple tutorials which focus on few things at one time. That's good because people generally dump long projects which take time to understand that diverts the focus of reader.

yok on Jun 14, 2011:

Thank you. I was finding the way to get rgb pixel with opencv. This help me now.

Goh on Jul 31, 2011:

hi,

I agree with Alex, the program will run out of memory after a long hovering. It is easy to overcome this problem, just remember to cvReleaseImage at the end of the mouseHandler function.

Leave a comment

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000