Is there any built-in function to apply min, max and Avg filters to an image in opencv2.4.13 ? I'm using c++ .
As @Miki mentioned in the comments, boxfilter is a mean filter. Just set the kernel size you want and leave normalize=true (the default).
The functions erode and dilate are min and max filters, respectively. You can create the kernel using createMorphologyFilter, create your own, or use the default 3x3 as I've done. The borders are set by default to +inf for erode and -inf for dilate so they do not contribute to the result.
int main(int argc, const char * argv[]) {
char image_data[25] = {1, 3, 8, 8, 4,
4, 2, 7, 9, 9,
1, 5, 0, 5, 9,
3, 7, 5, 2, 1,
0, 4, 7, 9, 4};
cv::Mat image = cv::Mat(5, 5, CV_8U, image_data);
std::cout << "image = " << std::endl << image << std::endl;
cv::Mat avgImage;
// Perform mean filtering on image using boxfilter
cv::boxFilter(image, avgImage, -1, cv::Size(3,3));
std::cout << "avgImage = " << std::endl << avgImage << std::endl;
cv::Mat kernel; // Use the default structuring element (kernel) for erode and dilate
cv::Mat minImage;
// Perform min filtering on image using erode
cv::erode(image, minImage, kernel);
std::cout << "minImage = " << std::endl << minImage << std::endl;
cv::Mat maxImage;
// Perform max filtering on image using dilate
cv::dilate(image, maxImage, kernel);
std::cout << "maxImage = " << std::endl << maxImage << std::endl;
return 0;
}
Here are the results:
image =
[ 1, 3, 8, 8, 4;
4, 2, 7, 9, 9;
1, 5, 0, 5, 9;
3, 7, 5, 2, 1;
0, 4, 7, 9, 4]
avgImage =
[ 3, 4, 6, 8, 8;
3, 3, 5, 7, 7;
4, 4, 5, 5, 6;
4, 4, 5, 5, 5;
5, 5, 5, 4, 4]
minImage =
[ 1, 1, 2, 4, 4;
1, 0, 0, 0, 4;
1, 0, 0, 0, 1;
0, 0, 0, 0, 1;
0, 0, 2, 1, 1]
maxImage =
[ 4, 8, 9, 9, 9;
5, 8, 9, 9, 9;
7, 7, 9, 9, 9;
7, 7, 9, 9, 9;
7, 7, 9, 9, 9]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With