command image changing colors

18 visualizzazioni (ultimi 30 giorni)
Alejandro Alarcon
Alejandro Alarcon il 26 Apr 2020
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

Risposte (1)

Srivardhan Gadila
Srivardhan Gadila il 2 Mag 2020
Modificato: Srivardhan Gadila il 6 Mag 2020
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 Commenti
Alejandro Alarcon
Alejandro Alarcon il 6 Mag 2020
Modificato: Alejandro Alarcon il 6 Mag 2020
I implemented your code to my program and it worked.
Thank you for your help
Srivardhan Gadila
Srivardhan Gadila il 6 Mag 2020
@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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by