I need to create a 3 by 3 window and calculate mean within the window. This window is to move through the image. How can I do it?

조회 수: 1 (최근 30일)
I am new to MATLAB. This is what I have done. I got this from here and there online. Is there an efficient way of doin it?
Thank you
for i = side:p
for j = side:q
nhood_ctr = [i j];
nhood = im3((nhood_ctr(1)-floor(side/2)):(nhood_ctr(1)+floor(side/2)),(nhood_ctr(2)-floor(side/2)):(nhood_ctr(2)+floor(side/2)));
%size(nhood)
avg(i,j) = mean(mean(nhood));
sum = 0;
for k = (nhood_ctr(1)-floor(side/2)):(nhood_ctr(1)+floor(side/2))%i-2:i+2
for l = (nhood_ctr(2)-floor(side/2)):(nhood_ctr(2)+floor(side/2))%j-2:j+2
x = im3(k,l)- avg(i,j);
sum = sum + x*x;
end
end
igvm(i,j) = sum;
end
end

답변 (1개)

Joseph Cheng
Joseph Cheng 2017년 4월 6일
I would take a look at the conv() and conv2() function. you would use something like [1 1 1;1 1 1;1 1 1]/9 to get mean. and then probably select 'same' or valid depending on how you want to treat the edges of the image
  댓글 수: 3
Sibin Prasad
Sibin Prasad 2017년 4월 9일
Thank you Joseph Cheng. I think your answer might have worked. But I need do to the following (below image). So can you tell me how to the step 2. I think the idea you have provided me is enough for step 1. How can I do step 2 also. Thank You.
Mohammed Abdul Wadood
Mohammed Abdul Wadood 2022년 5월 13일
I have question please @Image Analyst , your answer above
kernel = [1 1 1;1 1 1;1 1 1]/9 ;
averageImage = conv2(double(grayImage), kernel, 'same');
imshow(averageImage, []);
it is for calculate the average within the window, but what if I want to calculate euclidean distance insted of average, how can i solve this?
another quastion please, why you use (double) in
averageImage = conv2(double(grayImage), kernel, 'same');
if I split the image to (R, G & B) channel which it is gray channels, can I avoid (double)?
and the last question, what the purpose from ( [ ] ) in (imshow) step
imshow(averageImage, []);
sorry for annoyance and thank you so much

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by