clear all
close all
a=imread('images.jpg');
bw_a=im2bw(a,0.5);

row_size=size(bw_a,1);
col_size=size(bw_a,2);
tmp=ones(max(size(a)));
bw_tmp=im2bw(tmp,0.5);

for i = 1:row_size
    for j=1:col_size
        bw_tmp(i,j)=bw_a(i,j);
    end
end

resized_by_32=imresize(bw_tmp,[32,32]);
resized_by_16=imresize(bw_tmp,[16,16]);
resized_by_8=imresize(bw_tmp,[8,8]);
resized_by_4=imresize(bw_tmp,[4,4]);

inv_resized_by_32=~resized_by_32;
inv_resized_by_16=~resized_by_16;
inv_resized_by_8=~resized_by_8;
inv_resized_by_4=~resized_by_4;

% figure;
% subplot(1,5,1)
% imshow(bw_tmp)
% 
% subplot(1,5,2)
% imshow(resized_by_4);
% 
% subplot(1,5,3)
% imshow(resized_by_8);
% 
% subplot(1,5,4)
% imshow(resized_by_16);
% 
% subplot(1,5,5)
% imshow(resized_by_32);


figure;
subplot(2,5,1)
imshow(bw_tmp)

subplot(2,5,2)
imshow(inv_resized_by_4);

subplot(2,5,3)
imshow(inv_resized_by_8);

subplot(2,5,4)
imshow(inv_resized_by_16);

subplot(2,5,5)
imshow(inv_resized_by_32);

for i = 1:8
    for j=1:8
        if (inv_resized_by_16(2*i-1,2*j-1)==1)&&(inv_resized_by_16(2*i,2*j-1)==1)&&(inv_resized_by_16(2*i-1,2*j)==1)&&(inv_resized_by_16(2*i,2*j)==1)
            mask1(i,j)=1;
        else
            mask1(i,j)=0;
        end
    end
end

subplot(2,5,8)
imshow(mask1);

for i = 1:8
    for j=1:8
        if i==8|j==8
            mask2(i,j)=0;
        else
      
            if (inv_resized_by_16(2*i,2*j)==1)&&(inv_resized_by_16(2*i+1,2*j)==1)&&(inv_resized_by_16(2*i,2*j+1)==1)&&(inv_resized_by_16(2*i+1,2*j+1)==1)
                mask2(i,j)=1;
            else
                mask2(i,j)=0;
            end
        end
    end
end
subplot(2,5,9)
imshow(mask2);

mask3=zeros(16);
for i = 1:8
    for j = 1:8
        if i==1&&j==1
            mask3(2*i-1,2*j-1)=mask1(i,j);
            mask3(2*i-1,2*j)=mask1(i,j);
            mask3(2*i,2*j-1)=mask1(i,j);
            mask3(2*i,2*j)=mask1(i,j)|mask2(i,j);
        elseif i==1
                mask3(2*i-1,2*j-1)=mask1(i,j);
                mask3(2*i-1,2*j)=mask1(i,j);
                mask3(2*i,2*j-1)=mask1(i,j)|mask2(i,j-1);
                mask3(2*i,2*j)=mask1(i,j)|mask2(i,j);
               
            
        elseif j==1
                mask3(2*i-1,2*j-1)=mask1(i,j);
                mask3(2*i-1,2*j)=mask1(i,j)|mask2(i-1,j);
                mask3(2*i,2*j-1)=mask1(i,j);
                mask3(2*i,2*j)=mask1(i,j)|mask2(i,j);
               
            
        
                
        else
            mask3(2*i-1,2*j-1)=mask1(i,j)|mask2(i-1,j-1);
            mask3(2*i-1,2*j)=mask1(i,j)|mask2(i-1,j);
            mask3(2*i,2*j-1)=mask1(i,j)|mask2(i,j-1);
            mask3(2*i,2*j)=mask1(i,j)|mask2(i,j);
        end
    end
end
subplot(2,5,10)
imshow(mask3);
            