美文网首页
三 (3.1 core 模块) 矩阵掩膜与图像线性操作

三 (3.1 core 模块) 矩阵掩膜与图像线性操作

作者: 交大小丑 | 来源:发表于2018-10-25 11:56 被阅读0次

    一、掩膜操作


    1.1掩膜操作函数:

    1.1.1filter2D 函数的定义如下:

    void filter2D( InputArray src, OutputArray dst, int ddepth,
                                InputArray kernel, Point anchor=Point(-1,-1),
                                double delta=0, int borderType=BORDER_DEFAULT );
    

    1.1.2像素范围处理saturate_cast<typename _Tp>()

    • saturate_cast<uchar>(-100),返回0
    • saturate_cast<uchar>(288),返回255
    • saturate_cast<uchar>(100),返回100
      这个函数的功能是确保RGB值范围在0~255之间。

    1.2 实例:理解filter2D函数的作用

    #include <iostream>
    #include <opencv2/opencv.hpp>
    #include <math.h>
    
    using namespace cv;
    
    int main(int argc, const char * argv[]) {
        Mat src, dst;
        //加载图像
        src = imread("/Users/Longxia/Downloads/552566-XXL.jpg");
        
        if (!src.data) {
            printf("could not load image\n");
            return -1;
        }
        //显示
        namedWindow("input Image", CV_WINDOW_AUTOSIZE);
        imshow("input Image", src);
        
        /*
         //掩膜操作
         int cols = (src.cols-1) * src.channels();
         int offsetx = src.channels();
         int rows = src.rows;
         dst = Mat::zeros(src.size(), src.type());
    
         for (int row = 1; row < rows-1; row++) {
           const uchar *previous = src.ptr<uchar>(row-1);
           const uchar *current = src.ptr<uchar>(row);
           const uchar *next = src.ptr<uchar>(row+1);
           uchar *output = dst.ptr<uchar>(row);
           for (int col = offsetx; col < cols; col++) {
             output[col] = saturate_cast<uchar>(5*current[col] - (current[col-offsetx] + current[col+offsetx] + previous[col] + next[col]));
           }
         }
         */
    
        // openCV API 掩膜操作
        //定义一个掩膜
        double t = getTickCount();  //获得当前时间
        Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1 ,0);
        //src.depth() 表示与原图深度一样,-1也表示一样
        filter2D(src, dst, src.depth(), kernel);
        double time = (getTickCount() - t) / getTickFrequency();
        printf("time consume %.5f", time);
        //显示
        namedWindow("contrast Image", CV_WINDOW_AUTOSIZE);
        imshow("contrast Image", dst);
        
        waitKey(0);
        return 0;
    }
    

    二、图像线性操作

    实现自己的线性滤波器 — OpenCV 2.3.2 documentation http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/imgtrans/filter_2d/filter_2d.html#filter-2d

    实例

    #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("D:/vcprojects/images/test1.png");
        if (!src.data) {
            printf("could not load image...\n");
            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);
        
        // 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 (true) {
            c = waitKey(500);
            if ((char)c == 27) {// ESC 
                break;
            }
            ksize = 5 + (index % 8) * 2;
            Mat kernel = Mat::ones(Size(ksize, ksize), CV_32F) / (float)(ksize * ksize);
            filter2D(src, dst, -1, kernel, Point(-1, -1));
            index++;
            imshow(OUTPUT_WIN, dst);    
        }
    
        // imshow("Sobel Y", yimg);
        return 0;
    }
    

    OpenCV学习之路(四)——矩阵的掩膜操作 - 简书 https://www.jianshu.com/p/c6d1c01c900b

    图像卷积与滤波 - 简书 https://www.jianshu.com/p/cbd1a1f86d1b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

    相关文章

      网友评论

          本文标题:三 (3.1 core 模块) 矩阵掩膜与图像线性操作

          本文链接:https://www.haomeiwen.com/subject/iepqtqtx.html