Sum particular adjacent elements

조회 수: 6 (최근 30일)
ASC
ASC 2020년 7월 18일
댓글: ASC 2020년 7월 19일
I learned about conv2 from the the post:
My original goal was to sum all the adjacent elements in a matrix. The solution using conv2, from the post above, with the mask:
msk_0 = [1 1 1; 1 0 1; 1 1 1]
worked well.
Using conv2, with a similar logic, now I want to sum the elements above and adjacent to a given element. In this case my mask becomes:
msk_1 = [1 1 1; 0 0 0; 0 0 0] %top
I want to run this on a matrix f, indentical to the mask.
f = [1 1 1; 0 0 0; 0 0 0]
The answer produced is 0
>> conv2(f, msk_1, 'same')
ans =
0 0 0
0 0 0
0 0 0
My expectation was that the mask would result in the sum of the elements above and adjacent to a given element in f. The result in this case would be:
0 0 0
2 3 2
0 0 0
What is it I am missing about the functionality of conv2?
How do I achieve my goal of summing only the specific elements in a given mask?
Thanks for your input.

채택된 답변

Image Analyst
Image Analyst 2020년 7월 18일
No, you don't understand what convolution is. With convolution, the kernel gets flipped top-to-bottom, and left-to-right. There are mathematical reasons for this based on linear systems theory. So if you want the sum of the above row, your kernel would have to be
msk_1 = [0 0 0; 0 0 0; 1 1 1] %top
Or you can use imfilter() and not flip the mask because imfilter() does not flip the mask.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by