Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 cv::Mat &img, cv::Mat& dest, const cv::Mat& kernel, ConvolutionType type)
- {
- cv::Mat source = img;
- if (type == CONVOLUTION_FULL)
- {
- source = cv::Mat();
- const int additionalRows = kernel.rows - 1, additionalCols = kernel.cols - 1;
- copyMakeBorder(img, source, (additionalRows + 1) / 2, additionalRows / 2, (additionalCols + 1) / 2, additionalCols / 2, cv::BORDER_CONSTANT, cv::Scalar(0));
- }
- cv::Point anchor(cv::Point(-1, -1));
- int borderMode = cv::BORDER_CONSTANT;
- filter2D(source, dest, img.depth(), kernel.t(), anchor, 0, borderMode);
- if (type == CONVOLUTION_VALID)
- {
- dest = dest.colRange((kernel.cols - 1) / 2, dest.cols - kernel.cols / 2)
- .rowRange((kernel.rows - 1) / 2, dest.rows - kernel.rows / 2);
- }
- }
- int main(void)
- {
- // ....
- cv::Mat imgMat = cv::imread("sample.jpg", CV_LOAD_IMAGE_COLOR);
- cv::Mat gau_mask = cv::getGaussianKernel(7, 1.5, CV_32F);
- conv2(imgMat_bw, imgMat_bw, gau_mask, CONVOLUTION_SAME);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement