weixin_48459780 2020-07-12 16:54 采纳率: 0%
浏览 4838

TypeError: 'NoneType' object is not iterable出现这个应该怎解决?

在复制网上找到的代码以后,运行,出现错误,应该怎么解决?
看过相关资料但还是不会改,我就是个渣渣。请直接告诉我怎么改,谢谢了,救救孩子吧。

报错:

图片说明

代码:

import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

'''根据极角极径参数在原图像中画线'''
def draw_lines(img, lines, color=[255, 0, 0], thickness=6):
    for line in lines:
        for x1, y1, x2, y2 in line:
            # 在图像中设置俩条水平线,只有当直线于其相交,才绘制该直线,用于筛选直线
            top_horizon_line = ([0, img.shape[0] * 0.6], [img.shape[1], img.shape[0] * 0.7])
            bottom_horizon_line = ([0, img.shape[0]], [img.shape[1], img.shape[0]])
            line_intersection_top = line_intersection(top_horizon_line, ([x1, y1], [x2, y2]))
            line_intersection_bottom = line_intersection(bottom_horizon_line, ([x1, y1], [x2, y2]))
            if line_intersection_top == None or line_intersection_bottom == None:
                return
            # 绘制直线
            cv2.line(img, line_intersection_top, line_intersection_bottom, color, thickness)

'''判断俩条线段是否相交'''
def line_intersection(line1, line2):
    xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
    ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])

    def det(a, b):
        return a[0] * b[1] - a[1] * b[0]
    div = det(xdiff, ydiff)

    if div == 0:
        return None

    d = (det(*line1), det(*line2))
    x = det(d, xdiff) / div
    y = det(d, ydiff) / div
    return int(x), int(y)


image = cv2.imread("test.png")  # 读取源图像
showImg = True
image = np.array(image)
grayscale_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)  # 转为灰度图
kernel_size = 5
# 高斯平滑,大小为5的高斯核
gaussian_blur_image = cv2.GaussianBlur(grayscale_image, (kernel_size, kernel_size), 0)
# 设置阈值,进行canny边缘提取
canny_low_threshold = 10
canny_high_threshold = 20
edge_image = cv2.Canny(gaussian_blur_image, canny_low_threshold, canny_high_threshold)
# ROI可视区域选择,本程序选取左测公路区域
image_shape = edge_image.shape
x_offset = 200
y_offset = 90
v1 = (0, image_shape[0] - y_offset / 2)
v2 = (int(image_shape[1] / 4 + x_offset), int(image_shape[0] / 2 + y_offset))
v3 = (int(image_shape[1] / 2 - x_offset), int(image_shape[0] / 2 + y_offset))
v4 = (image_shape[1] / 2 + x_offset, image_shape[0] - y_offset / 2)
vert = np.array([[v1, v2, v3, v4]], dtype=np.int32)
mask = np.zeros_like(edge_image)
if len(edge_image.shape) > 2:
    channel_count = edge_image.shape[2]
    ignore_mask_color = (255,) * channel_count
else:
    ignore_mask_color = 255
# ROI可视区填充,在用mask与灰度图进行与运算,即在灰度图中得可视区
cv2.fillPoly(mask, vert, ignore_mask_color)
masked_edge_image = cv2.bitwise_and(edge_image, mask)
# 显示可视区的边缘提取二值图像
cv2.imshow("edge", masked_edge_image)
print(np.array([[v1, v2, v3, v4]], dtype=np.int32))
# 霍夫线变换
rho = 2  # 设置极径分辨率
theta = (np.pi) / 180  # 设置极角分辨率
threshold = 100  # 设置检测一条直线所需最少的交点
min_line_len = 300  # 设置线段最小长度
max_line_gap = 20  # 设置线段最近俩点的距离
lines = cv2.HoughLinesP(masked_edge_image, rho, theta, threshold, np.array([]), minLineLength=min_line_len,
                        maxLineGap=max_line_gap)
hough_line_image = np.zeros((masked_edge_image.shape[0], masked_edge_image.shape[1], 3), dtype=np.uint8)
# 绘制检测到的直线
draw_lines(hough_line_image, lines)
# 将直线与原图像合成为一幅图像
sync_image = cv2.addWeighted(image, 0.8, hough_line_image, 1, 0)
# 显示图像
if showImg:
    plt.imshow(sync_image)
    plt.show()
    cv2.imshow("lane", sync_image)
    cv2.waitKey(0)

  • 写回答

2条回答 默认 最新

  • threenewbee 2020-07-12 19:01
    关注

    调试下,应该是lines没有值

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮