How to apply a colormap to rgb image ?
17 次查看(过去 30 天)
显示 更早的评论
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));
0 个评论
采纳的回答
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 个评论
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
2021-4-14
I=imread('Capture3.PNG');
I_gray= im2gray(I);
imshow(I_gray);
colormap('jet');
0 个评论
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
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().
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Red 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!