Effacer les filtres
Effacer les filtres

How do I correct my colors in my indexed image displayed with image() function?

3 vues (au cours des 30 derniers jours)
Me
Me le 18 Nov 2022
Commenté : Me le 18 Nov 2022
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:
My Figure:
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)

Réponse acceptée

Image Analyst
Image Analyst le 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
  1 commentaire
Me
Me le 18 Nov 2022
I had it uncommented, but it wasn't working in the location I had it. I moved it to the bottom like the code you sent back and it worked. Thank you so much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Red dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by