Trouble detecting vertical red line in video

asked 2020-08-17 12:01:02 -0600

TimK421 gravatar image

updated 2020-08-17 13:06:06 -0600

Hi all,

I hope your week is going well. I'm having difficulty detecting a vertical red line from a captured high-speed video.

Here is my "red-line-detection" script:

import cv2
import numpy as np
import glob
import os
from datetime import datetime

#######################
# CONSTANTS
#######################
# BGR limits for different colors
LOWER_RED = np.array([0, 0, 50])
UPPER_RED = np.array([30, 40, 200])

# we run at 120Hz frame rate
FRAME_MS = 8.33333333

# window distances to detect video cue
BLIP_NEAR = 5
BLIP_FAR = 15

#######################
# MAIN PROGRAM
#######################

# process all filenames ending in .mp4
for fname in glob.glob("*.mp4"):
    print("Processing " + fname)

    # capture the video
    vidcap = cv2.VideoCapture(fname)

    # process each frame
    while(vidcap.isOpened()):
        # capture each frame
        ret, frame = vidcap.read()
        if ret == True:
            # Find different colors in image
            redshape = cv2.inRange(frame, LOWER_RED, UPPER_RED)

            # find primary vertical red line
            lines = cv2.HoughLinesP(redshape,2,np.pi/180,50,
                                    minLineLength=350,maxLineGap=200)
            if lines is not None:
                #display line on image
                for x1,y1,x2,y2 in lines[0]:
                    cv2.line(frame,(x1,y1),(x2,y2),(255,255,255),1)
                # search for a blip of cyan pixels to the left of this line
                mid_y = int((y2+y1)/2) # determine halfway vertical point on line
                cv2.rectangle(frame,(x1-BLIP_FAR,y1),(x1-BLIP_NEAR,mid_y),(255,255,255),1)

            # show results
            cv2.imshow("Composite", frame)
            if cv2.waitKey(100) & 0xFF == ord('q'):
                break
        else:
            # break out of loop
            break

    # close input video file
    vidcap.release()

# pause to see final screen
#cv2.waitKey(5000)

# erase all windows
cv2.destroyAllWindows()

Here are some sample images captured from the video I'm processing:

https://www.dropbox.com/s/ttwwhv2sed9...

https://www.dropbox.com/s/ji1v9e6624d...

https://www.dropbox.com/s/5opgropardt...

https://www.dropbox.com/s/btofi6veabe...

If you have the time to download a huge (410MB) video file, you can get the script and video here: https://www.dropbox.com/s/vde4dvi73h3...

The algorithm basically looks for red pixels and then applies HoughLines to detect the lines on the screen. My problem is that the detection algorithm jumps around a lot and I'm trying to smooth it out to read the cyan blips (captured inside the white rectangle). I've added a 100ms delay between frames so you can see what's going on, but you can remove the delay to make it faster.

Any suggestions for how to stop the line detection from jumping around erratically?

Thanks for your time, Tim

edit retag flag offensive close merge delete

Comments

sorry to say so, but noone will download half a gigabyte from a dropbox to find out about your problem

please put your code (and maybe some small example images) here

berak gravatar imageberak ( 2020-08-17 12:11:09 -0600 )edit

Thanks for the feedback. I hope my edits have addressed your concerns. Tim

TimK421 gravatar imageTimK421 ( 2020-08-17 13:07:25 -0600 )edit

what do you not understand about "put things here" ?

berak gravatar imageberak ( 2020-08-17 14:16:29 -0600 )edit

I am also disinclined to run this. please post a screenshot of an unsatisfactory (wrong) detection. your thresholds for inRange may need tweaking, or maybe houghLinesP isn't suitable. have you done imshow() on 'redshape' and observed it?

crackwitz gravatar imagecrackwitz ( 2020-08-17 20:58:40 -0600 )edit

@TimK421. Do you want rectangle and line stay enacted w/out jumpy up and down?. I got 98%. Actually, I don't understand why rectangle erased cyan spike and created new location cyan spike.

supra56 gravatar imagesupra56 ( 2020-08-17 22:11:26 -0600 )edit