How to apply a colormap to rgb image ?
조회 수: 10 (최근 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!