How to apply a colormap to rgb image ?

조회 수: 10 (최근 30일)
Mohamed Khaled
Mohamed Khaled 2021년 4월 14일
댓글: Adam Danz 2021년 4월 15일
I'm trying to assign a colormap (parula) to rgb image, I tried to convert the original image to gray scale then assigning the colormap (as shown in the mathworks tutorials [colormap(target,map)]) but there is an error that keeps appearing that I don't understand which is [Error using colormap (line 49)First argument must be a scalar axes or figure handle.]. Thanks in advance !
The Code:
I=imread('Capture3.PNG');
I_gray= im2gray(I);
IH_gray=imshow(I_gray)
colormap(I_gray,jet(256));

채택된 답변

Adam Danz
Adam Danz 2021년 4월 14일
As the error message indicates, for the syntax colormap(target,map), target is an axis handle but you're not supplying an axis handle. You're supplying image data I.
IH_gray is the image object plotted to the current axes. Get the axis handle from that object's properties.
ax = ancestor(IH_gray,'axes'); % or ax = IH_gray.Parent;
colormap(ax,jet(256));
  댓글 수: 2
Mohamed Khaled
Mohamed Khaled 2021년 4월 14일
Thank you for the explanation! Everything worked just fine, but I can't really get what's the meaning of the "ancestor" or "parent" part, can you please explain what does that mean/do?
Adam Danz
Adam Danz 2021년 4월 14일
IH_gray is an object handle.
The object is plotted on a pair of axes.
The first input to the colormap(target,map) specifyies what object the colormap should belong to. You are assigning the colormap to the axes so target should be the handle to the axes that contains the IH_gray object.
In other words, you want the axis handle that contains the image data. The image is a child of the axes and the axes are a parent of the image.
IH_gray.Parent is the axis handle.
Another way to the axis handle is with ancestor(IH_gray,'axes').
See also

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

추가 답변 (2개)

David Hill
David Hill 2021년 4월 14일
I=imread('Capture3.PNG');
I_gray= im2gray(I);
imshow(I_gray);
colormap('jet');

Image Analyst
Image Analyst 2021년 4월 15일
If you pass in the colormap arguments correctly without specifying an axes, it will use the last axes you displayed into.
rgbImage = imread('Capture3.PNG');
if ndims(rgbImage) == 3
I_gray = im2gray(rgbImage); % Only call this if you're 100% sure it's an RGB image.
end
imshow(I_gray)
colormap(jet(256));
Or you can pass the colormap directly into imshow():
rgbImage = imread('Capture3.PNG');
if ndims(rgbImage) == 3
I_gray = im2gray(rgbImage); % Only call this if you're 100% sure it's an RGB image.
end
imshow(I_gray, 'Colormap', (jet(256));
You actually passed in the thing IN the axes, not the aces handle itself.
  댓글 수: 3
Image Analyst
Image Analyst 2021년 4월 15일
It is more explicit, yes. But you'd need to pass in gca or the output of subplot() rather than the output of imshow().
Adam Danz
Adam Danz 2021년 4월 15일
Right, or get the axis handle from the image object parent.

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

카테고리

Help CenterFile Exchange에서 Red에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by