springles
springles

Reputation: 13

Downsampling an image issue

So I am trying to downsample an image using nested for loops. Here, i have a 359x479(widthxheight) image. I am trying to downsample it to a 180x240 image by removing the even rows and columns. Yet, it doesn't seem to be working. I end up getting the same image as the output.

a=imread('w.jpg'); %input image
a=im2double(a); %convert it to double
r=[[1 1 1];[1 1 1];[1 1 1]]/9;  % the next 3 steps done to low pass 
filter the image
c=imfilter(a,r,'replicate');
imshow(c);
for i=1:359 % for rows
    for j=1:479 %for columns
        if(mod(i,2)~=0) %to remove even rows
            if(mod(j,2)~=0) %to remove odd columns
               b(i,j)=c(i,j);  %if i and j are odd, the pixel value is assigned to b
            end
        end
    end
end 
figure, imshow(b);

should get a 180x240 image but still getting the same image of size 359x479

Upvotes: 0

Views: 92

Answers (1)

obchardon
obchardon

Reputation: 10792

You also need to assign only one pixel on two ! If you do not, half of yours columns/rows will contain only 0 value.

so you need to use:

b(ceil(i/2),ceil(j/2))=c(i,j);

where 2 correspond to the value of your modulo.

You could also avoid to use some loops by simply writing:

b = c(1:2:259,1:2:194);

Upvotes: 1

Related Questions