MATLAB: Create new image using the MSB for existing images

create image from 2 existing images

How can i create a new image such that 4 MSB bits of the 1st image becomes its MSB 4 bits while 4 MSB bits of the 2nd image becomes its LSB 4 bits as shown in the figure below??once using grayscale images and another using RGB pictures ?

Best Answer

filename1 = 'cameraman.tif';
filename2 = 'peppers.png';
%make sure they are grayscale
img1 = imread(filename1);
if ndims(img1) > 2; img1 = rgb2gray(img1); end
img2 = imread(filename2);
if ndims(img2) > 2; img2 = rgb2gray(img2); end
%make sizes compatible
sz1 = size(img1);
sz2 = size(img2);
sz = min(sz1, sz2);
img1 = img1(1:sz(1), 1:sz(2));
img2 = img2(1:sz(1), 1:sz(2));
%splice
img3 = uint8(floor(double(img1)/16)*16 + double(mod(img2,16)));
%show
imshow(img3)