cv2 获取图片的长宽
时间: 2024-06-03 21:05:08 浏览: 190
在使用OpenCV的Python接口时,可以使用cv2.imread()函数读取图片,并通过numpy数组的shape属性获取图片的长和宽。示例如下:
```python
import cv2
img = cv2.imread("image.jpg")
height, width = img.shape[:2]
print("Image height: ", height)
print("Image width: ", width)
```
如果你想获得图片的通道数,可以使用img.shape。但是需要注意的是,如果图片是灰度图,则img.shape只会返回两个值,即高和宽。
相关问题
pythonopencv获取图片长宽
使用Python和OpenCV获取图片的长宽可以使用以下代码实现:
```python
import cv2
img = cv2.imread("image.jpg") # 读取图片
height, width, channels = img.shape # 获取图片的长宽和通道数
print("长:", height, "宽:", width)
```
opencv获取图片长宽
在OpenCV中,可以使用`shape`属性来获取图像的长宽。具体的方法如下所示:
```python
import cv2
# 读取图像
image = cv2.imread("image.jpg")
# 获取图像的长宽
h, w, _ = image.shape
# 打印图像的长宽
print("图像的长为:", h)
print("图像的宽为:", w)
```
这段代码会输出图像的长和宽。请注意,`shape`属性返回的是一个包含三个元素的元组,分别表示图像的高度、宽度和通道数。在这个例子中,我们只关心图像的长和宽,所以使用了`_`来忽略通道数。
阅读全文
相关推荐
















