How to merge red and blue images on white background with transparency

1 次查看(过去 30 天)
There are two images. Image 1 is some red signal on white background, image 2 is blue signal on white background.
Is there a way to "merge" the blue and red signal on white bagkcround (transparency or alpha may be involved)?
I know that if the signal is on black background, setting the two signals in R and B channel of RGB image will generate red, blue and some purple/pink gradient, but this cannot be used to merged on white background.
Thanks.

采纳的回答

John D'Errico
John D'Errico 2025-7-5
编辑:John D'Errico 2025-7-5
Easy enough, in a very lazy way too. If you kow how to do what you want with a black background, then make it so. And then back. Since the two images are purely red and blue, I don't even need to play with the respective channels.
im = imread('image.png');
Rim = im(:,1:287,:);
Bim = im(:,288:end,:);
RBim = 256 - ((256 - Rim) + (256 - Bim));
imshow(RBim)
if you wanted to make them a little less dark, so semi-transparent, this is easy too.
RBim2 = 256 - ((256 - Rim)/2 + (256 - Bim)/2);
imshow(RBim2)

更多回答(2 个)

Image Analyst
Image Analyst 2025-7-6
Does it look acceptable if you just average them?
weightR = 0.5; % Change as you want it.
weightB = 0.5;
dblImage = weightR * double(redImage) + weightB * double(blueImage)
combinedImage = uint8(dblImage);

Umar
Umar 2025-7-6

Hi @raym,

To merge the red and blue signals from the two images on a white background in MATLAB, we can utilize the concept of alpha blending. This technique allows us to control the transparency of each image, enabling a smooth combination of the two signals while preserving their original colors. Below is a step-by-step guide on how to achieve this. First, load the two images into MATLAB. Ensure that the paths to the images are correct.

   % Load the images
  redSignal = imread('/MATLAB Drive/2_red_density.tif');
  blueSignal = imread('/MATLAB Drive/2_blue_density.tif');

For accurate blending, convert the images to double precision. This step is crucial as it allows for more precise calculations during the merging process.

   % Convert images to double precision
   redSignal = im2double(redSignal);
   blueSignal = im2double(blueSignal);

To control the transparency of each signal, we will create an alpha mask. This mask will determine how much of each signal is visible in the final merged image.

   % Create an alpha mask (0 for fully transparent, 1 for fully opaque)
   alphaRed = 0.5; % Adjust this value for red signal transparency
   alphaBlue = 0.5; % Adjust this value for blue signal transparency

Create a new image to hold the merged result. The size of this image should match the dimensions of the input images.

   % Initialize the merged image
   mergedImage = zeros(size(redSignal));

Now, blend the two images using the alpha values defined earlier by using formula for merging the images, it will ensure that the colors are combined based on their respective alpha values.

   % Merge the images using the alpha values
   mergedImage = (alphaRed * redSignal) + (alphaBlue * blueSignal);

Finally, display the merged image to visualize the result.

   % Display the merged image
   imshow(mergedImage);
   title('Merged Red and Blue Signals on White Background');

Please see attached.

By following the steps outlined above, you can effectively merge red and blue signals on a white background in MATLAB. This method leverages alpha blending to maintain the integrity of the colors while allowing for a visually appealing combination. Feel free to adjust the alpha values and explore further enhancements to suit your specific needs.

Hope this helps.

  2 个评论
Walter Roberson
Walter Roberson 2025-7-6
If you only need to display the result, then you can do alpha blending as follows:
%define alpha factors
alphaRed = 0.5; % Adjust this value for red signal transparency
alphaBlue = 0.5; % Adjust this value for blue signal transparency
%read images
redSignal = imread('/MATLAB Drive/2_red_density.tif');
blueSignal = imread('/MATLAB Drive/2_blue_density.tif');
%just in case make the blue the same size as the red
blueSignal = imresize(blueSignal, [size(redSignal,1), size(redSignal,2)]);
%define background color. Both red and blue signals
%have alpha applied to them, so the background color will show
%through to some extent
ax = axes('color', 'k');
%display images with alpha blending
image(redSignal, 'alphadata', alphaRed);
hold on
image(blueSignal, 'alphadata', alphaBlue);
hold off

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by