saving the R G B values displayed with impixelinfo in an image into a matrix

1 view (last 30 days)
Hi. How can I save the R G B values displayed with impixelinfo in an image into a matrix? (see red box)
Here is a demo image:
h = imshow("hestain.png");
hp = impixelinfo;

Answers (2)

Florian Bidaud
Florian Bidaud on 15 Aug 2023
Edited: Florian Bidaud on 15 Aug 2023
Everything is already stored in h, you can retrieve the values with h.CData(pixel_x, pixel_y, :). Be careful the values are unit8, if you want double, you need to convert them with double() function
  2 Comments
DGM
DGM on 15 Aug 2023
You are correct. For arithmetic operations, being in uint8 will often cause problems by itself.
Unless the user is cautious and aware of how the interpretation of image data scaling is dependent on numeric class, I suggest that it's safer to use im2double() to make sure the outputs are scaled appropriately for their class. For example, let's say you used some method to get the colors associated with a handful of pixels:
inpict = imread('peppers.png'); % a uint8 image
pixidx = [156 54684 3158 5648 3546]; % some indices
allpix = reshape(inpict,[],3);
selectedpix1 = allpix(pixidx,:) % the pixels as a Mx3 color table
selectedpix1 = 5×3
66 31 59 174 66 67 64 35 70 216 56 39 60 31 62
selectedpix2 = double(selectedpix1) % using double()
selectedpix2 = 5×3
66 31 59 174 66 67 64 35 70 216 56 39 60 31 62
selectedpix3 = im2double(selectedpix1) % using im2double()
selectedpix3 = 5×3
0.2588 0.1216 0.2314 0.6824 0.2588 0.2627 0.2510 0.1373 0.2745 0.8471 0.2196 0.1529 0.2353 0.1216 0.2431
Most MATLAB image tools that depend on data scale (e.g. imshow()/imwrite()) will handle case 1 or 3. Most MATLAB tools which handle color tables (e.g. rgb2ind()/ind2rgb()/imshow()/imwrite()/colormap()) will only correctly handle case 3. With case 2, those functions will either interpret the above colors as all white, or they will simply return an error.
Florian Bidaud
Florian Bidaud on 15 Aug 2023
Indeed that is probably better to use im2double. Just for information for the reader, selectedpix2/255 will give the same as selectedpix3.

Sign in to comment.


Image Analyst
Image Analyst on 17 Aug 2023
Use imread to read it into an array before you display it. Then you'll have all the RGB values. It's much easier to do it this way than
rgbImage = imread("hestain.png");
imshow(rgbImage);
axis('on', 'image');
hp = impixelinfo;
If you want to mouse around and have it automatically save the RGB and (x,y) values in the status bar, I don't think you can do that. However you can call imfreehand to have the user draw some curve in the image, and then use the curve coordinates to extract the RG value, then you can write them to disk.
I'm attaching some freehand drawing demos.

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!