python-opencv图像裁剪
时间: 2023-10-08 20:06:51
浏览: 71
在Python中使用OpenCV库进行图像裁剪非常简单,可以按照以下步骤进行操作:
1. 导入OpenCV库
```python
import cv2
2. 读取图像文件
```python
img = cv2.imread('path_to_image')
3. 设置裁剪区域
```python
# 设置裁剪区域的左上角和右下角坐标
x1, y1 = 100, 100
x2, y2 = 200, 200
# 获取裁剪区域
crop_img = img[y1:y2, x1:x2]
4. [显示](https://geek.csdn.net/educolumn/103ca72607c717122e068b9f06a24df6?spm=1055.2569.3001.10083)裁剪后的图像
```python
cv2.imshow('Cropped Image', crop_img)
cv2.waitKey(0)
cv2.destroyAll[window](https://geek.csdn.net/educolumn/03e5a5a554174a38fdfeb8376bd685e2?spm=1055.2569.3001.10083)s()
完整的[代码](https://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083)示例:
```python
import cv2
# 读取图像文件
img = cv2.imread('path_to_image')
# 设置裁剪区域的左上角和右下角坐标
x1, y1 = 100, 100
x2, y2 = 200, 200
# 获取裁剪区域
crop_img = img[y1:y2, x1:x2]
# [显示](https://geek.csdn.net/educolumn/103ca72607c717122e068b9f06a24df6?spm=1055.2569.3001.10083)裁剪后的图像
cv2.imshow('Cropped Image', crop_img)
cv2.waitKey(0)
cv2.destroyAll[window](https://geek.csdn.net/educolumn/03e5a5a554174a38fdfeb8376bd685e2?spm=1055.2569.3001.10083)s()
```
相关问题
opencv-python图像裁剪
可以使用OpenCV库中的cv2.crop()函数来裁剪图像。该函数需要指定裁剪区域的左上角和右下角坐标。以下是一个示例代码:
```py
```