Opencv学习(二)——读取图像信息

读取图像高度

使用cv2.imread()函数可以读取图片的高,宽,通道数。奇异返回值的第一个值就是图像的高度。
具体代码如下:

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

读取图像宽度

同理,读取宽度只要取返回值的第二个数据即可
代码如下:

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

读取图像通道数

同理,通道数只要访问返回值的第三个量
代码如下:

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

发布了7 篇原创文章 · 获赞 13 · 访问量 2710

猜你喜欢

转载自blog.csdn.net/qq_43650722/article/details/104033970