cv2.line(img, (x1, y1), (x2, y2), (0, 0, 0), -1)
时间: 2023-09-22 08:15:34 浏览: 67
在上面的代码中,如果将cv2.line()函数中的thickness参数设为-1,即可将直线以实心的形式绘制。这样就可以不用遍历每条直线,而是直接将所有直线以黑色实心的形式绘制在原图上。
修改后的代码如下:
```
import cv2
import numpy as np
# 读入原图
img = cv2.imread('image.jpg')
# 对img进行处理,得到筛选出的直线lines
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Scharr = cv2.Scharr(gray, cv2.CV_8U, 1, 0)
lines = cv2.HoughLinesP(Scharr, 1, np.pi / 180, 100, minLineLength=300, maxLineGap=20)
# 在img上将所有直线以黑色实心的形式绘制
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 0), thickness=-1)
# 显示处理后的图像
cv2.imshow('result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
运行以上代码,即可得到将筛选出的直线变为黑色实心后的图像。
相关问题
for line in lines: x1, y1, x2, y2 = line[0] cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
这段代码是用于在原始图像上绘制检测到的直线,其中lines表示检测得到的直线的端点坐标,x1, y1, x2, y2分别表示直线的起点和终点坐标,cv2.line函数用于绘制直线,img表示要绘制的原始图像,(x1, y1)和(x2, y2)表示直线的起点和终点坐标,(0, 0, 255)表示直线的颜色,2表示直线的宽度。具体来说,该代码实现了将检测到的直线绘制到原始图像上的功能,使得用户可以直观地看到图像中的直线。
def draw_line(line): x1, y1, x2, y2 = line[0] cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)这段代码出现错误,应该怎么改正
There seems to be a missing comma after `y1`. Here's the corrected code:
```
def draw_line(line):
x1, y1, x2, y2 = line[0]
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
```
相关推荐
![tgz](https://img-home.csdnimg.cn/images/20210720083646.png)
![dll](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)