注意,本库基于python2.7,所以语法细节和之前有所不同
cv2和numpy深度契合,其图片读入后就是numpy.array,只不过dtype比较不常用而已,支持全部数组方法(废话...
cv2.cvtColor(img,cv2.COLOR_BAYER_BG2BGR)
img.item(0,0)
img.itemset((0,0),0)
cv2.imshow(‘my image‘,img)
cv2.waitKey()
cv2.destroyAllWindows()
1 # coding=utf-8 2 import cv2 3 import numpy as np 4 5 # array数组生成 6 img = np.zeros((3,3),dtype=np.uint8) 7 print img.shape 8 9 # array数组转化为BGR模式 10 # 我也不懂为什么不用RGB而用BGB这么蹩脚的用法 11 img = cv2.cvtColor(img,cv2.COLOR_BAYER_BG2BGR) 12 print img.shape 13 14 # 读取图片,左上像素点改写为蓝色,保存 15 img = cv2.imread(‘beauti.jpeg‘) 16 img[0][0] = [255,0,0] 17 cv2.imwrite(‘MyPic.png‘,img) 18 19 # 丢失颜色信息,左上像素点改写为黑色,保存 20 img = cv2.imread(‘beauti.jpeg‘,cv2.IMREAD_GRAYSCALE) 21 print img.shape 22 img[0][0] = 0 23 cv2.imwrite(‘MyPic-gray.png‘,img) 24 25 # 使用array.item和array.itemset优雅的重写上面代码 26 img = cv2.imread(‘beauti.jpeg‘,cv2.IMREAD_GRAYSCALE) 27 print img.shape 28 #img[0][0] = 0 29 print img.item(0,0) 30 img.itemset((0,0),0) 31 cv2.imwrite(‘MyPic-gray.png‘,img) 32 33 # 去掉绿色通道 34 img = cv2.imread(‘beauti.jpeg‘) 35 img[:,:,1] = 0 36 cv2.imwrite(‘no_green.png‘,img) 37 print img.shape,img.size,img.dtype 38 39 img = cv2.imread(‘beauti.jpeg‘) 40 # 显示图片,必须输入两个参数 41 cv2.imshow(‘my image‘,img) 42 # 窗口展示时间 43 cv2.waitKey() 44 # 释放窗口 45 cv2.destroyAllWindows()
时间: 2024-10-06 21:37:13