Shrinking image by averaging

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. :/

댓글 수: 4

KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 10월 17일
What you have done so far, please share?
Image Analyst
Image Analyst 2018년 10월 17일
If you do not want to use imresize(), then I guess you do not want to use interp2() either, and do not want to use anyone else's function or code. So, how can we help? Point you to Wikipedia's page on bilinear interpolation??? And to this page on how to format your code?
Rik
Rik 2020년 11월 20일
편집: John Kelly 2021년 1월 15일
Rik
Rik 2020년 11월 20일
편집: John Kelly 2021년 1월 15일
Also some deleted comments :
OP responding to Kalyan:
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. :/
OP responding to Image Analyst:
I am taking a first year introductory computing science course and I dont think it is that hard. But, I am stuck on it.

댓글을 달려면 로그인하십시오.

답변 (2개)

Michael Hawks
Michael Hawks 2018년 10월 17일

1 개 추천

Try this:
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,:) );
end
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.
Sean de Wolski
Sean de Wolski 2021년 1월 15일

0 개 추천

This works
img = repmat(1:6,4,1)
shrink = @(x)reshape(sum(reshape(sum(reshape(x,2,1,[])),size(x,1)/2,2,[]),2)./4,size(x,1)/2,[])
shrink(img)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2018년 10월 17일

답변:

2021년 1월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by