Combine two images in MATLAB


MATLAB allows us to combine two images through various methods like image fusion, image join, image concatenate, image overlay, image blending, etc. In this tutorial, we will explore all these techniques of combining two images.

Combine Two Image in MATLAB using `imtile()` Function

In MATLAB, we can combine two images by using the `imtile()` function. This function allows us to arrange our multiple images in a tiled layout.

Syntax

To combine two images, the `imtile` function takes the following syntax:

img = imtile({img1, img2}, 'GridSize', [1, 2]);

Now, let us see the implementation of the `imtile` function through a MATLAB program.

Example

% MATLAB program to combine two images using `imtile` function
% Read the two input images
img1 = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425340.jpg');
img2 = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');
% Call `imtile` function to combine two images in tiled layout
img = imtile({img1, img2}, 'GridSize', [1, 2]);
% Display the combined images
imshow(img); title('Combined Images');
    

Output

Explanation

In this MATLAB program, we start by reading two images that we want to combine. For this, we use the `imread` function and store the two input images in variables `img1` and `img2`. Then, we use the `imtile` function to combine the images in a tiled layout. In this case, we have used [1, 2] which creates a horizontal tiled layout. If we want to create a vertical layout, then we will use [2, 1]. Finally, we display the combined images using the `imshow` function with a proper title.

Combine Two Images in MATLAB using `cat()` Function

In MATLAB, we can use the `cat()` function to concatenate two images. The `cat()` function allows use to combine two images either horizontally or vertically without tiling.

Syntax

img = cat(dim, img1, img2);    

Here, `img1` and `img2` are the two images that we want to combine. The parameter `dim` defines the dimension to operate along, i.e. for vertical direction its value is `1` and for horizontal direction, it is `2`.

The following MATLAB program demonstrate the implementation of `cat()` function to combine two images horizontally and vertically.

Example

% MATLAB program to combine two images using `cat` function
% Read the two input images
img1 = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425308.jpg');
img2 = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');
% Resize the img2 to match the size of img1
img2 = imresize(img2, size(img1, [1, 2]));
% Concatenate the images horizontally
outimg1 = cat(2, img1, img2);
% Concatenate the images Vertically
outimg2 = cat(1, img1, img2);
% Display the combined images
subplot(1, 2, 1); imshow(outimage1); title('Horizontally Combined Images');
subplot(1, 2, 2); imshow(outimage2); title('Vertically Combined Images');   

Output

Explanation

In this MATLAB program, we first read the two input images using the `imread()` function. Then, we resize the second image `img2` to match the size of the first image `img1` by using the `imresize` function. Next, we use the `cat` function to combine the two images horizontally and vertically and store the outputs in `outimg1` and `outimg2` variables. Finally, we use the `imshow` function to display the images stored in `outimg1` and `outimg2` with proper titles.

Combine Two Images in MATLAB using `imfuse` Function

MATLAB provides another built-in function named `imfuse` to combine two images based on different fusion methods.

Syntax

To combine two images, the `imfuse` function takes the following syntax,

img = imfuse(img1, img2, 'FusionMode', 'options');

Here, the `img1` and `img2` are the two images that we want to combine, `FusionMode` specifies the fusion method that we want to use to combine the images.

Let us now understand the implementation of `imfuse()` function to combine two images by using different fusion techniques.

Example

% MATLAB program to demonstrate the use of infuse function to combine two images
% Read the two input images
img1 = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425308.jpg');
img2 = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');
% Resize img2 to match the size of img1
img2 = imresize(img2, size(img1, [1, 2]));
% Create a false-color composite image
outimg1 = imfuse(img1, img2, 'falsecolor');
% Create a composite image to highlight areas of dissimilarity between the images
outimg2 = imfuse(img1, img2, 'diff');
% Create a blended image
outimg3 = imfuse(img1, img2, 'blend');
% Display the combined images
subplot(1, 3, 1); imshow(outimg1); title('false-color image');
subplot(1, 3, 2); imshow(outimg2); title('difference image');
subplot(1, 3, 3); imshow(outimg3); title('blend image');   

Output

Explanation

In this MATLAB program, we use the `imread` function to read the two input images and store in `img1` and `img2` variables. Next, we match the size of the `img2` with `img1`. After that we call the `imfuse` function with different parameters, such as `faslecolor`, `diff`, and `blend` to combine two images with different fusion techniques. Finally, we use the `imshow` function to display the three output images with proper titles.

Conclusion

In conclusion, MATLAB provides various built-in functions and techniques to combine images. In the above sections of this article, we have explained all those techniques to combine two images together with the help of MATLAB programs.

Updated on: 18-Jul-2023

255 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements