Reputation:
I am trying to take an image and output a larger image whose height and width are twice as much as those of the input image, by duplicating the rows and columns.
I have implemented this with the code below; however, the output is an entirely black image.
img = imread('https://people.sc.fsu.edu/~jburkardt/data/png/lena.png');
[M1,N1] = size(img);
M2 = M1*2;
N2 = N1*2;
g = zeros(M2,N2);
imshow(g);
The program should generate a larger image where the height and width are twice as much as those of the input image.
Upvotes: 1
Views: 377
Reputation: 19689
To resize the image and duplicate the pixels, use imresize
with the nearest neighbour interpolation method.
g = imresize(img,2,'nearest'); %resizing to twice the size of the original image
Now you can see:
>> size(img)
ans =
512 512
>> size(g)
ans =
1024 1024
You didn't duplicate any row/column in your code. Rather you initialised a zero matrix of twice the size of img
. A matrix of all zeros is nothing but a black image which is what you're getting.
Upvotes: 1
Reputation: 15867
If I don't find an efficient repelem
I may use kron
:
g = kron(img , ones(2));
Upvotes: 2
Reputation: 104545
Another suggestion I have if you somehow don't have access to repelem
is to create a meshgrid
of coordinates that introduces 0.5 coordinates in each dimension, remove the decimals with floor
then index into the image. You'll have to convert the coordinates into linear indices via sub2ind
before indexing.
In other words:
img = imread('https://people.sc.fsu.edu/~jburkardt/data/png/lena.png');
[M1,N1] = size(img);
% Create grid of coordinates at twice the frequency
[X,Y] = meshgrid(1:0.5:N1+0.5, 1:0.5:M1+0.5);
% Remove decimal precision
X = floor(X);
Y = floor(Y);
% Convert to linear indices and sample
ind = sub2ind([M1, N1], Y, X);
g = img(ind);
Upvotes: 2
Reputation: 626
While @sardar-usama 's answer is likely what you want to do in practice, strictly speaking it doesn't duplicate the rows/columns as the question requested. Rather, it interpolates the image to the new size (though, imresize includes options on how to perform the interpolation).
If you actually wanted to duplicate the rows and columns, you can do:
g = repelem(img,2,2);
Upvotes: 7