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

조회 수: 1 (최근 30일)
Alberto Acri
Alberto Acri 2023년 8월 15일
답변: Image Analyst 2023년 8월 17일
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;

답변 (2개)

Florian Bidaud
Florian Bidaud 2023년 8월 15일
편집: Florian Bidaud 2023년 8월 15일
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
DGM
DGM 2023년 8월 15일
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 2023년 8월 15일
Indeed that is probably better to use im2double. Just for information for the reader, selectedpix2/255 will give the same as selectedpix3.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2023년 8월 17일
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.

카테고리

Help CenterFile Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by