command image changing colors

조회 수: 19 (최근 30일)
Alejandro Alarcon
Alejandro Alarcon 2020년 4월 26일
편집: Alejandro Alarcon 2020년 5월 6일
I have an matrix called "A" of 3x3 with values on it, either 0 or 1. Im using the command image to display the colors but i need to display the image ina way that if the value is 0, it show that pixel in color red and if the value is 1. then it show the pixel on green. Anyone has an idea how can i code that please?
Example
A =
1 1 1
1 0 1
1 1 0
so, im trying to show an image that looks like this
A =
red red red
red green red
red red green

답변 (1개)

Srivardhan Gadila
Srivardhan Gadila 2020년 5월 2일
편집: Srivardhan Gadila 2020년 5월 6일
The following code might help you:
A = [1 1 1;1 0 1; 1 1 0]
%Create an image matrix same as size(A) with 3 RGB channels
img = zeros([size(A) 3])
%Channel one corresponds to red
img(:,:,1) = ~A;
%Channel 2 corresponds to green
img(:,:,2) = A;
img
imshow(img)
Refer to CData - Image color data argument in Color and Transparency of Image Properties for more information.
For an color image(RGB) matrix img, img(:,:,1) corresponds to channel R ie., red pixels, img(:,:,2) corresponds to channel G i.e., green pixels & img(:,:,3) corresponds to channel B ie., blue pixels.
img(i,j,:) corresponds to (i,j)'th pixel and the image color would be
  1. Red if it's value is [1 0 0]
  2. Green if it's value is [0 1 0]
  3. Blue if it's value is [0 0 1]
~A flips the binary values in the matrix i.e., the if the value of A(i,j) is 1 then the output of ~A(i,j) would be 0 & if the value of A(i,j) is 0 then the output of ~A(i,j) would be 1.
The RGB triplet of color yellow is [255 255 0] (in uint8) or [1 1 0] (0-1 normalized). So if you want an (i,j)'th pixel to be yellow color then change the value of img(i,j,:) to [1 1 0]
img(i,j,:) = [1 1 0]
  댓글 수: 2
Alejandro Alarcon
Alejandro Alarcon 2020년 5월 6일
편집: Alejandro Alarcon 2020년 5월 6일
I implemented your code to my program and it worked.
Thank you for your help
Srivardhan Gadila
Srivardhan Gadila 2020년 5월 6일
@Alejandro Alarcon, there was a small error in the initial code. I have updated the answer with the right code and some explanation w.r.t your comment.

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

Community Treasure Hunt

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

Start Hunting!

Translated by