调用opencv函数:enum ConvolutionType { /* Return the full convolution, including border */ CONVOLUTION_FULL, /* Return only the part that corresponds to the original image */ CONVOLUTION_SAME,/* Return only the submatrix containing elements that were not influenced by the border */ CONVOLUTION_VALID };void conv2(const Mat &img, const Mat& kernel, ConvolutionType type, Mat& dest) { Mat source = img; if(CONVOLUTION_FULL == type) { source = Mat(); const int additionalRows = kernel.rows-1, additionalCols = kernel.cols-1; copyMakeBorder(img, source, (additionalRows+1)/2, additionalRows/2, (additionalCols+1)/2, additionalCols/2, BORDER_CONSTANT, Scalar(0)); } Point anchor(kernel.cols - kernel.cols/2 - 1, kernel.rows - kernel.rows/2 - 1); int borderMode = BORDER_CONSTANT; filter2D(source, dest, img.depth(), flip(kernel), anchor, 0, borderMode); if(CONVOLUTION_VALID == type) { dest = dest.colRange((kernel.cols-1)/2, dest.cols - kernel.cols/2) .rowRange((kernel.rows-1)/2, dest.rows - kernel.rows/2); } }
效率问题需要优化.可以参考INTEL IPP库,看是否有类似的实现.
也可考虑用GPU优化,参考nvida的CUDA.
/* Return the full convolution, including border */
CONVOLUTION_FULL, /* Return only the part that corresponds to the original image */
CONVOLUTION_SAME,/* Return only the submatrix containing elements that were not influenced by the border
*/
CONVOLUTION_VALID
};void conv2(const Mat &img, const Mat& kernel, ConvolutionType type, Mat& dest) {
Mat source = img;
if(CONVOLUTION_FULL == type) {
source = Mat();
const int additionalRows = kernel.rows-1, additionalCols = kernel.cols-1;
copyMakeBorder(img, source, (additionalRows+1)/2, additionalRows/2,
(additionalCols+1)/2, additionalCols/2, BORDER_CONSTANT, Scalar(0));
} Point anchor(kernel.cols - kernel.cols/2 - 1, kernel.rows - kernel.rows/2 - 1);
int borderMode = BORDER_CONSTANT;
filter2D(source, dest, img.depth(), flip(kernel), anchor, 0, borderMode); if(CONVOLUTION_VALID == type) {
dest = dest.colRange((kernel.cols-1)/2, dest.cols - kernel.cols/2)
.rowRange((kernel.rows-1)/2, dest.rows - kernel.rows/2);
}
}