zoukankan      html  css  js  c++  java
  • OpenCV-C++ 图像卷积计算的边缘问题

    卷积边缘问题

    图像在执行卷积计算的时候,图像边缘的像素无法被计算,边缘无法被卷积核正确覆盖;

    • BORDER_DEFAULT
    • BORDER_CONSTANT: 填充边缘用指定像素值;
    • BORDER_REPLICATE: 填充边缘像素用已知的边缘像素值;
    • BORDER_WRAP: 用另外一遍的像素来补偿填充;

    使用copyMakeBorder添加边缘像素:

    void copyMakeBorder(InputArray src, OutputArray dst,
                        int top, int bottom, int left, int right,
                        int borderType, const Scalar& value = Scalar() );
    
    • src表示原始图像;
    • dst表示输出图像;
    • top, bottom, left, right表示四周扩展的像素数目;
    • borderType表示填充像素的方式
    • Scalar表示填充的像素值,配合BORDER_CONSTANT使用;

    完整代码

    #include <iostream>
    #include <opencv2/opencv.hpp>
    
    using namespace std;
    using namespace cv;
    
    /**
     * 边缘处理
    */
    int main(){
        // 读取图像
        Mat src = imread("/home/chen/dataset/lena.jpg");
        if (src.empty()){
            cout << "could not load image." << endl;
            return -1;
        }
        namedWindow("src", WINDOW_AUTOSIZE);
        imshow("src", src); 
    
        // 使用copyMakeBorder添加边缘像素
        int c = 0;
        RNG rng(12345);
        int borderType = BORDER_DEFAULT;
        Mat dst;
        namedWindow("dst", WINDOW_AUTOSIZE);
        while(true){
            c = waitKey(500);
            if((char)c == 27){
                break;
            }
            if ((char)c == 'r'){
                borderType = BORDER_REFLECT;
            } else if ((char)c == 'w'){
                borderType = BORDER_WRAP;
            } else if ((char)c == 'c'){
                borderType = BORDER_CONSTANT;
            }
            Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
            copyMakeBorder(src, dst, 100, 100, 100, 100, borderType, color);
            imshow("dst", dst);
        }
        waitKey(0);
    
        return 0;
    }
    
  • 相关阅读:
    【2020-04-03】多注意一下自己闲下来的思绪
    vue 去哪网项目 学习笔记(一)
    数据分析相关的内容
    vue 自学项目笔记
    vue 所有的指令
    vue 自学笔记(5) 列表渲染
    vue 自学笔记(4): 样式绑定与条件渲染
    vue 自学笔记(三) 计算属性与侦听器
    自学vue笔记 (二) : 实例与生命周期
    杜教BM模板
  • 原文地址:https://www.cnblogs.com/chenzhen0530/p/14655774.html
Copyright © 2011-2022 走看看