Opencv 图像数字化 ndarray
灰度图转化为ndarray
灰度图像的位深度为8位,图像中的每一个像素点灰度的深浅由256个数字来衡量,所以灰度图在计算机面前就是一个二维数字矩阵。通过设置imread函数中的flag参数为IMREAD_GRAYSCALE即可将灰度图转化为ndarray
import cv2
import numpy as np
img = cv2.imread("c:/users/76973/desktop/output_image1.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("img",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
彩色图像转化为三维ndarray
通过设置imread函数中的flag参数为IMREAD_COLOR即可将灰度图转化为ndarray
import cv2
import numpy as np
image = cv2.imread("c:/users/76973/desktop/compressedpicture.jpg",cv2.IMREAD_COLOR)
# 得到三个颜色通道
b = image[:,:,0]
g = image[:,:,1]
r = image[:,:,2]
cv2.imshow("BGR",image)
cv2.imshow("B",b)
cv2.imshow("G",g)
cv2.imshow("R",r)
cv2.waitKey(0)
cv2.destroyAllWindows()