copyMakeBorder

本文介绍使用OpenCV进行图像边界填充的方法,重点讲解了两种填充方式:BORDER_CONSTANT和BORDER_REPLICATE,并通过代码示例展示了如何实现这两种边界填充效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

copyMakeBorder(src,dst,top,bottom,left,right,borderType,value);

borderType有两种
- BORDER_CONSTANT
- BORDER_REPLICATE

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

using namespace cv;
using namespace std;
Mat src, dst;
int top0, bottom0, left0, right0;
int borderType;
Scalar value;
String window_name="copyMakeBorder Demo";
RNG rng(12345);

int main()
{
    Mat src=imread("image.jpg");
    int c;
    namedWindow(window_name,WINDOW_AUTOSIZE);
    top0=(int)(0.05*src.rows);
    bottom0=(int)(0.05*src.rows);
    left0=(int)(0.05*src.cols);
    right0=(int)(0.05*src.cols);

    dst=src;
    imshow(window_name,dst);
    while(true)
    {
        c=waitKey(500);
        if((char)c==27)
            break;
        else if((char)c=='c')
            borderType=BORDER_CONSTANT;
        else if((char)c=='r')
            borderType=BORDER_REPLICATE;

        value=Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255));
        copyMakeBorder(src,dst,top0,bottom0,left0,right0,borderType,value);

        imshow(window_name,dst);
    }
    return 0;
}

实验截图
这里写图片描述

这里写图片描述
以上两张为BORDER_CONSTANT结果
这里写图片描述
以上为BORDER_REPLICATE结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值