Sum particular adjacent elements

7 views (last 30 days)
ASC
ASC on 18 Jul 2020
Commented: ASC on 19 Jul 2020
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.

Accepted Answer

Image Analyst
Image Analyst on 18 Jul 2020
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.

More Answers (0)

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!