- Red if it's value is [1 0 0]
- Green if it's value is [0 1 0]
- Blue if it's value is [0 0 1]
command image changing colors
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Srivardhan Gadila
on 2 May 2020
Edited: Srivardhan Gadila
on 6 May 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
~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 Comments
Srivardhan Gadila
on 6 May 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.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!