How to change RGB value without changing index value of an image?
3 views (last 30 days)
Show older comments
Emre Doruk
on 15 Feb 2023
Commented: Emre Doruk
on 20 Feb 2023
Hi,
I have NxM matrix, I want to plot iamge of it and the change specific rgb value without changing index value. How can I do that? I add a sample image.
For example pixel [26,28] and Index 87 have meaningful value for me. I want to change color of this pixel to red without changing index value 87.
Thanks for your help.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1296275/image.png)
0 Comments
Accepted Answer
Walter Roberson
on 15 Feb 2023
You would have to change the active colormap at index 87 to be red, which would change all other locations with the same value.
Alternatives:
You could create an overlay image the same size with AlphaData 0.0 except at locations that you wish to recolor. You will probably want to use an rgb image to contain the additional color. You would turn hittest off on the overlay image so that the data cursor would report for the original image.
Or you could change the data cursor callback function so that it outputs your values of interest even though you have changed the image data
I would have to check to see if image objects are eligible to use the newer datatip templates
6 Comments
More Answers (1)
Image Analyst
on 19 Feb 2023
Try imoverlay
% Make sample data.
rows = 10;
columns = 40;
indexedImage = randi(255, [rows, columns], 'uint8');
% Show original image.
subplot(2, 1, 1);
imshow(indexedImage, [])
% Create mask of where the image is exactly 87.
mask = indexedImage == 87;
% overlay the mask onto the original image, creating an RGB image.
rgbImage = imoverlay(indexedImage, mask, 'r');
% Display the overlay image.
subplot(2, 1, 2);
imshow(rgbImage)
3 Comments
Image Analyst
on 20 Feb 2023
They said "I want to change color of this pixel to red without changing index value 87." Walter gave one way - to apply a colormap. I gave an alternative way - using an overlay. I did not change the original pixel value. I just created a new image with red in those pixel locations. imoverlay is a function worth knowing about. The original poster can use the method they like best.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!