OpenCV-Python是旨在解決計算機視覺問題的Python綁定庫。cv2.copyMakeBorder()
方法用於在像相框一樣的圖像周圍創建邊框。
用法: cv2.copyMakeBorder(src, top, bottom, left, right, borderType, value)
參數:
src:它是源圖像。
top:它是頂部方向上的像素數的邊框寬度。
bottom:它是底部方向上的像素數的邊框寬度。
left:它是左側像素的邊界寬度。
right:它是沿正確方向的像素數的邊框寬度。
borderType:它描述了要添加哪種邊框。它由cv2.BORDER_CONSTANT,cv2.BORDER_REFLECT等標誌定義
value:這是一個可選參數,如果border type為,則描述邊框的顏色cv2.BORDER_CONSTANT。
返回值:它返回一個圖像。
borderType標誌如下所述:
cv2.BORDER_CONSTANT: It adds a constant colored border. The value should be given as next argument.
cv2.BORDER_REFLECT: The border will be mirror reflection of the border elements. Suppose, if image contains letters “abcdefg” then output will be “gfedcba|abcdefg|gfedcba“.
cv2.BORDER_REFLECT_101 or cv2.BORDER_DEFAULT: It does the same works as cv2.BORDER_REFLECT but with slight change. Suppose, if image contains letters “abcdefgh” then output will be “gfedcb|abcdefgh|gfedcba“.
cv2.BORDER_REPLICATE: It replicates the last element. Suppose, if image contains letters “abcdefgh” then output will be “aaaaa|abcdefgh|hhhhh“.
用於以下所有示例的圖像:
示例1:
# Python program to explain cv2.copyMakeBorder() method
# importing cv2
import cv2
# path
path = r'C:\Users\Rajnish\Desktop\geeksforgeeks\geeks.png'
# Reading an image in default mode
image = cv2.imread(path)
# Window name in which image is displayed
window_name = 'Image'
# Using cv2.copyMakeBorder() method
image = cv2.copyMakeBorder(image, 10, 10, 10, 10, cv2.BORDER_CONSTANT)
# Displaying the image
cv2.imshow(window_name, image)
輸出:
示例2:
# Python program to explain cv2.copyMakeBorder() method
# importing cv2
import cv2
# path
path = r'C:\Users\Rajnish\Desktop\geeksforgeeks\geeks.png'
# Reading an image in default mode
image = cv2.imread(path)
# Window name in which image is displayed
window_name = 'Image'
# Using cv2.copyMakeBorder() method
image = cv2.copyMakeBorder(image, 100, 100, 50, 50, cv2.BORDER_REFLECT)
# Displaying the image
cv2.imshow(window_name, image)
輸出:
相關用法
- Python OpenCV cv2.putText()用法及代碼示例
- Python OpenCV cv2.rectangle()用法及代碼示例
- Python OpenCV cv2.erode()用法及代碼示例
- Python OpenCV cv2.circle()用法及代碼示例
- Python OpenCV cv2.blur()用法及代碼示例
- Python OpenCV cv2.line()用法及代碼示例
- Python OpenCV cv2.arrowedLine()用法及代碼示例
- Python OpenCV cv2.ellipse()用法及代碼示例
- Python OpenCV cv2.imwrite()用法及代碼示例
- Python OpenCV cv2.cvtColor()用法及代碼示例
- Python OpenCV cv2.imread()用法及代碼示例
- Python OpenCV cv2.imshow()用法及代碼示例
注:本文由純淨天空篩選整理自Rajnis09大神的英文原創作品 Python OpenCV | cv2.copyMakeBorder() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。