转自:http://www.2cto.com/kf/201312/267308.html

Mask Operation filter2D函数 Last Edit 2013/12/24 所谓的Mask Operation就是滤波。 第一步:建立Mask:

1
2
3
Mat kern = (Mat_<char>(3,3) <<  0, -10,
                               -15, -1,
                                0, -10);</char>

Mat_是一个模板,建立了一个3*3的矩阵,矩阵的值在-128~127. 
第二步:使用filter2D. 函数原型:

1
2
3
4
5
6
7
8
void filter2D(InputArray src, //要进行滤波的图像
              OutputArray dst,//滤波后的图像
              int ddepth,     //原图像的深度  src.depth()
              InputArray kernel, //第一步建立的Mask
              Point anchor=Point(-1,-1),//Mask的中心点
              double delta=0, //Optional value added to the filtered pixels before storing them in dst
              int borderType=BORDER_DEFAULT
               )
1
filter2D(I, K, I.depth(), kern );

以下是OpenCV2.0提供的sample:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <opencv2 core="" core.hpp="">
#include <opencv2 highgui="" highgui.hpp="">
#include <opencv2 imgproc="" imgproc.hpp="">
#include <iostream>
 
using namespace std;
using namespace cv;
 
void help(char* progName)
{
    cout << endl
        <<  "This program shows how to filter images with mask: the write it yourself and the"
        << "filter2d way. " << endl
        <<  "Usage:"                                                                        << endl
        << progName << " [image_name -- default lena.jpg] [G -- grayscale] "        << endl << endl;
}
 
 
void Sharpen(const Mat& myImage,Mat& Result);
 
int main( int argc, char* argv[])
{
    help(argv[0]);
    const char* filename = argc >=2 ? argv[1] : "lena.jpg";
 
    Mat I, J, K;
 
    if (argc >= 3 && !strcmp("G", argv[2]))
        I = imread( filename, CV_LOAD_IMAGE_GRAYSCALE);
    else
        I = imread( filename, CV_LOAD_IMAGE_COLOR);
 
    namedWindow("Input", CV_WINDOW_AUTOSIZE);
    namedWindow("Output", CV_WINDOW_AUTOSIZE);
 
    imshow("Input", I);
    double t = (double)getTickCount();
     
    Sharpen(I, J);
     
    t = ((double)getTickCount() - t)/getTickFrequency();
    cout << "Hand written function times passed in seconds: " << t << endl;
 
    imshow("Output", J);
    cvWaitKey(0);
 
    Mat kern = (Mat_<char>(3,3) <<  0, -10,
                                   -15, -1,
                                    0, -10);
    t = (double)getTickCount();
    filter2D(I, K, I.depth(), kern );
    t = ((double)getTickCount() - t)/getTickFrequency();
    cout << "Built-in filter2D time passed in seconds:      " << t << endl;
 
    imshow("Output", K);
 
    cvWaitKey(0);
    return 0;
}
void Sharpen(const Mat& myImage,Mat& Result)
{
    CV_Assert(myImage.depth() == CV_8U);  // accept only uchar images
 
    const int nChannels = myImage.channels();
    Result.create(myImage.size(),myImage.type());
     
    for(int j = 1 ; j < myImage.rows-1; ++j)
    {
        const uchar* previous = myImage.ptr<uchar>(j - 1);
        const uchar* current  = myImage.ptr<uchar>(j    );
        const uchar* next     = myImage.ptr<uchar>(j + 1);
 
        uchar* output = Result.ptr<uchar>(j);
 
        for(int i= nChannels;i < nChannels*(myImage.cols-1); ++i)
        {
            *output++ = saturate_cast<uchar>(5*current[i]
                         -current[i-nChannels] - current[i+nChannels] - previous[i] - next[i]);
        }
    }
 
    Result.row(0).setTo(Scalar(0));
    Result.row(Result.rows-1).setTo(Scalar(0));
    Result.col(0).setTo(Scalar(0));
    Result.col(Result.cols-1).setTo(Scalar(0));
}</uchar></uchar></uchar></uchar></uchar></char></iostream></opencv2></o

(转)Opencv卷积操作的更多相关文章

  1. Inception模型和Residual模型卷积操作的keras实现

    Inception模型和Residual残差模型是卷积神经网络中对卷积升级的两个操作. 一.  Inception模型(by google) 这个模型的trick是将大卷积核变成小卷积核,将多个卷积核 ...

  2. python——对图像进行卷积操作,使用多个滤波器

    线性滤波可以说是图像处理最基本的方法,它可以允许我们对图像进行处理,产生很多不同的效果.做法很简单.首先,我们有一个二维的滤波器矩阵(有个高大上的名字叫卷积核)和一个要处理的二维图像.然后,对于图像的 ...

  3. 对抗生成网络-图像卷积-mnist数据生成(代码) 1.tf.layers.conv2d(卷积操作) 2.tf.layers.conv2d_transpose(反卷积操作) 3.tf.layers.batch_normalize(归一化操作) 4.tf.maximum(用于lrelu) 5.tf.train_variable(训练中所有参数) 6.np.random.uniform(生成正态数据

    1. tf.layers.conv2d(input, filter, kernel_size, stride, padding) # 进行卷积操作 参数说明:input输入数据, filter特征图的 ...

  4. 深度学习原理与框架- tf.nn.conv2d_transpose(反卷积操作) tf.nn.conv2d_transpose(进行反卷积操作) 对于stride的理解存在问题?

    反卷积操作: 首先对需要进行维度扩张的feature_map 进行补零操作,然后使用3*3的卷积核,进行卷积操作,使得其维度进行扩张,图中可以看出,2*2的feature经过卷积变成了4*4.    ...

  5. 深度学习原理与框架-Tensorflow卷积神经网络-卷积神经网络mnist分类 1.tf.nn.conv2d(卷积操作) 2.tf.nn.max_pool(最大池化操作) 3.tf.nn.dropout(执行dropout操作) 4.tf.nn.softmax_cross_entropy_with_logits(交叉熵损失) 5.tf.truncated_normal(两个标准差内的正态分布)

    1. tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')  # 对数据进行卷积操作 参数说明:x表示输入数据,w表示卷积核, stride ...

  6. opencv::卷积运算函数filter2D()

    opencv::卷积运算函数filter2D() 使用掩模板矩阵(kernel)计算每个像素值 与原图相比,没有黑边 int main(int argc, char** argv) { Mat src ...

  7. ubuntu之路——day17.1 卷积操作的意义、边缘检测的示例、filter与padding的关系、卷积步长

    感谢吴恩达老师的公开课,以下图片均来自于吴恩达老师的公开课课件 为什么要进行卷积操作? 我们通过前几天的实验已经做了64*64大小的猫图片的识别. 在普通的神经网络上我们在输入层上输入的数据X的维数为 ...

  8. opencv 矩阵操作

    OpenCv矩阵操作 有很多函数有mask,代表掩码,如果某位mask是0,那么对应的src的那一位就不计算,mask要和矩阵/ROI/的大小相等 大多数函数支持ROI,如果图像ROI被设置,那么只处 ...

  9. ICLR 2020 | 抛开卷积,multi-head self-attention能够表达任何卷积操作

    近年来很多研究将nlp中的attention机制融入到视觉的研究中,得到很不错的结果,于是,论文侧重于从理论和实验去验证self-attention可以代替卷积网络独立进行类似卷积的操作,给self- ...

随机推荐

  1. BZOJ4197 [Noi2015]寿司晚宴 【状压dp】

    题目链接 BZOJ4197 题解 两个人选的数都互质,意味着两个人选择了没有交集的质因子集合 容易想到将两个人所选的质因子集合作为状态\(dp\) \(n\)以内质数很多,但容易发现\(\sqrt{n ...

  2. [HNOI2002]跳蚤 【容斥】

    题目描述 Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有N+1个自然数.其中最 ...

  3. Zend Hash table 详解--转

    原文地址:http://www.phppan.com/2009/12/zend-hashtable/ 在PHP的Zend引擎中,有一个数据结构非常重要,它无处不在,是PHP数据存储的核心,各种常量.变 ...

  4. position:fixed 相对父元素定位

    position:fixed是对于浏览器窗口定位的,要实现相当于父元素定位,可以这样: 不设置fixed元素的top,bottom,left,right,只设置margin来实现. 这种方法本质上fi ...

  5. 【loj6179】Pyh的求和

    Portal -->loj6179 Solution ​  这题其实有一个式子一喵一样的版本在bzoj,但是那题是\(m\)特别大然后只有一组数据  这题多组数据== ​    首先根据\(\v ...

  6. oracle中 trunc(),round(),ceil(),floor的使用

    oracle中 trunc(),round(),ceil(),floor的使用 原文: http://www.2cto.com/database/201310/248336.html 1.round函 ...

  7. oracle 数据库记录

    /*----------------------------------------------------------------------------*/ 问题1[--------] Selec ...

  8. CVPR 2014 ObjectnessBING 原文翻译

    BING: Binarized Normed Gradients for Objectness Estimation at 300fps Ming-Ming Cheng, Ziming Zhang, ...

  9. CSS中background样式的repeat和no-repeat

    自http://blog.sina.com.cn/s/blog_98eef7830101cv8t.html获取的知识,特此感谢. <!DOCTYPE html> <html lang ...

  10. 题解 P1682 【过家家】

    P1682 过家家 题目描述 有2n个小学生来玩过家家游戏,其中有n个男生,编号为1到n,另外n个女生,编号也是1到n.每一个女生可以先选择一个和她不吵嘴的男生来玩,除此之外,如果编号为X的女生的朋友 ...