3

I have large matrix (image) and a small template. I would like to convolve the small matrix with the larger matrix. For example, the blue region is the section that I want to be used for convolution. In other words, I can use the convolution for all of the image, but since the CPU time is increased, therefore, I would like to just focus on the desired blue part.

Is there any command in MATLAB that can be used for this convolution? Or, how I can force the convolution function to just use that specific irregular section for convolution.

enter image description here

4
  • If you want to improve performance of your convolution code, did you try to profile it? for example, in conv2 are you using single instead of double?
    – bla
    Jan 29, 2013 at 0:49
  • 1
    So I think that your best bet is to get the smallest rectangular bounding rect. conv2 doesn't optimize for sparse input. I think that filter2 doesn't optimize for sparse input either. One of the reasons is because it probably uses SIMD instructions. Using SIMD, skipping small holes actually doesn't speed things up.
    – thang
    Jan 29, 2013 at 1:03
  • So this guy's code helped me in the past mathworks.com/matlabcentral/fileexchange/…. Not sure if it is relevant to your use. Basically, if you're convolving something small with something big, an SVD can be used to decompose the small thing into separable components...
    – thang
    Jan 29, 2013 at 1:33
  • this thread addresses the matter somewhat: mathworks.com/matlabcentral/answers/5011 though I wouldn't say that's the final word on the matter...
    – bla
    Jan 29, 2013 at 5:29

2 Answers 2

0

I doubt you can do an irregular shape (fast convolution is done with 2D FFT, which would require a square region). You could optimize it by finding the shape's bounding box and thus discarding the empty border.

5
  • @ nneonneo: Actually my matrix is a very large 3D matrix and I need to repeat this convolution several times. That is why I am trying to ignore the outer boundary side.
    – Nicole
    Jan 29, 2013 at 0:52
  • -1. actually, in general it isn't true that using the convolution theorem makes it faster, and conv2 doesn't use fft.
    – thang
    Jan 29, 2013 at 0:54
  • OK, so I didn't realize conv2 didn't use FFT. Why is that? Perhaps you could enlighten me as to why it wouldn't be faster to use an FFT to compute the convolution.
    – nneonneo
    Jan 29, 2013 at 0:58
  • 2
    how fast is it to convolve an mxm filter with an nxn image directly? how fast is it to do fft2 on an nxn image (for now, don't even count multiply and ifft2)? when is one greater than the other? when is the other greater than the one?
    – thang
    Jan 29, 2013 at 1:00
  • @thang and nneonneo: I am sure that using fft can make the convolution to be faster, but in my algorithm, event the seconds are important! I already have implemented fft in my algorithm, but I want to escape that NaN region!!
    – Nicole
    Jan 29, 2013 at 1:04
0

@Nicole i would go for the fft2(im).*fft(smallIm) which is the equivalent for conv2(im,smallIm).
as far as recognizing the irregular shape you can use edge detection like canny and find the values of the most (left,right,top,bottom) dots,
since canny returns a binary (1,0) image and prepare a bounding box, using the values. however this will take some time to create.
and i'm not sure about how much faster will this be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.