Opencv学习(二)——读取图像信息
发布日期:2021-05-07 22:56:12 浏览次数:19 分类:精选文章

本文共 906 字,大约阅读时间需要 3 分钟。

读取图像的尺寸信息在图像处理任务中非常重要。OpenCV 提供了通过 cv2.imread() 函数读取图像的高度、宽度和通道数的功能。以下将详细介绍如何读取并获取图像的这些属性。

读取图像高度

要读取图像的高度,可以通过 im.shape[0] 获取返回值的第一个值。以下是具体的代码示例:

import cv2 as cv
import numpy as np
im = cv.imread('fruit.png')
height = im.shape[0]
cv.imshow('origin image', im)
print(im.shape)
print('height is:', height)
cv.waitKey()

运行该代码后,输出结果为:

(477, 686, 3)
height is: 477

读取图像宽度

同样地,读取图像的宽度可以通过 im.shape[1] 获取返回值的第二个值。以下是具体的代码示例:

import cv2 as cv
import numpy as np
im = cv.imread('fruit.png')
width = im.shape[1]
cv.imshow('origin image', im)
print(im.shape)
print('width is:', width)
cv.waitKey()

运行该代码后,输出结果为:

(477, 686, 3)
width is: 686

读取图像通道数

最后,读取图像的通道数可以通过 im.shape[2] 获取返回值的第三个值。以下是具体的代码示例:

import cv2 as cv
import numpy as np
im = cv.imread('fruit.png')
channel = im.shape[2]
cv.imshow('origin image', im)
print(im.shape)
print('channel is:', channel)
cv.waitKey()

运行该代码后,输出结果为:

(477, 686, 3)
channel is: 3
上一篇:基于FPGA的数字钟(四)——时钟控制模块
下一篇:Opencv学习(一)——图片的加载显示和保存

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年04月09日 04时28分28秒