Opencv访问图像像素
预备知识:
图像矩阵的大小取决于所用的颜色模型(或者说通道数),灰度图矩阵如下:
多通道图像,如RGB颜色模型的矩阵如下:
注:opencv的通道顺序是BGR,而不是RGB
访问图像中像素方法:
import numpy as np
import cv2
size = (2560, 1600)
black = np.zeros(size)
black[225:300] = 255
black[34,56] = 255
print(black[34][56])
cv2.imshow(‘black’, black)
cv2.waitKey(0)
cv2.imwrite(‘black.jpg’,black)
black[:]=255
print(black[34][56])
cv2.imshow(‘white’, black)
cv2.waitKey(0)
cv2.imwrite(‘white.jpg’,black)
import numpy as np
import cv2
size = (2560, 1600)
# 全黑.可以用在屏保
black = np.zeros(size)
black[225:300] = 255
# black[:,225:300] = 255
black[34,56] = 255
print(black[34][56])
cv2.imshow('black', black)
cv2.waitKey(0)
cv2.imwrite('black.jpg',black)
#white 全白
black[:]=255
print(black[34][56])
cv2.imshow('white', black)
cv2.waitKey(0)
cv2.imwrite('white.jpg',black)