Python-OpenCV 基本用法

在这里插入图片描述

鼠标右键查看图像信息,如下图所示

  • 宽度 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 先 高度,再宽度

在这里插入图片描述

图1. SLAM14 讲 P107

索引像素,一些几何物体按照(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)索引,理应是一条水平而非竖直线段。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

培之

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值