cv2.copyMakeBorder(src,top, bottom, left, right ,borderType,value)
BORDER_REFLICATE # 直接用边界的颜色填充
BORDER_REFLECT # 反转
BORDER_REFLECT_101 # 反转,和上面类似,但在反转时,会把边界空开
BORDER_WRAP
BORDER_CONSTANT # 常量,增加的变量均为value填充的颜色
import cv2
img = cv2.imread(r'C:\Users\x\Desktop\11.jpg', cv2.IMREAD_ANYCOLOR)
green = [0,255,0]
replicate = cv2.copyMakeBorder(img,50,50,50,50,cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img,10,10,10,10,cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img,10,10,10,10,cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img,10,10,10,10,cv2.BORDER_WRAP)
constant= cv2.copyMakeBorder(img,50,50,10,10,cv2.BORDER_CONSTANT,value=green)
cv2.imshow("image", img)
cv2.imshow("replicate", replicate)
cv2.imshow("reflect", reflect)
cv2.imshow("reflect101", reflect101)
cv2.imshow("wrap", wrap)
cv2.imshow("constant", constant)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
img = cv2.imread(r'C:\Users\x\Desktop\11.jpg', cv2.IMREAD_ANYCOLOR)
#构建矩阵,进行cv2.add 与 cv2.substract运算
A = np.ones(img.shape, dtype='uint8')*100
add = cv2.add(img, A)
subtract = cv2.subtract(img, A)
cv2.imshow("image", img)
cv2.imshow("add", add)
cv2.imshow("subtract", subtract)
cv2.waitKey(0)
cv2.destroyAllWindows()
图像与运算-cv2.bitwise_and(src1, src2, dst=None, mask=one)
图像或运算-cv2.bitwise_or(src1, src2, dst=None, mask=None)
图像非运算-cv2.bitwise_not(src1, src2, dst=None, mask=None)
图像异或运算-cv2.bitwise_xor,一个二值图,将黑色转为白色,白色转为黑
图像异或运算-cv2.bitwise_xor(src1, src2, dst=None, mask=None)
这四个运算,最少要有两个参数,即src1,src2;dst:参数返回结果可选,mask:参数也是可选的,指定msk区域进行操作。
import cv2
import numpy as np
# 生成矩形图形
rectangle = np.zeros((300, 300, 3), dtype='uint8')
cv2.rectangle(rectangle, (25, 25), (275, 275), (255,255,255), -1)
#生成圆形
circle = np.zeros((300, 300, 3), dtype='uint8')
cv2.circle(circle, (150, 150), 150, (255,255,255), -1)
# 位运算
bitwise_and = cv2.bitwise_and(rectangle, circle) #与
bitwise_or = cv2.bitwise_or(rectangle, circle) #或
bitwise_xor = cv2.bitwise_xor(rectangle, circle) #异或
bitwise_not = cv2.bitwise_not(rectangle) #非
cv2.imshow("rectangle", rectangle)
cv2.imshow("circle", circle)
cv2.imshow("bitwise_and", bitwise_and)
cv2.imshow("bitwise_or", bitwise_or)
cv2.imshow("bitwise_xor", bitwise_xor)
cv2.imshow("bitwise_not", bitwise_not)
cv2.waitKey(0)
cv2.destroyAllWindows()
图像掩模处理mask
import cv2
import numpy as np
img = cv2.imread(r'C:\Users\x\Desktop\47.jpg', cv2.IMREAD_ANYCOLOR)
mask = np.zeros(img.shape[:2], dtype='uint8')
(w, h) = (img.shape[1]//2, img.shape[0]//2)
cv2.rectangle(mask, (w-200, h-200), (w, h), 255, -1)
cv2.circle(mask, (w+20, h+90), 100, 255, -1)
masked = cv2.bitwise_and(img, img, mask=mask)
masked = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow("image", img)
cv2.imshow("mask", masked)
cv2.waitKey(0)
cv2.destroyAllWindows()
opencv中有多种色彩空间,包括 RGB、HSI、HSL、HSV、HSB、YCrCb、CIE XYZ、CIE Lab8种。
cv2.cvtColor(img, flag) 可以RGB转为GRAY&HSV
HSV(Hue(色调), Saturation(饱和度) , Value(明度)):
n色度H:用角度度量,取值范围为0~360,红色开始按逆时针方向计算,红色为0度,绿色为120度,蓝色为240度
饱和度S:接近光谱色的程度,颜色可以看成是光谱色与白色混合结果,光谱色占的比例愈大,颜色接近光谱色的程度越高,颜色饱和度就越高。光谱色中白色成分为0,饱和度达到最高,取值范围0%~100%,值越大,颜色越饱和
明度V:表示颜色明亮的程度,对于光源色,明度值与发光体的光亮度有关;对于物体色,与物体的透射比有关,取值范围为0%(黑)~100%(白)
在Opencv中
H色度取值范围是[0,179]
S饱和度的取值范围是[0,255]
V明度的取值范围是[0,255]
import cv2
img = cv2.imread(r'C:\Users\x\Desktop\47.jpg', cv2.IMREAD_ANYCOLOR)
GRAY = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
LAB = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
cv2.imshow("image", img)
cv2.imshow("GRAY", GRAY)
cv2.imshow("HSV", HSV)
cv2.imshow("LAB", LAB)
cv2.waitKey(0)
cv2.destroyAllWindows()