How can you apply a 3x3 local averaging mask to enhance the sharpness in an image. How can you set the mask? I'd like to get a better understanding in how this method of filtering an image works and how to compute the algorithm.

 채택된 답변

Walter Roberson
Walter Roberson 2018년 2월 26일

2 개 추천

Local averaging:
conv(double(YourImage), ones(3,3)/9, 'same')
ones(3,3)/9 is not usually the mask used for sharpening. You tend to sharpen horizontally and vertically separately, using a mask such as [0, 1, 0; -1 0 -1; 0 1 0]

댓글 수: 6

Diego Espinosa
Diego Espinosa 2018년 2월 26일
Can you explain what you are doing with this line of code?
Walter Roberson
Walter Roberson 2018년 2월 26일
Oops should be conv2 instead of conv.
Using each pixel as the upper left corner temporarily, lay down the ones(3,3)/9 and multiply that by the pixels overlayed and add those all together. So in other words, total a 3 x 3 area and divide by 9, the number being totalled, so thereby calculating the average there. Store that. Now move the mask one pixel to the right and do the same and so calculate the local average relative to that position, store, move on...
It is a 3 by 3 moving average, effectively.
Diego Espinosa
Diego Espinosa 2018년 2월 27일
Is there another way to apply a local mask without using the convolution function; I'd like to know how to do this manually by iterating though the an image's array or matrix?
Walter Roberson
Walter Roberson 2018년 2월 27일
Yes you can use loops. You could also use blockproc with a block size of 1 1 and a border size of 1 1, in which case you should be careful about the setting for trimming the border (see blockproc)
Thulitha Theekshana
Thulitha Theekshana 2019년 8월 14일
Remember to convert the output of conv2 to uint range. ( maybe using a function such as mat2gray ) before imshow. Otherwise the image wouldn't be displayed.
Or you can use bracket bracket and leave it as floating point:
filteredImage = conv(double(YourImage), ones(3,3)/9, 'same')
imshow(filteredImage, []);

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

추가 답변 (1개)

Image Analyst
Image Analyst 2018년 2월 27일
편집: Image Analyst 2019년 8월 14일

0 개 추천

You can use imfilter() instead of convolution, but it's doing pretty much a similar thing (multiplying a sliding window by the image under the window). To sharpen edges you'd use a kernel that's 17 in the middle and -1 around the sides:
kernel = -1 * ones(3);
kernel(3,3) = 17;
output = conv2(double(intputImage), kernel, 'same');
imshow(output, []);
See my attached manual convolution, though I don't recommend it. It's mainly for students who are not allowed to use built-in functions.
The theory for the -1, 17 thing is that it's the average gradient around the 8 directions (which gives a flat, harsh, Laplacian edge detection image), plus 9 in the center to add back in the original image so that the result looks like a edge enhanced image rather than a pure edge detection image. If you need more explanation on the theory, ask.

카테고리

도움말 센터File Exchange에서 Image Filtering and Enhancement에 대해 자세히 알아보기

질문:

2018년 2월 26일

편집:

2019년 8월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by