Python OpenCV2(cv2)包装器获取图像大小? [英] Python OpenCV2 (cv2) wrapper to get image size?
问题描述
如何在Python OpenCV(numpy)中的cv2
包装器中获取图像的大小.除了numpy.shape()
之外,还有其他正确的方法吗?如何获得以下格式的尺寸:(宽度,高度)列表?
How to get the size of an image in cv2
wrapper in Python OpenCV (numpy). Is there a correct way to do that other than numpy.shape()
. How can I get it in these format dimensions: (width, height) list?
推荐答案
cv2
使用numpy
来处理图像,因此获取图像大小的正确和最佳方法是使用numpy.shape
.假设您正在使用BGR图片,下面是一个示例:
cv2
uses numpy
for manipulating images, so the proper and best way to get the size of an image is using numpy.shape
. Assuming you are working with BGR images, here is an example:
>>> import numpy as np
>>> import cv2
>>> img = cv2.imread('foo.jpg')
>>> height, width, channels = img.shape
>>> print height, width, channels
600 800 3
如果要使用二进制图像,则img
将具有两个尺寸,因此必须将代码更改为:height, width = img.shape
In case you were working with binary images, img
will have two dimensions, and therefore you must change the code to: height, width = img.shape
这篇关于Python OpenCV2(cv2)包装器获取图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!