首页 技术 正文
技术 2022年11月16日
0 收藏 387 点赞 4,417 浏览 3729 个字

Use the OpenCV function :copy_make_border:`copyMakeBorder <>` to set the borders (extra padding to your image).The explanation below belongs to the book Learning OpenCV by Bradski and Kaehler.

  1. In our previous tutorial we learned to use convolution to operate on images. One problem that naturally arises is how to handle the boundaries. How can we convolve them if the evaluated points are at the edge of the image?
  2. What most of OpenCV functions do is to copy a given image onto another slightly larger image and then automatically pads the boundary (by any of the methods explained in the sample code just below). This way, the convolution can be performed over the needed pixels without problems (the extra padding is cut after the operation is done).
  3. In this tutorial, we will briefly explore two ways of defining the extra padding (border) for an image:
    1. BORDER_CONSTANT: Pad the image with a constant value (i.e. black or0)
    2. BORDER_REPLICATE: The row or column at the very edge of the original is replicated to the extra border.

This will be seen more clearly in the Code section.

What does this program do?

  • Load an image
  • Let the user choose what kind of padding use in the input image. There are two options:
  • Constant value border: Applies a padding of a constant value for the whole border. This value will be updated randomly each 0.5 seconds.
  • Replicated border: The border will be replicated from the pixel values at the edges of the original image.
  • The user chooses either option by pressing ‘c’ (constant) or ‘r’ (replicate)
  • The program finishes when the user presses ‘ESC’

The tutorial code’s is shown lines below.

''' file name : border.py
Description : This sample shows how to add border to an image'''import cv2
import numpy as npprint " Press r to replicate the border with a random color "
print " Press c to replicate the border "
print " Press Esc to exit "img = cv2.imread('../boldt.jpg')
rows,cols = img.shape[:2]
dst = img.copy()top = int (0.05*rows)
bottom = int (0.05*rows)
left = int (0.05*cols)
right = int (0.05*cols)while(True):
cv2.imshow('border',dst)
k = cv2.waitKey(500)
if k==27:
break
elif k == ord('c'):
value = np.random.randint(0,255,(3,)).tolist()
dst = cv2.copyMakeBorder(img,top,bottom,left,right,
cv2.BORDER_CONSTANT,value = value)
elif k == ord('r'):
dst = cv2.copyMakeBorder(img,top,bottom,left,right,cv2.BORDER_REPLICATE)
cv2.destroyAllWindows()

Explanation

1. Now we initialize the argument that defines the size of the borders (top,bottom,left andright). We give them a value of 5% the size of src.

top = int (0.05*rows)
bottom = int (0.05*rows)left = int (0.05*cols)
right = int (0.05*cols)

2. The program begins a while loop. If the user presses ‘c’ or ‘r’, the borderType variable takes the value of BORDER_CONSTANT or BORDER_REPLICATE respectively:

while(True):    cv2.imshow('border',dst)
k = cv2.waitKey(500)
if k==27:
break
elif k == ord('c'):
value = np.random.randint(0,255,(3,)).tolist()
dst = cv2.copyMakeBorder(img,top,bottom,left,right,cv2.BORDER_CONSTANT,value = value)
elif k == ord('r'):
dst = cv2.copyMakeBorder(img,top,bottom,left,right,cv2.BORDER_REPLICATE)

3. Finally, we call the function :copy_make_border:`copyMakeBorder <>` to apply the respective padding:

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

The arguments are:

  • src: Source image
  • dst: Destination image
  • top, bottom, left, right: Length in pixels of the borders at each side of the image. We define them as being 5% of the original size of the image.
  • borderType: Define what type of border is applied. It can be constant or replicate for this example.
  • value: If borderType is BORDER_CONSTANT, this is the value used to fill the border pixels.

输出结果

【OpenCV】解析OpenCV中copyMakerBorder函数

After compiling the code above, you can execute it giving as argument the path of an image. The result should be:

  • By default, it begins with the border set to BORDER_CONSTANT. Hence, a succession of random colored borders will be shown.
  • If you press ‘r’, the border will become a replica of the edge pixels.
  • If you press ‘c’, the random colored borders will appear again
  • If you press ‘ESC’ the program will exit.

Below some screenshot showing how the border changes color and how the BORDER_REPLICATE option looks:

【OpenCV】解析OpenCV中copyMakerBorder函数
【OpenCV】解析OpenCV中copyMakerBorder函数
【OpenCV】解析OpenCV中copyMakerBorder函数

=====================================================
转载请注明处:http://blog.csdn.net/songzitea/article/details/8698083
=====================================================
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291