OpenCV学习14--自定义线性滤波

卷积
模糊图像,图像边缘,增强图像

常见卷积算子:
Robert、sobel、拉普拉斯算子

代码:

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;

int main(int argc,char ** argv)
{
    Mat src,dst;
    Mat kernel;

    char outwindow1[] = "out1";
    char outwindow2[] = "out2";
    char outwindow3[] = "out3";
    char outwindow4[] = "out4";
    char outwindow5[] = "out5";
    char outwindow6[] = "out6";
    src = imread("1.jpg");
    namedWindow("input window",CV_WINDOW_AUTOSIZE);
    imshow("input window",src);

    namedWindow(outwindow1,CV_WINDOW_AUTOSIZE);
    namedWindow(outwindow2,CV_WINDOW_AUTOSIZE);
    namedWindow(outwindow3,CV_WINDOW_AUTOSIZE);
    namedWindow(outwindow4,CV_WINDOW_AUTOSIZE);
    namedWindow(outwindow5,CV_WINDOW_AUTOSIZE);
    namedWindow(outwindow6,CV_WINDOW_AUTOSIZE);
    Mat kernel_x = (Mat_<int>(2,2)<<1,0,0,-1);
    filter2D(src,dst,-1,kernel_x,Point(-1,-1),0.0);
    imshow(outwindow1,dst);
    Mat kernel_y = (Mat_<int>(2,2)<<0,1,-1,0);
    filter2D(src,dst,-1,kernel_y,Point(-1,-1),0.0);
    imshow(outwindow2,dst);
    //左右的差异
    Mat kernel2_x = (Mat_<int>(3,3)<<-1,0,1,-2,0,2,-1,0,1);
    filter2D(src,dst,-1,kernel2_x,Point(-1,-1),0.0);
    imshow(outwindow3,dst);
    //上下差异
    Mat kernel2_y = (Mat_<int>(3,3)<<-1,-2,1,0,0,0,1,2,1);
    filter2D(src,dst,-1,kernel2_y,Point(-1,-1),0.0);
    imshow(outwindow4,dst);

    //拉普拉斯算子
    Mat kernel3 = (Mat_<int>(3,3)<<0,-1,0,-1,4,-1,0,-1,0);
    filter2D(src,dst,-1,kernel3,Point(-1,-1),0.0);
    imshow(outwindow5,dst);

    int ksize = 0;
    int c = 0;
    int index = 0;
    while(true)
    {
        c = waitKey(500);
        if((char)c == 27)
        {
            break;
        }
        ksize = 4 + (index % 8) * 2 + 1;
        Mat kernel_auto = Mat::ones(Size(ksize,ksize),CV_32F)/float(ksize*ksize);
        filter2D(src,dst,-1,kernel_auto,Point(-1,-1),0.0);
        index ++;
        imshow(outwindow6,dst);
    }

    return 0;
}

效果:
这里写图片描述

源码请到github下载:
https://github.com/MRwangmaomao/OpencvConvolution-Project.git

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南山二毛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值