how to fuse two png images with equal tranparency and with proper positioning in the final image?
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Saurav Parmar
el 14 de Sept. de 2024
Hello there,
I need to mix two pictures in a way so that one is placed over the other. But both should have equal transparency. Also the location of the first picture over the second should be determinable. is there any way we can do this using imfuse or any other function in MATLAB? whenevr I use this script one gets faded and is located in one of the corners which are not desirable. Can we fix this?
0 comentarios
Respuesta aceptada
Ayush
el 14 de Sept. de 2024
Hi Saurav,
To blend two images, start by adjusting their transparency using alpha blending to control how see-through each image is. Next, carefully position one image over the other by using indexing to get the placement just right. Finally, combine the images using element-wise operations to create a seamless blend.
Here is the sample code for the same:
% Read the images
img1 = imread('image1.png');
img2 = imread('image2.png');
% Resize images if necessary (optional)
[rows, cols, ~] = size(img1);
img2 = imresize(img2, [rows, cols]);
% Ensure both images are of the same size - I realized this is not needed
% with your images.
% assert(isequal(size(img1), size(img2)), 'Images must be of the same size.');
% Convert images to double precision for blending
img1 = im2double(img1);
img2 = im2double(img2);
% Set transparency level
alpha = 0.5;
% Create an empty canvas for the blended image
blendedImage = img1; % Start with img1 as the base
% Determine the position for the second image over the first
% Example: Place img2 at (rowOffset, colOffset) over img1
rowOffset = 0; % Change this value to move img2 vertically
colOffset = 300; % Change this value to move img2 horizontally
% Ensure the offset does not cause out-of-bound indexing
[rowMax, colMax, ~] = size(img1);
rowRange = rowOffset+1:min(rowOffset+size(img2,1), rowMax);
colRange = colOffset+1:min(colOffset+size(img2,2), colMax);
% Blend the images with the specified transparency
blendedImage(rowRange, colRange, :) = ...
alpha * img2(1:length(rowRange), 1:length(colRange), :) + ...
alpha * img1(rowRange, colRange, :);
% Display the result
imshow(blendedImage);
An alpha value of 0 makes the image fully transparent, while a value of 1 makes it fully opaque. This technique allows for smooth transitions and overlays of images, creating effects such as transparency and fading.
You may read more about alpha blending from there: https://www.mathworks.com/help/matlab/ref/alpha.html
I hope this was helpful!
2 comentarios
DGM
el 15 de Sept. de 2024
Editada: DGM
el 15 de Sept. de 2024
It depends what you mean by image "processing" or "editing". A lot of the tools in the Image Processing Toolbox are tailored for technical purposes, whereas something like this is more visual -- akin to something one might do in Photoshop.
I would call this image "composition", or "compositing" (the simple linear combination of images over a specified region). I've posted a lot of examples on blending and composition, if that's what you're after. I've posted a toolbox on the File Exchange to satisfy more of the "photoshop" motives than what IPT offers. Webdocs for that are here.
FWIW, since alpha is 0.5, this problem probably could be done with imfuse() and the spatial referencing tools, but I agree with Ayush that a direct approach is probably more inutitive and instructive.
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!