基本语法
def read_demo():
image = cv.imread('C:/Users/Administrator/Pictures/Camera Roll/1.jpg')
cv.imshow('input',image)
cv.waitKey(0)
cv.destroyAllWindows()
图像色彩 (滤镜)
def color_space_demo():
'''图像色彩'''
image = cv.imread('D:/tupian/2.jpg') #BGR,0-255
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
hsv = cv.cvtColor(image,cv.COLOR_HSV2RGB) # H, 0-180
cv.imshow('gray', gray)
cv.imshow('hsv', hsv)
cv.waitKey(0)
# cv.namedWindow('input', cv.WINDOW_AUTOSIZE)
cv.destroyAllWindows()
图像对象的创建与复制
def mat_demo():
'''图像对象的创建与复制'''
# image = cv.imread('D:/tupian/2.jpg',cv.IMREAD_GRAYSCALE) #灰度色彩,没有 C
image = cv.imread('D:/tupian/2.jpg')
# print(image)
print(image.shape) # (1024, 681, 3) H W C:代表通道
roi = image[100:200,100:200,:]
blank = np.zeros_like(image) #创建空白图像
blank[1:300, 100:300, 1:20] = image[1:300,100:300,1:20] # copy一部分image图像到空白图像blank上
#或者用zero
# h,w,c = image.shape
# blank = np.zeros((h,w,c),dtype=np.uint8)
# blank[1:300, 100:300, 1:20] = image[1:300, 100:300, 1:20] # copy一部分到空白图像
blank = np.copy(image) #全copy
cv.imshow('image', image)
cv.imshow('blank', blank)
# cv.imshow('roi', roi)
cv.waitKey(0)
cv.destroyAllWindows()
结果如下
对图像像素取反效果
def piexl_demo():
'''对图像像素取反效果'''
image = cv.imread('D:/tupian/2.jpg') #BGR,0-255
cv.imshow('start',image)
h,w,c = image.shape # 1024 681 3
print(image)
print(image[1,1])
for row in range(h):
for col in range(w):
b,g,r = image[row,col]
image[row, col] = (255-b,255-g,255-r) #像素取反效果
cv.imshow('result',image)
cv.imwrite('D:/tupian/2_bac.jpg',image) # 保存在本地
cv.waitKey(0)
# cv.namedWindow('input', cv.WINDOW_AUTOSIZE)
cv.destroyAllWindows()
结果如下