突出显示图像中显示两种效果的区域

问题描述 投票:0回答:0

使用 OpenCV,我创建了两个函数:

  1. 水平线,和
  2. 斜线,同时在源视频图像上。

下一步,在应用正确的代码后,我的目标是创建一个视频图像结果,其中几个过滤器重合的点(例如,一个水平条出现在一个圆圈内)只要效果重叠就会突出显示,而其余部分图像变暗。我的目标是在那些毫秒内清楚地看到重叠区域的存在。

什么代码可以给我那个结果?

import cv2
import numpy as np

cap = cv2.VideoCapture(r'C:\Users\Usuario\untitled\13.mp4')

# Setup for optical flow filter
ret, frame1 = cap.read()
if frame1.shape[0] > 1400:
    frame1 = cv2.resize(frame1, None, fx = 1/4, fy = 1/4)
if frame1.shape[0] > 500:
    frame1 = cv2.resize(frame1, None, fx = 1/2, fy = 1/2)
prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
hsv[...,1] = 255



# Create numpy arrays to hold the results of contour_detection and corner_detection functions
lines_edges = np.zeros_like(frame1)
output = np.zeros_like(frame1)
          
            
#HORIZONTAL LINES ####################################   
def horizontal_lines_detection():
    global lines_edges
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    kernel_size = 25 #25 (default 5)
    blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0) #0
    low_threshold = 5 #5, (default 50)
    high_threshold = 84 #84 (default 150)
    edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
    rho = 2  # distance resolution in pixels of the Hough grid 1
    theta = np.pi / 120  # angular resolution in radians of the Hough grid #180
    threshold = 25  # minimum number of votes (intersections in Hough grid cell) 15
    min_line_length = 5  # minimum number of pixels making up a line 50
    max_line_gap = 39  # maximum gap in pixels between connectable line segments 19
    line_image = np.copy(img) # * 0  # creating a blank to draw lines on

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

    if lines is not None:
        for line in lines[:3]: #7
            for x1,y1,x2,y2 in line:
                #cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),5)
                cv2.line(line_image,(x1,y1),(x2,y2),(0,0,255),2)
                #cv2.circle(line_image, (x1, y1), r-25, (255, 255, 0), 1)
        mergi = cv2.merge((imgray, line_image))
       
    cv2.imshow('img', img)
#     cv2.imshow('line_image', line_image)

    lines_edges = cv2.addWeighted(img, 0.5, line_image, 0.8, 0)
    #cv2.imshow('lines_edges', lines_edges)

    
# CIRCLE DETECTION #################################### slow on video! ###
def circle_detection():
    global output
    imgcirc = img.copy()
    gray = cv2.cvtColor(imgcirc, cv2.COLOR_BGR2GRAY)
    gray = cv2.medianBlur(gray, 5, 3) # 5, 3 is good
    output = img.copy()

    circles = cv2.HoughCircles(imgray, cv2.HOUGH_GRADIENT, 1.3, 100, maxRadius = 100, minRadius = 3) #1.2, 100, 100, 0
    #print(circles)

    # ensure at least some circles were found
    if circles is not None:
        # convert the (x, y) coordinates and radius of the circles to integers
        circles = np.round(circles[0, :]).astype("int")

        # loop over the (x, y) coordinates and radius of the circles
        for (x, y, r) in circles[:2]:
            # draw the circle in the output image, then draw a rectangle
            # corresponding to the center of the circle
            #cv2.circle(output, (x, y), r, (0, 255, 0), 2)
            cv2.circle(output, (x, y), int(r*0.75), (0, 0, 0), 2)
            cv2.rectangle(output, (x - 3, y - 3), (x + 3, y + 3), (255, 255, 255), 1)
            cv2.rectangle(output, (x, y), (x, y), (0, 128, 255), -1)
            cv2.rectangle(output, (x - 10, y - 10), (x + 10, y + 10), (0, 128, 255), 1)
            #cv2.imshow('output', output)

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

    if img.shape[0] > 1400:
        img = cv2.resize(img, None, fx = 1/4, fy = 1/4)
    if img.shape[0] > 500:
        img = cv2.resize(img, None, fx = 1/2, fy = 1/2)
      
    imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # Call functions
    horizontal_lines_detection()
    circle_detection()
    
    
    # Draw contours and corners onto the original video frame
    #img_with_contours = cv2.addWeighted(img, 0.5, imgc, 0.5, 0)
    img_with_corners = cv2.addWeighted(img, 0.9, imgcorn, 0.9, 0)
    img_with_lines = cv2.addWeighted(img_with_corners, 0.5, lines_edges, 0.9, 0)
    img_with_lines_circles = cv2.addWeighted(img_with_lines, 0.9, output, 0.3, 0)
    img_with_lines_circles_optf = cv2.addWeighted(img_with_lines_circles, 0.9, opticalf_img, 0.6, 0)

#    img_with_lines_circles1 = cv2.subtract(img_with_lines, output)
    img_with_lines_output_add = img_with_lines - output
    img_with_lines_circles_optf = cv2.addWeighted(img_with_lines_output_add, 0.7, opticalf_img, 0.9, 0)
    
    # Display the modified frame
    cv2.imshow('img_with_corners', img_with_corners)
    cv2.imshow('img_with_lines', img_with_lines)
    cv2.imshow('img_with_lines_circles', img_with_lines_circles)
    cv2.imshow('img_with_lines_output_add', img_with_lines_output_add)
    cv2.imshow('img_with_lines_circles_optf', img_with_lines_circles_optf)
       
    key = cv2.waitKey(25) #controls speed of video frame in ms
    if key == 27:
        break
 
cap.release()
cv2.destroyAllWindows()```
python numpy opencv computer-vision cv2
© www.soinside.com 2019 - 2024. All rights reserved.