Python opencv2(cv2)包装器获取图像大小? [英] Python opencv2 (cv2) wrapper get image size?
问题描述
如何在Python OpenCV(numpy)中的 cv2
包装器中获取图像大小。有没有比 numpy.shape()
更正确的方法。如何获得格式尺寸:(宽度,高度)列表?
How to get size of image in cv2
wrapper in Python OpenCV (numpy). Is there a correct way to do that than numpy.shape()
. How to get it in 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屋!