Python+OpenCV学习笔记

图片读写及获取像素点信息

#基本图片读写和显示
import cv2
img = cv2.imread(’./black4.png’) # 或cv2.imread(black4.png) 或cv2.imread(‘D:\AAA\BBB\black4.png’)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #颜色转换为GRAY
cv2.imshow(‘winname’,gray) #显示图片
cv2.imwrite(’./gray4.png’, gray) #写入图片,和读取一致
cv2.waitKey(0) #等待窗口关闭
cv2.destroyAllWindows() #关闭所有窗体
#BGR分量的提取,OpenCV图像空间是B,G,R顺序
(B,G,R) = cv2.split(image) #提取BGR分量
cv2.imshow(“Red”,R)
cv2.imshow(“Green”,G)
cv2.imshow(“Blue”,B)
cv2.waitKey(0)
‘’’
blue=img2[x,y,0] #0位是B,1位是G,2位是R
green=img2[x,y,1]
red=img2[x,y,2]
print (blue,green,red)
color2=img2[x,y]
print (‘the BGR=’,color2)
b1=int(color2[0]) #强制转换数组型类型至int型,有些场景适用
g1=int(color2[1])
r1=int(color2[2])
‘’’’
#BGR分量的合并
merged = cv2.merge([B,G,R]) #合并BGR分量
cv2.imshow(“Merged”,merged)
cv2.waitKey(0)

你可能感兴趣的:(Python+OpenCV学习笔记)