简体   繁体   中英

OpenCV filter2D: Filtering only part of the matrix/image

I encountered the following problem. I need to filter the matrix/image with linear filter, but I want to filter only those pixels that have sufficient number of neighbors around itself (according to the kernel size). To be concretely the result of filtering 32x32 image with 5x5 kernel should be of the 28x28 size.
Is it possible to do such a processing in relatively simple way with OpenCV built-in functions?

int kernel_size = 3;
cv::Mat in_img, out_img;
cv::Mat kernel = Mat::ones( kernel_size, kernel_size, CV_32F )/ (float)(kernel_size*kernel_size);       
cv::filter2D(in_img, out_img, -1 , kernel); //filtering

cv::Size size = in_img.size();
cv::Rect roi(kernel_size, kernel_size,size.width - 2*kernel_size, size.height - 2*kernel_size);
cv::Mat cropped = in_img(roi).clone(); //cropping

there is a function called cv::filter2D in opencv, but the output image will be of the same size as the input image (with zero padings during the filtering). There is another image/mathematical library called vxl, there you can find a convolution operator suitable for your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM