深度学习(Deep Learning)与OpenCV(Open Source Computer Vision Library)的结合为计算机视觉领域带来了强大的解决方案。OpenCV是一个开源的计算机视觉和机器学习软件库,它提供了大量的视觉处理算法,包括但不限于图像和视频处理、特征检测、对象识别等。
import cv2
a = cv2.imread('2.png')
top, bottom, left, right = 50, 50, 50, 50
constant = cv2.copyMakeBorder(a, top, bottom, left, right, borderType=cv2.BORDER_CONSTANT, value=(0, 0, 0))
reflect = cv2.copyMakeBorder(a, top, bottom, left, right, borderType=cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(a, top, bottom, left, right, borderType=cv2.BORDER_REFLECT101)
replicate = cv2.copyMakeBorder(a, top, bottom, left, right, borderType=cv2.BORDER_REPLICATE)
wrap = cv2.copyMakeBorder(a, top, bottom, left, right, borderType=cv2.BORDER_WRAP)
cv2.imshow('a', a)
cv2.waitKey(0)
cv2.imshow('constant', constant)
cv2.waitKey(0)
cv2.imshow('reflect', reflect)
cv2.waitKey(0)
cv2.imshow('reflect101', reflect101)
cv2.waitKey(0)
cv2.imshow('replicate', replicate)
cv2.waitKey(0)
cv2.imshow('wrap', wrap)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
image = cv2.imread('3.png', cv2.IMREAD_GRAYSCALE)
ret, binary = cv2.threshold(image, 180, 255, cv2.THRESH_BINARY) # maxval:0
ret1, binaryinv = cv2.threshold(image, 180, 255, cv2.THRESH_BINARY_INV) # 0:maxval
ret2, trunc = cv2.threshold(image, 190, 255, cv2.THRESH_TRUNC) # thresh:当前灰度值
ret3, tozero = cv2.threshold(image, 100, 255, cv2.THRESH_TOZERO) # 当前灰度值:0
ret4, tozeroinv = cv2.threshold(image, 190, 255, cv2.THRESH_TOZERO_INV) # 0:当前灰度值
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.imshow('binary', binary)
cv2.waitKey(0)
cv2.imshow('binaryniv', binaryinv)
cv2.waitKey(0)
cv2.imshow('trunc', trunc)
cv2.waitKey(0)
cv2.imshow('tozero', tozero)
cv2.waitKey(0)
cv2.imshow('tozeroinv', tozeroinv)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
def add_peppersalt_noise(image, n=10000):
result = image.copy()
h, w = image.shape[:2]
for i in range(n):
x = np.random.randint(1, h)
y = np.random.randint(1, w)
if np.random.randint(0, 2) == 0:
result[x, y] = 0
else:
result[x, y] = 255
return result
image = cv2.imread('3.png')
cv2.imshow('a', image)
cv2.waitKey(0)
noise = add_peppersalt_noise(image)
cv2.imshow('noise', noise)
cv2.waitKey(0)
OpenCV作为一个开源的计算机视觉库,具有显著的优点和一定的缺点。以下是对其优缺点的详细分析: