Sum particular adjacent elements

5 次查看(过去 30 天)
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