• 自定义卷积核


     

     

     

     

     

       示例代码:

    //Robert算子
        Mat kernel = (Mat_<float>(2, 2) << 1, 0, 0, -1);
        filter2D(src, dst, -1, kernel, Point(-1, -1), 0.0);
        imshow(OUTPUT_WIN, dst);

    自定义卷积核,可以用

    Mat kernel = (Mat_<float>(2, 2) << 1, 0, 0, -1);

    给出卷积核的矩阵。

    下面是生成一个one矩阵然后归一化矩阵的卷积核,模糊图像。

     自动模糊图像显示代码:

    #include <opencv2/opencv.hpp>
    #include <iostream>
    #include <math.h>
    
    using namespace cv;
    int main(int argc, char** argv) {
        Mat src, dst;
        int ksize = 0;
    
        src = imread("L:4.jpg");
        if (!src.data) {
            printf("could not load image...
    ");
            return -1;
        }
    
        char INPUT_WIN[] = "input image";
        char OUTPUT_WIN[] = "Custom Blur Filter Result";
        namedWindow(INPUT_WIN, CV_WINDOW_AUTOSIZE);
        namedWindow(OUTPUT_WIN, CV_WINDOW_AUTOSIZE);
    
        imshow(INPUT_WIN, src);
    
        //Robert算子
        //Mat kernel = (Mat_<float>(2, 2) << 1, 0, 0, -1);
        //filter2D(src, dst, -1, kernel, Point(-1, -1), 0.0);
        //imshow(OUTPUT_WIN, dst);
    
        // Sobel X 方向
        // Mat kernel_x = (Mat_<int>(3, 3) << -1, 0, 1, -2,0,2,-1,0,1);
        // filter2D(src, dst, -1, kernel_x, Point(-1, -1), 0.0);
    
        // Sobel Y 方向
        // Mat yimg;
        // Mat kernel_y = (Mat_<int>(3, 3) << -1, -2, -1, 0,0,0, 1,2,1);
        // filter2D(src, yimg, -1, kernel_y, Point(-1, -1), 0.0);
    
        // 拉普拉斯算子
        //Mat kernel_y = (Mat_<int>(3, 3) << 0, -1, 0, -1, 4, -1, 0, -1, 0);
        //filter2D(src, dst, -1, kernel_y, Point(-1, -1), 0.0);
    
        int c = 0;
        int index = 0;
        //while循环:
        while (true) {
            c = waitKey(500);      //延时500ms
            if ((char)c == 27) {   // 按键ESC=27,退出while循环 
                break;
            }
            ksize = 5 + (index % 8) * 2;   //index对8取余运算后乘2加5
            Mat kernel = Mat::ones(Size(ksize, ksize), CV_32F) / (float)(ksize * ksize);
            //定义一个ksize的one矩阵,格式CV_32F,然后除ksize*ksize归一化。
            filter2D(src, dst, -1, kernel, Point(-1, -1));
            //filter2D参数:1.源图  2.目标图像  3.为-1,目标图像和原图像深度保持一致  4.卷积核  5.默认Point(-1,-1)卷积核中心位置
            index++;
            imshow(OUTPUT_WIN, dst);
        }
    
        // imshow("Sobel Y", yimg);
        return 0;
    }

    结果:

     500ms模糊一次:

  • 相关阅读:
    php的冒泡算法
    mysql 处理重复数据
    Ehcache配置详解及CacheManager使用
    JFinal常见问题和知识点笔记
    JFinal连接数据库配置说明
    innerHTML和innerText区分
    方法$.data()和$.('#test').on()的使用
    input text的所有事件
    jQuery操作Select
    在js或css后加?v= 版本号不让浏览器缓存
  • 原文地址:https://www.cnblogs.com/Jack-Elvis/p/11510470.html
Copyright © 2020-2023  润新知