3 images fused into RGB placed upon one another
4 views (last 30 days)
Show older comments
3 images into RED,GREEN and BLUE ,placed onto one another and with the background being black
cl_img_1 = init_img_gs('img1.png');
cl_img_2 = init_img_gs('img2.png');
cl_img_3 = init_img_gs('img3.png');
img_out= cat(3,cl_img_t_gs,cl_img_c_gs,cl_img_p_gs);
figure(4), subplot(1,1,1), imshow(img_out);
1 Comment
Rik
on 10 Dec 2021
How do you decide which color should take precedence? There are 8 combinations, but you only want 4 colors (black+RGB), so you need to decide.
Accepted Answer
Voss
on 10 Dec 2021
A = imread('triangle.png');
B = logical(imread('circle.png')); % convert uint8 to logical to be consistent with A and C
C = imread('parr.png');
AA = zeros(size(A),'uint8');
BB = zeros(size(A),'uint8');
CC = zeros(size(A),'uint8');
AA(A & ~B & ~C) = 255; % red is 255 where the triangle exists and the others don't
BB(B & ~C) = 255; % green is 255 where the circle exists and parallelogram does not
CC(C) = 255; % blue parallelogram on top
out = cat(3,AA,BB,CC);
imwrite(out,'finaloutput_test.png');
0 Comments
More Answers (1)
DGM
on 14 Dec 2023
Edited: DGM
on 14 Dec 2023
I can only assume that things have been edited, because as it stands, there isn't really a question. Since it's accepted, I'm going to assume that Voss's composition is the intended layer and color sequence.
% the images as read
A = imread('triangle.png'); % logical
B = imread('circle.png'); % uint8
C = imread('parr.png'); % logical
% layer colors (from BG to FG)
CT = [0 0 0; 1 0 0; 0 1 0; 0 0 1]; % any colors, any class
% compose the image one layer at a time
% replacepixels(FG,BG,mask)
outpict = replacepixels(CT(2,:),CT(1,:),A); % FG, BG are tuples, mask is I
outpict = replacepixels(CT(3,:),outpict,B); % FG is tuple, BG is RGB, mask is I
outpict = replacepixels(CT(4,:),outpict,C); % FG is tuple, BG is RGB, mask is I
imshow(outpict)
Note that in this composition, we have a mix of double, logical, and uint8 data, but the class mismatches don't matter so long as everything is properly-scaled for its respective class. The output class is inherited from BG, which in this case is CT(1,:) (i.e. 'double'). Also note that our inputs don't need to have the same size, so long as they can be unambiguously expanded to a common size.
Besides being class-agnostic, this workflow seamlessly supports both binarized and graduated masks. It also allows us to simply use any color sequence we want without being constrained to simple primary-secondary schemes, as happens with simple channel combinations.
% the images as read
A = imread('triangle.png'); % logical
B = imread('circle.png'); % uint8
C = imread('parr.png'); % logical
% say they're not binarized masks
A = imgaussfilt(im2double(A),5);
B = imgaussfilt(B,3);
C = imgaussfilt(im2double(C),1);
% any layer colors we want
CT = [0 0 0.2; 0.3 0 0.5; 0.6 0 0.5; 0.8 0 0.5];
% the composition is exactly the same
outpict = replacepixels(CT(2,:),CT(1,:),A);
outpict = replacepixels(CT(3,:),outpict,B);
outpict = replacepixels(CT(4,:),outpict,C);
imshow(outpict)
While it's doubtful that OP ever needed that sort of flexibility, it's not clear what OP actually asked for. The only thing I can do is provide information to passers-by.
0 Comments
See Also
Categories
Find more on Convert Image Type 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!