identify the pixel color as a single number (no RGB triplet)

4 views (last 30 days)
Hi. I am using the function you find at this link1 (but you can also use Image Viewer App explained at this link2).
I should ask for some information:
1. Is there a way to display the single number (not the RGB triplet) as visible in link2 (see "pixel region tool")?
2. Is there a way to display the single number directly on the image as shown? (see image on the left)

Accepted Answer

Adam Danz
Adam Danz on 23 Jan 2023
Is there a way to display the single number (not the RGB triplet)?
Here are two ways to explore the CData in your image interactively.
Show CData value under the cursor
This uses the axes currentPoint property that shows the location of your cursor within the axes. It assumes that the image's XData and YData properties are default where pixels are centered on consecutive integers. Pardon the poor GIF quality.
rgb = imread('moon.tif');
I = imshow(rgb);
colorbar()
ax = ancestor(I,'axes');
hold on
txt = text(ax,nan, nan, '', 'FontSize', 10, 'Color','r','VerticalAlignment','bottom');
set(gcf,'WindowButtonMotionFcn', {@showCData,txt,ax,I})
function showCData(~,~,txt,ax,I)
currpt = round(ax.CurrentPoint(1,1:2));
isInImg = inpolygon(currpt(1),currpt(2),ax.XLim([1 2 2 1]),ax.YLim([1 1 2 2]));
if isInImg
% Find nearest pixel
str = string(I.CData(currpt(2),currpt(1)));
set(txt,'Position', [currpt,0], 'String', str)
else
txt.String = '';
end
end
Show all CData values within a subsection of the image
Showing all CData values across the entire image would exhaust the graphics system unless the image was very small. Insead, this use the LimitsChangedFcn to detect when the x-axis limits have changed (you can adapt it to respond to the y-axis too). When the limits result in a smaller subsection of pixels, a threshold I set to 15 pixels along the x-axis, the CData values will turn on within the frame. This demo also assumes that the image's XData and YData properties are default where pixels are centered on consecutive integers.
rgb = imread('moon.tif');
I = imshow(rgb);
colorbar()
hold on
ax = gca();
ax.XAxis.LimitsChangedFcn = {@imageZoomLabeler,I};
function imageZoomLabeler(ruler,limitsChanged,img)
xrng = limitsChanged.NewLimits(2)-limitsChanged.NewLimits(1);
persistent texthandles
delete(texthandles)
if xrng < 15 % set threshold
x = ceil(limitsChanged.NewLimits(1)) : floor(limitsChanged.NewLimits(2));
yl = ruler.Parent.YLim;
y = ceil(yl(1)) : floor(yl(2));
xm = x.*ones(numel(y),1);
ym = y.'.*ones(1,numel(x));
ind = sub2ind(size(img.CData),ym,xm);
str = string(img.CData(ind));
texthandles = text(ruler.Parent,xm(:),ym(:),str(:), ...
'FontSize',10,'Color','r',...
'VerticalAlignment','middle',...
'HorizontalAlignment','center');
end
end

More Answers (0)

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!