0

I'm trying to target a specific area of a video so that the HoughLines don't show up on other parts of the video. I don't know how to do this but here is my code so far:

import cv2
import numpy as np
import window_names
import track_bars

frame_counter = 0

vid = 'rain.mkv'

cap = cv2.VideoCapture(vid)

while (True):
    ret, frame = cap.read()

    frame_counter += 1

    if frame_counter == cap.get(cv2.CAP_PROP_FRAME_COUNT):
        frame_counter = 0
        cap.set(cv2.CAP_PROP_POS_FRAMES, 0)

    grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    output = np.empty(grey.shape, dtype=np.uint8)

    cv2.normalize(
        grey,
        output,
        alpha=0,
        beta=255,
        norm_type=cv2.NORM_MINMAX)

    hist = cv2.equalizeHist(output)

    track_bars.lower_threshold = cv2.getTrackbarPos("lower", window_names.window_canny)
    track_bars.upper_threshold = cv2.getTrackbarPos("upper", window_names.window_canny)
    track_bars.smoothing_neighbourhood = cv2.getTrackbarPos("smoothing", window_names.window_canny)
    track_bars.sobel_size = cv2.getTrackbarPos("sobel size", window_names.window_canny)

    track_bars.smoothing_neighbourhood = max(3, track_bars.smoothing_neighbourhood)
    if not (track_bars.smoothing_neighbourhood % 2):
        track_bars.smoothing_neighbourhood = track_bars.smoothing_neighbourhood + 1

    track_bars.sobel_size = max(3, track_bars.sobel_size)
    if not (track_bars.sobel_size % 2):
        track_bars.sobel_size = track_bars.sobel_size + 1

    smoothed = cv2.GaussianBlur(
        hist, (track_bars.smoothing_neighbourhood, track_bars.smoothing_neighbourhood), 0)

    edges = cv2.Canny(
        smoothed,
        track_bars.lower_threshold,
        track_bars.upper_threshold,
        apertureSize=track_bars.sobel_size)

    rho = 1  # distance resolution in pixels of the Hough grid
    theta = np.pi / 180  # angular resolution in radians of the Hough grid
    threshold = 15  # minimum number of votes (intersections in Hough grid cell)

    min_line_length = 50  # minimum number of pixels making up a line
    max_line_gap = 20  
    line_image = np.copy(frame) * 0

    lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                        min_line_length, max_line_gap)

    if lines is not None:
        for x1, y1, x2, y2 in lines[0]:
            cv2.line(frame,(x1,y1),(x2,y2),(255,0,0),5)

    lines_edges = cv2.addWeighted(frame, 0.8, line_image, 1, 0)

    cv2.imshow(window_names.window_hough, frame)
    cv2.imshow(window_names.window_canny, edges)

    key = cv2.waitKey(27)
    if (key == ord('x')) & 0xFF:
        break

cap.release()
cv2.destroyAllWindows()

The code works perfectly fine but I just want to make it so the HoughLines don't show up on other parts and just the bit that I select.

Isaac_E
  • 21
  • 5

0 Answers0