How do I correct my colors in my indexed image displayed with image() function?
1 view (last 30 days)
Show older comments
For this problem I needed to code an image that contains five different colors placed in different places in a 40 x 40. My code gets the image to print, but the colors are wrong. There are different colors, but they are all variations of purple. I am not sure what I am doing wrong and what I need to do to fix it. I tried retyping the code and redoing the figure, but it didn't help. I also tried to look up several solutions and nothing I came across helped. I tried with variable names, numbers, colormap, no colormap, and I am not sure how to fix it. Help would be greatly appreciated. Thank you in advance!
Guidelines:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1198783/image.jpeg)
My Figure:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1198788/image.jpeg)
My Code:
teal = [0.08,0.25,0.3];
black = [0,0,0];
red = [1,0,0];
darkred = [0.24,0.008,0.008];
% 1 == teal;
% 2 == black;
% 3 == red;
% 4 == darkred;
% myColors = [teal; black; red; darkred];
%
% colormap(myColors)
pixelPicture = ones(40,40);
pixelPicture(1:10,1:5) = 1;
pixelPicture(11:20,1:5) = 4;
pixelPicture(21:40,1:5) = 1;
pixelPicture(1:5,6:10) = 4;
pixelPicture(6:40,6:10) = 1;
pixelPicture(1:5,11:15) = 1;
pixelPicture(6:10,11:15) = 4;
pixelPicture(11:15,11:15) = 1;
pixelPicture(16:20,11:15) = 3;
pixelPicture(21:25,11:15) = 4;
pixelPicture(26:30,11:15) = 1;
pixelPicture(31:40,11:15) = 2;
pixelPicture(1:15,16:20) = 1;
pixelPicture(16:25,16:20) = 3;
pixelPicture(26:40,16:20) = 1;
pixelPicture(1:15,21:25) = 1;
pixelPicture(16:20,21:25) = 3;
pixelPicture(21:25,21:25) = 4;
pixelPicture(26:40,21:25) = 1;
pixelPicture(1:5,26:30) = 1;
pixelPicture(6:10,26:30) = 4;
pixelPicture(11:15,26:30) = 1;
pixelPicture(16:25,26:30) = 4;
pixelPicture(26:30,26:30) = 1;
pixelPicture(31:40,26:30) = 2;
pixelPicture(1:5,31:35) = 4;
pixelPicture(6:40,31:35) = 1;
pixelPicture(1:10,36:40) = 1;
pixelPicture(11:15,36:40) = 3;
pixelPicture(16:20,36:40) = 4;
pixelPicture(21:40,36:40) = 1;
figure(2);
image(pixelPicture)
0 Comments
Accepted Answer
Image Analyst
on 18 Nov 2022
You aren't calling colormap function - it's commented out and was appearing before you displayed anything. This works fine:
teal = [0.08,0.25,0.3];
black = [0,0,0];
red = [1,0,0];
darkred = [0.24,0.008,0.008];
% 1 == teal;
% 2 == black;
% 3 == red;
% 4 == darkred;
pixelPicture = ones(40,40);
pixelPicture(1:10,1:5) = 1;
pixelPicture(11:20,1:5) = 4;
pixelPicture(21:40,1:5) = 1;
pixelPicture(1:5,6:10) = 4;
pixelPicture(6:40,6:10) = 1;
pixelPicture(1:5,11:15) = 1;
pixelPicture(6:10,11:15) = 4;
pixelPicture(11:15,11:15) = 1;
pixelPicture(16:20,11:15) = 3;
pixelPicture(21:25,11:15) = 4;
pixelPicture(26:30,11:15) = 1;
pixelPicture(31:40,11:15) = 2;
pixelPicture(1:15,16:20) = 1;
pixelPicture(16:25,16:20) = 3;
pixelPicture(26:40,16:20) = 1;
pixelPicture(1:15,21:25) = 1;
pixelPicture(16:20,21:25) = 3;
pixelPicture(21:25,21:25) = 4;
pixelPicture(26:40,21:25) = 1;
pixelPicture(1:5,26:30) = 1;
pixelPicture(6:10,26:30) = 4;
pixelPicture(11:15,26:30) = 1;
pixelPicture(16:25,26:30) = 4;
pixelPicture(26:30,26:30) = 1;
pixelPicture(31:40,26:30) = 2;
pixelPicture(1:5,31:35) = 4;
pixelPicture(6:40,31:35) = 1;
pixelPicture(1:10,36:40) = 1;
pixelPicture(11:15,36:40) = 3;
pixelPicture(16:20,36:40) = 4;
pixelPicture(21:40,36:40) = 1;
figure(2);
image(pixelPicture)
impixelinfo
myColors = [teal; black; red; darkred];
colormap(myColors)
colorbar
More Answers (0)
See Also
Categories
Find more on Red in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!