Shrinking image by averaging
42 views (last 30 days)
I want to create a function: shrinkbyaveraging(imagematrix) that returns an image that is half as large in both height and width. Each pixel in the returned image should be the average of 4 pixels from the input image. Shrinkbyaveraging only needs to work for images having an even number of rows/columns. However, I do not want to use imresize.
function shrink = shrinkbyaveraging(imagematrix) [row,colm] = size(imagematrix); x = row/2; y = colm/2;
but im not sure what the question is asking since i cannot use imresize. :/
Michael Hawks on 17 Oct 2018
Nrow = 12; Ncol = 15;
input = rand(Nrow,Ncol);
shifts = [0 0; 0 1; 1 0; 0 -1];
A = zeros(Nrow,Ncol,4);
for ii = 1:4;
out(:,:,ii) = circshift( input, shifts(ii,:) );
out = sum( out, 3);
out = out(2:2:end,2:2:end);
In the for loop, we're building up an mxnx4 array of shifted copies of the input array. Then finding the mean over the neighborhood is as easy as averaging along dimension 3. Then the last line downsamples the array to the size you want.