Having one for loop instead of two

2 views (last 30 days)
Junaid Pirzada
Junaid Pirzada on 23 Nov 2015
Commented: Guillaume on 23 Nov 2015
I am trying to hardcode the upsampling of an image. The problem I am having is trying to do it in a loop. It seems impossible.Here is what I did.
Image = imread('grayscale.jpg');
%imshow(Image);
% Downsampling
[row, col] = size(Image);
Downimage = Image(1:2:row,1:2:col);
%imshow(Downimage);
%Upsampling
newrow = row*2;
newcol = col*2;
Upimage = zeros(newrow,newcol);
for r = 1:2:newrow
for c = 1:2:newcol
Upimage(r,c) = Downimage; %The Downimage should also have different parameters but I cant just use r and c since their length are not the same
Upimage(r+1,c+1) = Downimage;
end
end
%Upimage = reshape([Downimage ; Downimage],1,[]);
%Upimage = repmat(Downimage, 2, 1);
imshow(Upimage);
Now I realize that for the loop to work it needs parameters beside the Downimage in the loop, but I can's seem to make it work due to the downimage being of the shorter size then the upimage. P.S I cant use imresize...I have to hardcode it

Answers (1)

Guillaume
Guillaume on 23 Nov 2015
I'm not sure I understand your problem:
Upimage(r, c) = Downimage(ceil(r/2), ceil(c/2));
Upimage(r+1, c) = Downimage(ceil(r/2), ceil(c/2)); %or = Upimage(r, c)
%same for (r, c+1)
%same for (r+1, c+1)
would work. The four lines can simply be written as
Upimage(r:r+1, c:c+1) = Downimage(ceil(r/2), ceil(c/2));
  1 Comment
Guillaume
Guillaume on 23 Nov 2015
Note that I would have had the loop going over the pixels of the DownImage rather than the UpImage. It makes for a simpler code:
for c = 1:col %note that it is also faster to iterate over columns in the outer loop
for r = 1:row
UpImage(2*r-1:2*r, 2*c-1:2*c) = DownImage(r,c);
end
end

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!