OPENCV TypeError:应为整数参数,获得浮点值

2024-10-01 00:25:00 发布

您现在位置:Python中文网/ 问答频道 /正文

伙计们,我正在努力学习这个快速教程。 我也在努力学习我们输入的每一个代码。 但我犯了个错误。我正在努力学习每一个代码,所以你能告诉我为什么会出现这个错误吗?如何解决这个错误呢

我在函数def draw_lines(img,lines,color=[255,0,0],thickness=3)中得到错误:

我得到的错误:

File "C:\Users\alpgu\Desktop\opencv\yolboyama.py", line 112, in poly_left = np.poly1d(np.polyfit(

File "<array_function internals>", line 5, in polyfit

File "C:\Users\alpgu\anaconda3\lib\site-packages\numpy\lib\polynomial.py", line 599, in polyfit raise TypeError("expected non-empty vector for x")

TypeError: expected non-empty vector for x

我认为问题在于::

poly_left = np.poly1d(np.polyfit(
        left_line_y,
        left_line_x,
        deg=1
    ))

为什么会这样

注==>; 当我更改最大x和最小y代码时(你说它应该是int,所以我尝试更改代码。我也尝试了int,但它给出了另一个错误。所以我不明白我应该知道什么?)

min_y = int(yol_resmi.shape * (3 / 5)) # <-- Just below the horizon
    max_y = int(yol_resmi.shape)

它给出了

TypeError: can't multiply sequence by non-int of type 'float'

是关于我的max_x-min_y值的问题还是关于我的polly_left值的问题

我所有的代码:

from os.path import sep
import cv2
import numpy as np
import math

cap = cv2.VideoCapture('photos' + sep + 'roads.mp4')

while(cap.isOpened()):
    ret, yol_resmi = cap.read()
    
    # Define a blank matrix that matches the image height/width.
    def region_of_interest(img, vertices):
        mask = np.zeros_like(img)
        
        channel_count = yol_resmi.shape[2]
        
        match_mask_color = (255,) * channel_count
        
        cv2.fillPoly(mask, vertices, match_mask_color)
        
        masked_image = cv2.bitwise_and(img, mask)
        return masked_image
    
    height, width, channel = yol_resmi.shape 
    
    region_of_interest_vertices = [
        (0, height),
        (width / 2, height / 2),
        (width, height),
    ]
    
    def draw_lines(img, lines, color=[255, 0, 0], thickness=3):
        line_img = np.zeros(
            (
                img.shape[0],
                img.shape[1],
                3
            ),
            dtype=np.uint8
        )
        img = np.copy(img)
        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)
        return img
    
    #COLOR_BGR2GRAY ekle
    gray_image = cv2.cvtColor(yol_resmi, cv2.COLOR_RGB2GRAY)
    cannyed_image = cv2.Canny(gray_image, 200, 300)
    cropped_image = region_of_interest(
        cannyed_image,
        np.array(
            [region_of_interest_vertices],
            np.int32
        ),
    )
    lines = cv2.HoughLinesP(
        cropped_image,
        rho=6,
        theta=np.pi / 60,
        threshold=160,
        lines=np.array([]),
        minLineLength=40,
        maxLineGap=25
    )
    print(lines)
    
    line_image = draw_lines(yol_resmi, lines)
    
    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) # <-- Calculating the slope.
            if math.fabs(slope) < 0.5: # <-- Only consider extreme slope
                continue
            if slope <= 0: # <-- If the slope is negative, left group.
                left_line_x.extend([x1, x2])
                left_line_y.extend([y1, y2])
            else: # <-- Otherwise, right group.
                right_line_x.extend([x1, x2])
                right_line_y.extend([y1, y2])
    min_y = yol_resmi.shape[0] * (3 / 5) # <-- Just below the horizon
    max_y = yol_resmi.shape[0] # <-- The bottom of the image
    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(
        yol_resmi,
        [[
            [left_x_start, max_y, left_x_end, min_y],
            [right_x_start, max_y, right_x_end, min_y],
        ]],
        thickness=5,
    )
    
    # Convert to grayscale here.
    #gray_image = cv2.cvtColor(cropped_image, cv2.COLOR_RGB2GRAY)
    
    # Call Canny Edge Detection here.
    #cannyed_image = cv2.Canny(gray_image, 100, 200)
    
    #cv2.imshow('yol resmi', yol_resmi)
    cv2.imshow('edge detection', cropped_image)
    cv2.imshow('line detection', line_image)
    #cv2.imshow('Edge Detection', cannyed_image)
    
    cv2.waitKey(1)

Tags: inimagerightimgnplinemincv2
1条回答
网友
1楼 · 发布于 2024-10-01 00:25:00

这就是我所能做的:

from os.path import sep
import cv2
import numpy as np
import math
import warnings

cap = cv2.VideoCapture('photos' + sep + 'roads.mp4')

while cap.isOpened():
    ret, yol_resmi = cap.read()

    # Define a blank matrix that matches the image height/width.
    def region_of_interest(img, vertices):
        mask = np.zeros_like(img)

        channel_count = yol_resmi.shape[2]

        match_mask_color = (255,) * channel_count

        cv2.fillPoly(mask, vertices, match_mask_color)

        masked_image = cv2.bitwise_and(img, mask)
        return masked_image


    height, width, channel = yol_resmi.shape

    region_of_interest_vertices = [
        (0, height),
        (width / 2, height / 2),
        (width, height),
    ]


    def draw_lines(img, lines, color=[255, 0, 0], thickness=3):
        line_img = np.zeros(
            (
                img.shape[0],
                img.shape[1],
                3
            ),
            dtype=np.uint8
        )
        img = np.copy(img)
        if lines is None:
            return
        for line in lines:
            for x1, y1, x2, y2 in line:
                print(type(x1), type(x2), type(y1), type(y2))
                cv2.line(line_img, (x1, y1), (x2, int(y2)), color, thickness)
        img = cv2.addWeighted(img, 0.8, line_img, 1.0, 0.0)
        return img


    # COLOR_BGR2GRAY ekle
    gray_image = cv2.cvtColor(yol_resmi, cv2.COLOR_RGB2GRAY)
    cannyed_image = cv2.Canny(gray_image, 200, 300)
    cropped_image = region_of_interest(
        cannyed_image,
        np.array(
            [region_of_interest_vertices],
            np.int32
        ),
    )
    lines = cv2.HoughLinesP(
        cropped_image,
        rho=6,
        theta=np.pi / 60,
        threshold=160,
        lines=np.array([]),
        minLineLength=40,
        maxLineGap=25
    )
    print(lines)
    print(len(lines))

    line_image = draw_lines(yol_resmi, lines)

    left_line_x = [0, 0]
    left_line_y = [0, 0]
    right_line_x = [0, 0]
    right_line_y = [0, 0]

    for line in lines:
        for x1, y1, x2, y2 in line:
            slope = (y2 - y1) / (x2 - x1)  # <  Calculating the slope.
            if math.fabs(slope) < 0.5:  # <  Only consider extreme slope
                continue
            if slope <= 0:  # <  If the slope is negative, left group.
                left_line_x = [x1, x2]
                left_line_y = [y1, y2]
            else:  # <  Otherwise, right group.
                right_line_x = [x1, x2]
                right_line_y = [y1, y2]
    min_y = yol_resmi.shape[0] * (3 / 5)  # <  Just below the horizon
    max_y = yol_resmi.shape[0]  # <  The bottom of the image
    try:
        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))
    except np.linalg.LinAlgError as e:
        warnings.warn("There is no lines in chosen region")
        cv2.imshow('line detection', yol_resmi)
        cv2.waitKey(1)
        continue
    line_image = draw_lines(
        yol_resmi,
        [[
            [left_x_start, max_y, left_x_end, min_y],
            [right_x_start, max_y, right_x_end, min_y],
        ]],
        thickness=5,
    )

    # Convert to grayscale here.
    # gray_image = cv2.cvtColor(cropped_image, cv2.COLOR_RGB2GRAY)

    # Call Canny Edge Detection here.
    # cannyed_image = cv2.Canny(gray_image, 100, 200)

    # cv2.imshow('yol resmi', yol_resmi)
    cv2.imshow('edge detection', cropped_image)
    cv2.imshow('line detection', line_image)
    # cv2.imshow('Edge Detection', cannyed_image)

    cv2.waitKey(1)

相关问题 更多 >