coloration the pixels matlab
11 views (last 30 days)
Show older comments
I have two images image 1 and image 2 . I want to realise the image 3.
If image1(x,y) ==0 , image2(x,y)==0 so image3(x,y) color in Black
if image1(x,y) ==1 , image2(x,y)==0 so image3(x,y) color in Red
if image1(x,y) ==1 , image2(x,y)==1 so image3(x,y) color in white
if image1(x,y) ==0 , image2(x,y)==1 so image3(x,y) color in Blue
I want to realise image 3
helpme plzz
0 Comments
Answers (1)
Rik
on 24 Feb 2022
Edited: Rik
on 24 Feb 2022
The easiest way to do this is to create an index image and then use ind2rgb to convert to a color image.
im1=imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/905945/image%201.JPG');
im2=imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/905920/image%202.JPG');
%crop and convert to 0 and 1
im1=mean(im1(1:65,1:64,:),3)>128;
im2=mean(im2(1:65,1:64,:),3)>128;
im3=NaN(size(im1));
im3(im1==0 & im2==0)=1; %edited
im3(im1==1 & im2==0)=2;
im3(im1==1 & im2==1)=3;
im3(im1==0 & im2==1)=4;
map=[...
0 0 0;
1 0 0;
1 1 1;
0 0 1];
im3=ind2rgb(im3,map);
subplot(1,3,1),imshow(im1),title('im1')
subplot(1,3,2),imshow(im2),title('im2')
subplot(1,3,3),imshow(im3),title('im3')
4 Comments
Rik
on 24 Feb 2022
I didn't read the doc carefully enough:
- If you specify X as an array of integer data type, then the value 0 corresponds to the first color in the colormap map. For a colormap containing c colors, values of image X are clipped to the range [0, c-1].
- If you specify X as an array of data type single or double, then the value 1 corresponds to the first color in the colormap. For a colormap containing c colors, values of image X are clipped to the range [1, c].
Because im3 is a double, that means the first value should have been 1, not 0. You can either fix that, or a new line in the colormap.
% Simplified example to make the effects more visisble.
im1=[0 0;1 1];
im2=[0 1;0 1];
im3=NaN(size(im1));
im3(im1==0 & im2==0)=1;
im3(im1==1 & im2==0)=2;
im3(im1==1 & im2==1)=3;
im3(im1==0 & im2==1)=4;
map=[...
0 0 0;
1 0 0;
1 1 1;
0 0 1];
im3=ind2rgb(im3,map);
subplot(1,3,1),imshow(im1),title('im1')
subplot(1,3,2),imshow(im2),title('im2')
subplot(1,3,3),imshow(im3),title('im3')
See Also
Categories
Find more on White in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
