command image changing colors
이전 댓글 표시
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
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
- 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]
~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
2020년 5월 6일
편집: Alejandro Alarcon
2020년 5월 6일
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.
카테고리
도움말 센터 및 File Exchange에서 Contrast Adjustment에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!