필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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)

채택된 답변

Image Analyst
Image Analyst 2022년 11월 18일
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
Me
Me 2022년 11월 18일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Red에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by