Qt-OpenCV学习笔记--图像边界处理--copyMakeBorder()

概述

这个函数的作用是在图像的周边加上边框。但这个函数更多是用于 卷积核 操作前的边缘处理。

图像进行卷积操作时,图像的边界像素并不能被卷积操作到,原因在于边界像素没有完全跟 kernel 重叠,例如当3*3滤波时有1个像素的边缘没有被处理,5*5滤波时有2个像素的边缘没有被处理。

在卷积操作开始之前,先增加边缘像素,比如3*3滤波时,在图像四周各填充1个像素的边缘,这样就确保图像的边缘能被处理,在卷积处理之后再去掉这些边缘。

函数

void cv::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 边框类型 BORDER_CONSTANT BORDER_WRAP BORDER_REPLICATE BORDER_REFLECT BORDER_REFLECT_101 BORDER_CONSTANT_DEFAULT value 边框颜色(当 borderType 的值为 Border_CONSTANT 时)

测试代码

#include "widget.h"
#include "ui_widget.h"

#include <QDebug>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <vector>

using namespace cv;
using namespace std;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //载入源图像
    Mat src = imread("c:/opencv/logo.jpg");
    //显示
    imshow("src",src);

    //目标图像
    Mat dst;

    //定义边框颜色
    Scalar scalar(50,200,50);

    //边界处理
    copyMakeBorder(src,dst,20,20,20,20,BORDER_CONSTANT,scalar);
    //显示
    imshow("dst",dst);

}

Widget::~Widget()
{
    delete ui;
}

测试结果

参考

经验分享 程序员 微信小程序 职场和发展