鼠标右键查看图像信息,如下图所示
- 宽度 202 像素
- 高度 249 像素
使用 cv2.imread 读取进来的 image 的类是 numpy.ndarray,pixel 是按照(row,column)索引的
import cv2
# read image
img = cv2.imread('OpenCV.png', cv2.IMREAD_UNCHANGED)
# get dimensions of image
dimensions = img.shape
# height, width, number of channels in image
height = img.shape[0]
width = img.shape[1]
channels = img.shape[2]
print(type(img))
print('Image Dimension : ', dimensions)
print('Image Height : ', height)
print('Image Width : ', width)
print('Number of Channels : ', channels)
代码输出:
<class 'numpy.ndarray'>
Image Dimension : (249, 202, 3)
Image Height : 249
Image Width : 202
Number of Channels : 3
注意,使用 imread 函数读入的数据 image 是 ‘numpy.ndarray’ 数据类型。
image。shape 先 高度,再宽度。
索引像素,一些几何物体按照(column,row)顺序
注意,Mat 数据类型并不是按照 (column, row) 索引,因为 Mat 与 如图 1 所示的像素坐标系毫无关系,它只是矩阵数据类型。
举个例子,但是其他几何形状是按照(column, row) 索引的,比如在图像上画一条竖直线段的代码如下:
import cv2
# read image
img = cv2.imread('OpenCV.png', cv2.IMREAD_UNCHANGED)
# get dimensions of image
dimensions = img.shape
# height, width, number of channels in image
height = img.shape[0]
width = img.shape[1]
channels = img.shape[2]
cv2.line(img, (98, 0), (98, 1279), (0, 0, 125), 2)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.resizeWindow('image', 700, 500)
cv2.imshow("image", img)
cv2.waitKey(0)
输出图像:
Point (98, 0),和(98, 1279)这两个点,如果按照(row,column)索引,理应是一条水平而非竖直线段。