To concatenate two images

23 vues (au cours des 30 derniers jours)
Hajira raut
Hajira raut le 29 Nov 2018
  1. I want to arrange two images into one image with the first image on the left side and the other image on the right side.I want to know what the MATLAB commands are because I have tried cat(2,A,B) command to concatenate the two images but its not working.
This is my command:
f1=imread('PEDESTRIAN.jpg'); %reads the images
f2=imread('NO_PARKING.jpg');
f3=rgb2gray(f1); %converts the images into grayscale images
f4=rgb2gray(f2);
subplot(2,2,1),imshow(f3) %plots both the images in one figure
subplot(2,2,2),imshow(f4)
combImg=cat(f1,f2);
imshow(combImg)
But this doesnt give me any output. Please help.

Réponses (1)

Guillaume
Guillaume le 29 Nov 2018
"but its not working" is a useless statement without telling us why it's not working (you get an error, if so, what error? you don't get what you expected, if so, what did you expect and what did you actually get?).
You can use cat(2, A, B), which is the same as [A, B] only if the images are the same height. Otherwise, you'll get an error. Note that in your code you forgot the 2, so that's definitively going to cause an error.
The most reliable way of composing two images is with imfuse:
combImg = imfuse(f1, f2, 'montage');
imshow(combImg);
Note that if the composition is only for display, you can call imshowpair instead of imfuse and imshow:
imshowpair(f1, f2, 'montage');
imshowpair and imfuse will both take care of padding the images if one is smaller than the other.

Community Treasure Hunt

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

Start Hunting!

Translated by