Evasmith95 asked . 2020-04-22

How do I superimpose images in MATLAB?

I would like to know how to superimpose images in MATLAB.

image processing, matlab,

Expert Answer

John Williams answered . 2024-11-23 03:07:33

There are two basic ways to superimpose images in MATLAB. One involves using transparency for overlaying images objects that may not be exact rectangles, and the other involves indexing into the image data to replace pixels.
Using transparency allows you to achieve the effect of partly seeing through a pixel, rather than completely hiding the background. Indexing allows you to effectively create a new image object that is a combination of pixels from multiple image objects.
-----------------------------------------
1. Using transparency to superimpose images:
Suppose you have an image with pixels that are transparent or partially transparent, and you have another image that should serve as the background. You want the first image to let the background show through wherever there is transparency.
Note that, when using this method, you are actually dealing with multiple image objects. The superimposing is achieved by overlapping the image objects. Additionally, when using semitransparent pixels, you can achieve the effect of partially seeing through a pixel rather than completely hiding the background.
Here is an example that illustrates this method. Over a background image, you will overlay another image at a location within the background. If you are simply trying to place images within other images without using transparency, then you can ignore the code that sets the 'AlphaData' property of the image objects:
% Create the background
%   This example uses a blend of colors from left to right, converted to a TrueColor image
%   Use repmat to replicate the pattern in the matrix
%   Use the "jet" colormap to specify the color space
bg = ind2rgb(repmat(1:64,64,1),jet(64));

% Create an image and its corresponding transparency data
%   This example uses a random set of pixels to create a TrueColor image
im = rand(100,100,3);
% Make the image fade in from left to right by designing its alphadata
%   Use repmat to replicate the pattern in the transparency fading
imAlphaData = repmat(0:1/size(im,2):1-1/size(im,2),size(im,1),1);

% Display the images created in subplots
hf = figure('units','normalized','position',[.2 .2 .6 .6]);
ax1 = subplot(2,3,1);
ibg = image(bg);
axis off
title('Background')
ax2 = subplot(2,3,4);
iim = image(im);
axis off
title('Image without transparency yet')

%   Now set up axes that overlay the background with the image
%   Notice how the image is resized from specifying the spatial 
%   coordinates to locate it in the axes.
ax3 = subplot(2,3,[2:3, 5:6]);
ibg2 = image(bg);
axis off
hold on
%   Overlay the image, and set the transparency previously calculated
iim2 = image(im,'XData',[30 50],'YData',[10 30]);
set(iim2,'AlphaData',imAlphaData);
title(sprintf('Using transparency while overlaying images:\nresult is multiple image objects.'))

If you want to extract a circle from the image, and overlay just that (NOTE: This part of the example requires the Image Processing Toolbox function ROIPOLY to create the circle mask.):

%   Add another image to the background, but use the 'AlphaData' property
%   to just overlay a circle instead.  
%
%   First define the polygon region of interest
t = 0:.1:2*pi;
[imheight,imwidth] = size(im(:,:,1));
x = round(20*cos(t)+round(imwidth/2));
y = round(20*sin(t)+round(imheight/2));
%   Extract the polygon mask
imCircleAlphaData = roipoly(im,x,y);
axes(ax3);
%   Overlay the image, and set the transparency previously calculated
iim3 = image(im,'XData',[0 30],'YData',[30 60]);
set(iim3,'AlphaData',double(imCircleAlphaData));

2. Using indexing to superimpose images: Use this method if you have an image that you want to place in a scene, but prefer to have only a single image as a resultant. The following example achieves this by replacing the scene pixels with the object image pixels.

% Create the scene
%   This example uses a random set of pixels to create a TrueColor image
scene = rand(100,100,3);

% Create the object image
%   This example uses a blend of colors from left to right, converted to a TrueColor image
%   Use repmat to replicate the pattern in the matrix
%   Use the "jet" colormap to specify the color space
obj = ind2rgb(repmat(1:64,64,1),jet(64));

% Display the images created in subplots
hf2 = figure('units','normalized','position',[.2 .2 .6 .6]);
axi1 = subplot(2,3,1);
iscene = image(scene);
axis off
title('Scene')
axi2 = subplot(2,3,4);
iobj = image(obj);
axis off
title('Object image')

% Now replace pixels in the scene with the object image
result = scene;
%   Define where you want to place the object image in the scene
rowshift = 20;
colshift = 0;
%   Perform the actual indexing to replace the scene's pixels with the object
result((1:size(obj,1))+rowshift, (1:size(obj,2))+colshift, :) = obj;
%   Display the results
ax3 = subplot(2,3,[2:3, 5:6]);
iresult = image(result);
axis off
hold on
title(sprintf('Using indexing to overlay images:\nresult is one image object'))

If you want to extract a circle from the object, and overlay just that: (AGAIN, NOTE: This part of the example requires the Image Processing Toolbox function ROIPOLY to create the circle mask.):

%   Let's add another image to the scene.  
%   First define the polygon region of interest
t = 0:.1:2*pi;
[objheight,objwidth] = size(obj(:,:,1));
x = round(20*cos(t)+round(objwidth/2));
y = round(20*sin(t)+round(objheight/2));
%   Since the mask is 2D, repeat it through the 3D RGB matrix
%   while extracting the polygon mask
objCircleMask = repmat(roipoly(obj,x,y),[1,1,3]);
%   Define where we want to place the circle object in the scene
rowshift = 0;
colshift = 30;
result2 = result;
%   Perform the actual indexing.  First calculate the replacement pixels
%   by multiplying the scene by the negative of the mask, and then adding 
%   the object multiplied by the mask.  Multiplying the mask chooses all 
%   pixels corresponding to a 1 in the mask, and removes color from the 
%   pixels corresponding to a 0.
replacementPixels = ( result((1:size(obj,1))+rowshift, (1:size(obj,2))+colshift, :) ...
     .* double(~objCircleMask) ) + ( obj .* double(objCircleMask) );
%   Now replace the pixels with the ones we just calculated
result2((1:size(obj,1))+rowshift, (1:size(obj,2))+colshift, :) = replacementPixels;
%   Display the results.  Notice that we placed the circle object in the scene
%   that resulted from the previous object placement.
axes(ax3);
delete(iresult)
iresult2 = image(result2);


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.