Using Vectorization to Calculate New Values Based on Surrounding Values
Show older comments
Pardon any mistakes, I'm going through a massive panic attack at the moment with the stress of trying to solve this the last 4+ hours.
I have a project for my programming class that wants me to write a bunch of functions to assist an edge finding function. It has to all be vectorized, ie no iteration or selection. And I'm failing to vectorize the very first function.
The task in question:

The idea is that all the values in a matrix are given a weighted average based on given matrix W. I've already successfully implemented this code with iteration here:
W = [10/222 27/222 10/222;
27/222 74/222 27/222;
10/222 27/222 10/222];
for i=2:size(bw_im,1)-1
for j=2:size(bw_im,2)-1
A=bw_im(i-1:i+1, j-1:j+1);
bw_im(i,j)=sum(sum(A.*W));
end
end
bw_smooth=bw_im;
This successfully outputs the values the full instructions ask for. However, I've been unable to properly vectorize it. The best I've gotten is cutting out the iteration with this
bw_im(2:size(bw_im,1)-1,2:size(bw_im,2)-1)=
Which means it'll only overwrite the non-edge values. Other than that, I'm completely stumped. Please help, I'm panicking at the moment as there is still so much more to do.
Answers (1)
Image Analyst
on 24 Mar 2018
Just use conv2() with the valid option and then paste it onto a copy of the original. It's 3 lines of code. Hint:
conv2Image = conv2(A, w, 'valid');
result = A; % Initialize
result(2:............) = conv2Image; % Paste
I'll let you figure out the indexes in the third line to complete it. Should take you less than a minute, not 4 hours, once you realize what they are describing is convolution (actually correlation but with a symmetrical kernel correlation is the same as convolution).
If you can't use conv2() and need to do it yourself, I attach a very inefficient manual way of doing the scanning with a 4-nested for loop, though it's not recommended. I recommend using conv2 because it's highly efficient and optimized.
9 Comments
Image Analyst
on 24 Mar 2018
Convolution and correlation are basically scanning an array with a smaller array (called "kernel), multiplying the values of the bigger matrix underneath the smaller one by the values of the smaller, moving window and summing together. The sum goes into an output image at a location that is the center of the window. Then just slide the kernel window along until it covers the entire large matrix. Convolution has the kernel window flipped vertically and horizontally whereas correlation uses the kernel window as is (no flipping).
If you can't use the built-in function, then use my attached code where I do it "manually".
Sam Ajami
on 24 Mar 2018
Image Analyst
on 24 Mar 2018
Sorry, let me attach it here.
Sam Ajami
on 24 Mar 2018
Image Analyst
on 24 Mar 2018
Edited: Image Analyst
on 24 Mar 2018
The inner two loops can be vectorized - just use indexing to extract the 9 values in the little square window. But I don't see how you can do the whole thing without loops. The task does not say anything about vectorization.
Sam Ajami
on 24 Mar 2018
Sam Ajami
on 24 Mar 2018
Image Analyst
on 24 Mar 2018
There are differences between your method and the one I suggested. See attached code.

Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!