Reputation: 11
When I display my reconstructed images, they are just white. Is there something obviously wrong with my program?
The reconstructed images should have the values of the downsampled image at one pixel in the upsampled 2x2
pixel block. The interpolation method I'm using here is simply taking the value from one row above and filling the next row with it, repeating this process for the columns.
%% Image Resampling
close all; clear all; clc;
s_dir=pwd;
cd Images;
I=imread('aivazovsky78g.tif','tif');
cd(s_dir)
[N M]=size(I);
figure;
imshow(I)
axis image; hold on;
for k=1:4
pause(1)
I=I(1:2:N, 1:2:M);
[N M]=size(I);
image(I)
end
%% Image Reconstruction
Irec=zeros(2*size(I));
for r=1:5
for n=1:N-1
for m=1:M-1
Irec(2*n-1,2*m-1)=I(n,m);
end
end
[N M]=size(Irec);
for n=2:2:N
for m=2:2:M
Irec(n,:)=Irec(n-1,:);
Irec(:,m)=Irec(:,m-1);
end
end
I=Irec;
figure;
imshow(I)
end
Upvotes: 1
Views: 1896
Reputation: 124563
Not the most efficient way, but here is a working code:
% 256x256 grayscale image
I = imread('cameraman.tif');
% double in size
I2 = zeros(2*size(I),class(I));
for i=1:2:size(I2,1)
for j=1:2:size(I2,2)
I2([i i+1],[j j+1]) = I((i-1)/2 + 1, (j-1)/2 + 1);
end
end
% compare against @Magla's solution
I3 = imresize(I,2,'box');
isequal(I2,I3)
Upvotes: 0
Reputation: 7751
You may use B = imresize(A, scale, 'box')
where a scale
of 2 doubles the amount of pixels in x
and y
. The z
dimension will still have the same value.
The resizing method box
will copy the initial pixel value (i, j)
to its 3 new neighbors (i+1, j)
, (i, j+1)
, and (i+1, j+1)
- the same method you programmed.
Upvotes: 0