Fusion of two images in matlab

774 Views Asked by At

Consider two images. The size of these two images could be anything. Bring size of those two images same. Develop an algorithm to mix these two images, such that alternate pixels are brought from two image courses. It is fusion of two images. For example, pixel 1 is image1’s, pixel 2 is from image 2, 3rd pixel from image 1 and so on like that……

2

There are 2 best solutions below

0
On

Here is a way to do it with MATLAB.

clear
clc

%// Initialize red and blueimages
RedImage = zeros(300,300,3,'uint8');
BlueImage = zeros(200,400,3,'uint8');

%// Color them
RedImage(:,:,1) = 255;
BlueImage(:,:,3) = 255;

figure('Color',[1 1 1]);

%// Show them
subplot(1,2,1)
imshow(RedImage)
subplot(1,2,2)
imshow(BlueImage)

It looks like this:

enter image description here

%// Resize them to same size
RedImage = imresize(RedImage,[256 256]);
BlueImage = imresize(BlueImage,[256 256]);

%// Initialize new image
NewImage = zeros(256,256,3,'uint8');

%// Assign alternate pixels to new images
NewImage(1:2:end,1:2:end,:) = RedImage(1:2:end,1:2:end,:);
NewImage(2:2:end,2:2:end,:) = BlueImage(2:2:end,2:2:end,:);

figure
imshow(NewImage)

Which outputs this:

enter image description here

It looks dark but resize the figure will show you that it works indeed!

Hope that helps! Have fun.

0
On

I know you prefer to use Matlab, but until someone gives you a Matlab answer, you may like to play around with ImageMagick which can do this for you and is in most Linux distributions anyway and available for free for Windows and Mac OSX.

First, let's create 2 images of different sizes and colours:

convert -size 300x300 xc:blue image1.png
convert -size 200x400 xc:red  image2.png

enter image description here enter image description here

Basically, you can resize images as you read them in by specifying the image size in square brackets after the filename, so I am arbitrarily choosing to resize both images to 256x256 pixels. Then I use the extremely powerful fx operator, so detect if I am processing an odd or an even numbered pixel, and choose either from the first or the second image accordingly:

convert image1.png[256x256] image2.png[256x256] -fx "i%2?u:v" out.png

enter image description here