python 用opencv把人像上半身画线
时间: 2023-10-11 09:10:42 浏览: 121
在python中利用opencv做的人脸识别
要用Python和Opencv对人像上半身画线,可以按照以下步骤进行:
1. 导入必要的库和模块:cv2、numpy
```python
import cv2
import numpy as np
```
2. 读取原始图片
```python
img = cv2.imread('path/to/image')
```
3. 对图片进行剪切,只保留上半身部分
```python
height, width = img.shape[:2]
half_height = int(height / 2)
upper_body_img = img[0:half_height, :]
```
4. 绘制上半身轮廓线
```python
gray_img = cv2.cvtColor(upper_body_img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray_img, 100, 200)
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 50, minLineLength=50, maxLineGap=10)
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(upper_body_img, (x1, y1), (x2, y2), (0, 0, 255), 2)
```
5. 显示绘制完成的图片
```python
cv2.imshow('image', upper_body_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
将上述代码整合起来,可以得到以下完整代码:
```python
import cv2
import numpy as np
img = cv2.imread('path/to/image')
height, width = img.shape[:2]
half_height = int(height / 2)
upper_body_img = img[0:half_height, :]
gray_img = cv2.cvtColor(upper_body_img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray_img, 100, 200)
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 50, minLineLength=50, maxLineGap=10)
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(upper_body_img, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.imshow('image', upper_body_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
运行代码后,就可以看到绘制完成的图片,其中上半身部分的轮廓线已经被绘制出来了。
阅读全文