Creating a composite 2d image from multiple 2d arrays with a single colormap based on the intensity of each 2d array in each pixel

3 views (last 30 days)
I have multiple numeric arrays that have the same dimension. Each array I want to attribute a colormap that ranges from e.g. black/white to red or black/white to yellow. So in the end I have x arrays and x colormaps that I want to mix together into one single colormap where the colors in each pixel are mixed according to the intensity of each array (with their given color) and the color of that array.
I do not want to adjust opacity of the images/colormaps. Currently I am using a workaround that works for up to 3 colors by taking the values in each position of three arrays to create an RGB triplet. So the first array gives the intensity of red colors in that pixel from 0 to 1, second array for green and so on.
My question would be if there is a way to mix stacked colormaps in another way with more than 3 arrays as input and end up with a mixture of all defined colors based on the intensity in each pixel. I thought of maybe creating a colormap for each pixel along the third dimension but I am anything but an expert with this so I would be happy to have any feedback. This is the code I used so far to create an RGB image with 3 arrays. I attached also an example of what I want my output to look like. The cyan part corresponds to a mixin gof green and blue.
clear; close all
% Importing the data files. Input path of each file here
img1 = load('File1.txt'); %First map
img2 = load('File2.txt'); % Second map
img3 = load('File3.txt'); %Third map
%% Normalizing the values of the data to 0-1 to create a RGB triplet matrix
img1norm = (img1 - min(img1(:))) ./ (max(img1(:)) - min(img1(:))); %Red colors in plot
img2norm = (img2 - min(img2(:))) ./ (max(img2(:)) - min(img2(:))); % Green colors in plot
img3norm = (img3 - min(img3(:))) ./ (max(img3(:)) - min(img3(:))); % Blue colors in plot
figure;
rgbimage = cat(3, img1norm, img2norm, img3norm); %% Creating an RGB image from the normalized data (norm to 0 - 1)
imshow(rgbimage); %Displaying the created image
title('Composite RGB image of the three maps');

Accepted Answer

DGM
DGM on 22 Mar 2023
Edited: DGM on 22 Mar 2023
A given point in the image holds only three values. If you are superimposing more than three images, the color information must either be blended or discarded. In either case, information about the source images either becomes (potentially) inseparable or is lost.
Consider the example here:
That's blending four color-on-black images using various methods with their various compromises.
Or briefly:
A = imread('squig_bl.png');
B = imread('squig_or.png');
C = imread('squig_cy.png');
D = imread('squig_mg.png');
% stack the images for convenience
imstack = cat(4,A,B,C,D);
% averaging is muted by the black BG
outpict = uint8(mean(imstack,4));
imshow(outpict);
% a plain sum allows neutral response in black regions
% but will easily result in clipping
outpict = uint8(sum(imstack,4));
imshow(outpict);
% max() does not cause clipping, but discards color information
outpict = max(imstack,[],4);
imshow(outpict);

More Answers (0)

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!