Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Trouble detecting vertical red line in video

Hi all,

I hope your week is going well. I'm having difficulty detecting a vertical red line from a captured high-speed video. You can download the video and my existing script here: (410MB) https://www.dropbox.com/s/vde4dvi73h3rb8q/FindRedLine.zip?dl=0

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

Trouble detecting vertical red line in video

Hi all,

I hope your week is going well. I'm having difficulty detecting a vertical red line from a captured high-speed video. You 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/ttwwhv2sed91eqd/scene00001.png?dl=0

https://www.dropbox.com/s/ji1v9e6624dicx8/scene00151.png?dl=0

https://www.dropbox.com/s/5opgropardti467/scene00201.png?dl=0

https://www.dropbox.com/s/btofi6veaberb8i/scene00251.png?dl=0

If you have the time to download a huge (410MB) video file, you can download the get the script and video and my existing script here: (410MB) here: https://www.dropbox.com/s/vde4dvi73h3rb8q/FindRedLine.zip?dl=0

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