使用opencv测量两个元素之间的距离

时间:2019-09-10 10:52:13

标签: python opencv

我从汽车拍摄了视频。我的程序是测量前轮与道路白线之间的距离。该脚本对于左侧视频和右侧视频运行良好。

但是有时它会测量前轮和右侧白线之间的距离错误。

thresh = 150
distance_of_wood_plank = 80
pixel_of_wood_plank = 150
origin_width = 0
origin_height = 0
wheel_x = 0; wheel_y = 0 #xpoint and ypoint of wheel

df = pandas.DataFrame(columns=["Frame_No", "Distance", "TimeStrap"])
cap = cv2.VideoCapture(args.video)
frame_count = 0;
while(cap.isOpened()): #Reading input video by VideoCapture of Opencv
    try:
        frame_count += 1
        ret, source = cap.read() # get frame from video
        origin_height, origin_width, channels = source.shape

        timestamps = [cap.get(cv2.CAP_PROP_POS_MSEC)]
        milisecond = int(timestamps[0]) / 1000
        current_time = str(datetime.timedelta(seconds = milisecond))
        cv2.waitKey(1)
        grayImage = cv2.cvtColor(source, cv2.COLOR_RGB2GRAY) # get gray image
        crop_y = int(origin_height / 3 * 2) - 30
        crop_img = grayImage[crop_y:crop_y + 100, 0:0 + origin_width] # get interest area
        blur_image = cv2.blur(crop_img,(3,3))
        ret, th_wheel = cv2.threshold(blur_image, 10, 255, cv2.THRESH_BINARY) #get only wheel
        ret, th_line = cv2.threshold(blur_image, 150, 255, cv2.THRESH_BINARY) #get only white line
        contours, hierarchy = cv2.findContours(th_wheel, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2:]
        # get xpoint and ypoint of wheel
        for cnt in contours:
            x, y, w, h = cv2.boundingRect(cnt)
            if (x < origin_width/ 4):
                continue
            elif (w < 10):
                continue
            elif (w > 80):
                continue
            elif (x > origin_width / 4 * 3):
                continue
            wheel_x = int(x)
            wheel_y = int(y + h / 2 - 8)
        pixel_count = 0 # count of pixel between wheel and white line
        # get distance between wheel and white line
        if (wheel_x > origin_width/2):
            wheel_x -= 7
            for i in range(wheel_x, 0, -1):
                pixel_count += 1
                suit_point = th_line[wheel_y,i]
                if (suit_point == 255):
                    break
                if (i == 1):
                    pixel_count = 0
            pixel_count -= 4
            cv2.line(source, (wheel_x - pixel_count, wheel_y + crop_y), (wheel_x, wheel_y + crop_y), (255, 0, 0), 2)
        else :
            wheel_x += 7
            for i in range(wheel_x , origin_width):
                pixel_count += 1
                suit_point = th_line[wheel_y,i]
                if (suit_point == 255):
                    break
                if (i == origin_width - 1):
                    pixel_count = 0
            pixel_count += 4
            cv2.line(source, (wheel_x, wheel_y + crop_y), (wheel_x + pixel_count, wheel_y + crop_y), (255, 0, 0), 2)
        distance_Cm = int(pixel_count * 80 / pixel_of_wood_plank)
        str_distance = ""
        if distance_Cm > 10:
            str_distance = str(distance_Cm) + "Cm"
        else:
            str_distance = "No white line"

        cv2.putText(source, str_distance, (50, 250), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)

        df = df.append({'Frame_No': frame_count,'Distance': str_distance ,'TimeStrap': current_time}, ignore_index = True)

        df.to_csv("result.csv")
        cv2.imshow("Distance_window", source)
        cv2.waitKey(1)
    except:
        pass

这是视频的链接-https://drive.google.com/file/d/1IjJ-FA2LTGv8Cz-ReL7fFI7HPTiEhyxF/view?usp=sharing

1 个答案:

答案 0 :(得分:1)

实际上,您在测量轮胎与白线之间的距离方面做得非常好。您需要考虑的是样品中有多少噪音。除非您停下卡车,下车,然后用胶带测量从轮胎到生产线的距离,否则您将永远无法真正知道轮胎的距离。您还需要考虑到(除非撞毁卡车)轮胎到白线的距离在每帧之间的变化不会超过几个像素。

最好的解决方案是卡尔曼滤波器,但这非常复杂。我使用了一个更简单的解决方案。为了找到行的位置,我对最后四个值取平均值以减少噪音。

average

import numpy as np, cv2

thresh = 150
distance_of_wood_plank = 80
pixel_of_wood_plank = 150
origin_width = 0
origin_height = 0
wheel_x = 0; wheel_y = 0 #xpoint and ypoint of wheel

cap = cv2.VideoCapture('/home/stephen/Desktop/20180301 1100 VW Right.mp4')
frame_count = 0;
vid_writer = cv2.VideoWriter('/home/stephen/Desktop/writer.avi', cv2.VideoWriter_fourcc('M','J','P','G'), 30, (480,360))

positions = []

import math
def distance(a,b): return math.sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2)

while(cap.isOpened()): #Reading input video by VideoCapture of Opencv
    frame_count += 1
    ret, source = cap.read() # get frame from video
    origin_height, origin_width, channels = source.shape
    grayImage = cv2.cvtColor(source, cv2.COLOR_RGB2GRAY) # get gray image
    crop_y = int(origin_height / 3 * 2) - 30
    crop_img = grayImage[crop_y:crop_y + 100, 0:0 + origin_width] # get interest area
    blur_image = cv2.blur(crop_img,(3,3))
    ret, th_wheel = cv2.threshold(blur_image, 10, 255, cv2.THRESH_BINARY) #get only wheel
    ret, th_line = cv2.threshold(blur_image, 150, 255, cv2.THRESH_BINARY) #get only white line
    contours, hierarchy = cv2.findContours(th_wheel, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2:]
    # get xpoint and ypoint of wheel
    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        if (x < origin_width/ 4):
            continue
        elif (w < 10):
            continue
        elif (w > 80):
            continue
        elif (x > origin_width / 4 * 3):
            continue
        wheel_x = int(x)
        wheel_y = int(y + h / 2 - 8)
    pixel_count = 0 # count of pixel between wheel and white line
    # get distance between wheel and white line
    if (wheel_x > origin_width/2):
        wheel_x -= 7
        for i in range(wheel_x, 0, -1):
            pixel_count += 1
            suit_point = th_line[wheel_y,i]
            if (suit_point == 255):
                break
            if (i == 1):
                pixel_count = 0
        pixel_count -= 4
    else :
        wheel_x += 7
        for i in range(wheel_x , origin_width):
            pixel_count += 1
            suit_point = th_line[wheel_y,i]
            if (suit_point == 255):
                break
            if (i == origin_width - 1):
                pixel_count = 0
        pixel_count += 4
        a,b = (wheel_x - pixel_count, wheel_y + crop_y), (wheel_x, wheel_y + crop_y)
        if distance(a,b)>10: positions.append((wheel_x + pixel_count, wheel_y + crop_y))

    if len(positions)>10:
        radius = 2
        for position in positions[-10:]:
            radius += 2
            center = tuple(np.array(position, int))
            color = 255,255,0
            cv2.circle(source, center, radius, color, -1)
        x,y = zip(*positions[-4:])
        xa, ya = np.average(x), np.average(y)
        center = int(xa), int(ya)
        cv2.circle(source, center, 20, (0,0,255), 10)

    cv2.imshow("Distance_window", source)
    vid_writer.write(cv2.resize(source, (480,360)))
    k = cv2.waitKey(1)
    if k == 27: break

cv2.destroyAllWindows()