1

I have an image U, and when I want to convolve it with a box filter:

0  1  0
1 -4  1
0  1  0

I use imfilter function with a constant 2D array and there is no problem. But, when I have the following operation:

u(i,j) = v(i-1,j)^2 * u(i-1,j) + v(i+1,j)^2 * u(i+1,j) + v(i, j+1)^2 * u(i,j+1) + v(i,j-1)^2 * u(i,j-1)

(A simplified version of my filter). In other words, my filter to be used over image U is related to the pixel values of image V, but in the same location which the filter is applied. Is there a way to implement such an operation in MATLAB, WITHOUT using nested for loops for each pixel?

fercis
  • 611
  • 2
  • 12
  • 26
  • 3
    Are you not looking to take `u.*(v.^2)` and then convolve that with an appropriate filter? – nkjt Mar 18 '14 at 11:10
  • You could consider using BLOCKPROC with a [1 1] block size and appropriate border size. In the callback, you'll get the index to lookup pixels from v. Should be quick to implement, but slow to run. Lookup blockproc and give it a try, post back if you get stuck. Aside: You might be interested in http://www.mathworks.com/help/images/ref/imguidedfilter.html – Ashish Uthama Mar 18 '14 at 14:36
  • 1
    Be careful if you intend to follow @Ashish's recommendation: block processing ~= moving filter! Appart from that, I second the approach of multiplying the two images prior to filtering. – Cape Code Mar 18 '14 at 15:31
  • Jigg, it is exactly that. Using a block size of [1 1] with a BorderSize results in a sliding window filter. http://stackoverflow.com/questions/15195030/how-to-divide-image-matlab-into-overlapping-block/21562631#21562631 – Ashish Uthama Mar 18 '14 at 18:12

1 Answers1

0

You can solve it using im2col and col2im as following:

% Filtering A using B

mA = randn(10, 10); mB = randn(10, 10);

mACol = im2col(mA, [3, 3], 'sliding'); mBCol = im2col(mB, [3, 3], 'sliding');

mAColFilt = sum(mACol .* (mBCol .^ 2));

mAFilt = col2im(mAColFilt, [3, 3], [10, 10]);

I skipped the to get the correct coefficients (In your case, zero few of them and raise to the power of 2 the rest, I only raised all to the power of 2).

Pay attention that the filtered image is smaller (Bounadries of the filter).
You should pad it as required.

Royi
  • 4,640
  • 6
  • 46
  • 64