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 np print " 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.

输出结果

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:



=====================================================
转载请注明处:http://blog.csdn.net/songzitea/article/details/8698083
=====================================================

【OpenCV】解析OpenCV中copyMakerBorder函数的更多相关文章

  1. (转)解析PHP中ob_start()函数的用法

    本篇文章是对PHP中ob_start()函数的用法进行了详细的分析介绍,需要的朋友参考下     ob_start()函数用于打开缓冲区,比如header()函数之前如果就有输出,包括回车/空格/换行 ...

  2. 【PHP】解析PHP中的函数

    目录结构: contents structure [-] 可变参数的函数 变量函数 回调函数 自定义函数库 闭包(Closure)函数的使用 在这篇文章中,笔者将会讲解如何使用PHP中的函数,PHP是 ...

  3. 解析opencv中Box Filter的实现并提出进一步加速的方案(源码共享)。

    说明:本文所有算法的涉及到的优化均指在PC上进行的,对于其他构架是否合适未知,请自行试验. Box Filter,最经典的一种领域操作,在无数的场合中都有着广泛的应用,作为一个很基础的函数,其性能的好 ...

  4. OpenCV图像处理中常用函数汇总(1)

    //俗话说:好记性不如烂笔头 //用到opencv 中的函数时往往会一时记不起这个函数的具体参数怎么设置,故在此将常用函数做一汇总: Mat srcImage = imread("C:/Us ...

  5. OpenCV中phase函数计算方向场

    一.函数原型 ​该函数参数angleInDegrees默认为false,即弧度,当置为true时,则输出为角度. phase函数根据函数来计算角度,计算精度大约为0.3弧度,当x,y相等时,angle ...

  6. opencv学习笔记之cvSobel 函数解析

    首先,我们来开一下计算机是如何检测边缘的.以灰度图像为例,它的理论基础是这样的,如果出现一个边缘,那么图像的灰度就会有一定的变化,为了方便假设由黑渐变为白代表一个边界,那么对其灰度分析,在边缘的灰度函 ...

  7. opencv学习笔记——cv::CommandLineParser函数详解

    命令行解析类CommandLineParser 该类的作用主要用于命令行的解析,也就是分解命令行的作用.以前版本没这个类时,如果要运行带参数的.exe,必须在命令行中输入文件路径以及各种参数,并且输入 ...

  8. opencv通过dll调用matlab函数,图片作为参数

    [blog 项目实战派]opencv通过dll调用matlab函数,图片作为参数                   前文介绍了如何“csharp通过dll调用opencv函数,图片作为参数”.而在实 ...

  9. Opencv 3.3.0 常用函数

    如何调图像的亮度和对比度? //如何增加图片的对比度或亮度? void contrastOrBrightAdjust(InputArray &src,OutputArray &dst, ...

随机推荐

  1. 1010 Radix (25)(25 point(s))

    problem Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true ...

  2. JSTL介绍

    JSTL介绍 一.介绍 JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能. JSTL支持通用的.结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签,SQL标 ...

  3. UVA1378 A funny stone game

    博弈论. 就是有一堆石子你拿走一堆中的一个,然后再向后面两堆中加两个问胜负 i<j<=k 所以我们可以直接通过sg函数计算,考虑问题的奇偶性,如果这一位是奇的我们才考虑,偶的可以模仿 然后 ...

  4. nginx_lua vs nginx+php 应用场景

    在我的印象中很多人还是选择nginx+php这种组合搭配,你的选择是nginx+lua,那么nginx+lua比和php的组合优势在哪里?清无:首先,Nginx+php之间是要有进程之间通信的,这样以 ...

  5. hdu 1325 判断有向图是否为树

    题意:判断有向图是否为树 链接:点我 这题用并查集判断连通,连通后有且仅有1个入度为0,其余入度为1,就是树了 #include<cstdio> #include<iostream& ...

  6. spring boot2集成ES详解

    一:运行环境 JDK:1.8 ES:5.6.4 二:学习内容 如何构建spring-data-elasticsearch环境? 如何实现常用的增删改查? 如何实现对象嵌套也就是1对多这种关系? 三:J ...

  7. mac os 切换网络优先级

    升级到新系统OS X Yesemite 系统后有WIFI时会默认使用WIFI而不是有线. 但是公司的WIFI基本没法用,每次到公司之后就得把WIFI关掉,回家又打开,烦死了. 今天研究了下原来网络优先 ...

  8. HDOJ 4414 Finding crosses 暴力!

    Finding crosses Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. [Windows核心编程]32bit程序在64bit操作系统下处理重定向细节[1]

    这段时间,都在做Ring3层的普通32bit程序兼容64bit操作系统的代码修改,在此记录修改和学习心德.编程领域太广, 任何人经历有限,本人不是专家,所以我一贯原则是: 用到的时候,才去研究,在去记 ...

  10. 常见 core dump 原因分析signal 11 - SIGSEGV

    signal 6 - SIGABRT free 多次 char *p = malloc(100); free(p); free(p); fclose 多次 // fclose 内部调用 free FI ...