concatenate multiple 2D arrays to a 3D array for an image file.

7 views (last 30 days)
Lynne SOUTH
Lynne SOUTH on 31 Mar 2022
Edited: Kevin Holly on 31 Mar 2022
I have an image file (.jpg) that I am trying to display on a graph side by side with the original of the image. After splitting the 3D color array into 3 2D arrays (RGB), I made an edit to the file, and tried to concatenate the layers back into a 3D matrix, but instead it's displaying the images side by side. How do I get the file to display the image with all 3 2D colormaps on top of each other instead of displaying them side by side?
[WhiteImg, colorMap] = imread('OrigImg.jpg');
Rlayer = ...
WhiteImg( :, :, 1);
Glayer = ...
WhiteImg( :, :, 2);
Blayer = ...
WhiteImg( :, :, 3);
% FIND WHITE PIXELS
logArray = ...
(Rlayer > 220) & (Glayer > 220) & (Blayer > 220);
intensity( :, :) = ...
uint8(mean([Rlayer, Glayer, Blayer], 3));
% INDEX NEW RGB LAYERS
Rnew = ...
intensity(:, :);
Gnew = ...
intensity(:, :);
Bnew = ...
intensity(:, :);
% SWAP WHITE FOR RED COLOR
Rnew(logArray) = 255;
Gnew(logArray) = 0;
Bnew(logArray) = 0;
% CONCAT
RedImg = ...
cat(3, Rnew, Gnew, Bnew);
% PLOTS
figure
blockPlots = subplot(1, 2, 1);
image(WhiteImg);
axis image
axis off
blockPlots = subplot(1, 2, 2);
image(RedImg);
axis image
axis off

Answers (1)

Kevin Holly
Kevin Holly on 31 Mar 2022
Edited: Kevin Holly on 31 Mar 2022
[WhiteImg, colorMap] = imread('peppers.png');
Rlayer = ...
WhiteImg( :, :, 1);
Glayer = ...
WhiteImg( :, :, 2);
Blayer = ...
WhiteImg( :, :, 3);
% FIND WHITE PIXELS
logArray = ...
(Rlayer > 220) & (Glayer > 220) & (Blayer > 220);
intensity = uint8(mean(WhiteImg,3));
% INDEX NEW RGB LAYERS
Rnew = intensity;
Gnew = intensity;
Bnew = intensity;
% SWAP WHITE FOR RED COLOR
Rnew(logArray) = 255;
Gnew(logArray) = 0;
Bnew(logArray) = 0;
% CONCAT
RedImg = WhiteImg;
RedImg(:,:,1) = Rnew;
RedImg(:,:,2) = Gnew;
RedImg(:,:,3) = Bnew;
% PLOTS
figure
blockPlots = subplot(1, 2, 1);
image(WhiteImg);
axis image
axis off
blockPlots = subplot(1, 2, 2);
image(RedImg);
axis image
axis off
To make them closer together:
figure
imshowpair(WhiteImg,RedImg,"montage")

Tags

Community Treasure Hunt

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

Start Hunting!