3
votes

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

2
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
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
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
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

2 Answers

0
votes

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.

0
votes

@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.