opencv2.4.13中的Min,Max,Avg过滤器

时间:2016-09-29 14:16:08

标签: c++ opencv

是否有内置函数将min,max和Avg滤镜应用于opencv2.4.13中的图像? 我使用的是c ++。

1 个答案:

答案 0 :(得分:9)

正如@Miki在评论中提到的,boxfilter是一个均值过滤器。只需设置所需的内核大小,然后保留normalize=true(默认值)。

函数erodedilate分别是最小和最大过滤器。您可以使用createMorphologyFilter创建内核,创建自己的内核,或者使用默认的3x3。对于+inf,边框默认设置为erode-inf设置为dilate,因此它们不会对结果做出贡献。

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;
}

结果如下:

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]