如何修复参数'src2'的期望cv :: UMat

时间:2019-08-04 19:31:16

标签: python-3.x opencv4

我当前正在Jupyter笔记本上运行此代码,并且不断收到此错误:参数'src2'的预期cv :: UMat,我不知道为什么! 关于这个问题的所有其他问题都没有帮助我解决此代码问题 我怎样才能解决这个问题? 重要提示:代码可能在此处有错误的位置(对不起第一个问题)

 import numpy as np
 import cv2
 import math

 fourcc = cv2.VideoWriter_fourcc(*'XVID')

 cap = cv2.VideoCapture("tes.mp4")
 out = cv2.VideoWriter('ada.avi',fourcc, 20.0, (960,540))



while(cap.isOpened()):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    red = cv2.GaussianBlur(gray,(3,3),0)
    blue = cv2.Canny(red,50,50, apertureSize = 3)
    gold = cv2.resize(blue, (960, 540))
    mask = np.zeros(gold.shape, dtype=np.uint8)
    roi_corners = np.array([[(618,259), (430,259), (106,461) ,(840,461)]], dtype=np.int32)
    ignore_mask_color = (255)
    cv2.fillPoly(mask, roi_corners, ignore_mask_color)
    masked_image = cv2.bitwise_and(gold, mask)
    lines = cv2.HoughLinesP(masked_image, 1, np.pi/180, 150 , minLineLength=100, maxLineGap=100)
    def draw_lines(img, lines, color=[255, 0, 0], thickness=3):
        line_img = np.zeros(
    (
        frame_w,
        frame_h,
        frame_c
    ),
    dtype=np.uint8
)
    img = np.copy(frame)
    if lines is None:
        return    
    for line in lines:
        for x1, y1, x2, y2 in line:
            cv2.line(line_img, (x1, y1), (x2, y2), color, thickness)    
        img = cv2.addWeighted(img, 0.8, line_img, 1.0, 0.0)    
def pipeline(image):
    left_line_x = []
    left_line_y = []
    right_line_x = []
    right_line_y = []

    for line in lines:
        for x1, y1, x2, y2 in line:
            slope = (y2 - y1) / (x2 - x1)
        if math.fabs(slope) < 0.5:
            pass
        if slope <= 0:
            left_line_x.extend([x1, x2])
            left_line_y.extend([y1, y2])
        else:
            right_line_x.extend([x1, x2])
            right_line_y.extend([y1, y2])   
    min_y = int(frame.h * (3 / 5))
    max_y = int(frame.h)    
    poly_left = np.poly1d(np.polyfit(
    left_line_y,
    left_line_x,
    deg=1
))

    left_x_start = int(poly_left(max_y))
    left_x_end = int(poly_left(min_y))

    poly_right = np.poly1d(np.polyfit(
        right_line_y,
        right_line_x,
        deg=1
))

    right_x_start = int(poly_right(max_y))
    right_x_end = int(poly_right(min_y))    
    line_image = draw_lines(
        image,
    [[
        [left_x_start, max_y, left_x_end, min_y],
        [right_x_start, max_y, right_x_end, min_y],
    ]],
    thickness=5,
)   
c = cv2.add(frame, pipeline)
cv2.imshow('frame',c)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

cap.release()
out.release()
cv2.destroyAllWindows()

0 个答案:

没有答案