Skip to content
Snippets Groups Projects
Select Git revision
  • 4f185e1998d823c10dcc189d68ad265e3df3b996
  • main default protected
2 results

filter_images_V1.py

Blame
  • filter_images_V1.py 5.42 KiB
    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    import time
    from datetime import datetime
    
    VIDEO = 'Videomaterial/WIN_20230414_13_41_55_Pro.mp4'
    capture = cv2.VideoCapture(VIDEO)
    
    
    last = None
    frame = None
    diff = None
    window = 'Filter'
    
    # Einstellungen
    min_threshold  = 30
    max_threshold  = 110
    img_threshold  = 100
    line_threshold = 30
    
    def overlay_imgs(base, top):
        topgray = cv2.cvtColor(top, cv2.COLOR_RGB2GRAY)
    
        _, mask = cv2.threshold(topgray, 10, 255, cv2.THRESH_BINARY)
        mask_inv = cv2.bitwise_not(mask)
        # Now black-out the area of overlay
        img1_bg = cv2.bitwise_and(base,base,mask = mask_inv)
        # Take only region of from logo image.
        img2_fg = cv2.bitwise_and(top,top,mask = mask)
    
        return cv2.add(img1_bg, img2_fg)
    
    
                
    
    def plot_points(x, y, px, py):
        fig = plt.figure()
        ax = fig.add_subplot()
        ax.scatter(px, py, c='r')
        ax.plot(x, y)
        ax.set_xlim([0, 1280])
        ax.set_ylim([720, 0])
        plt.show()
    
    def find_points(image):
        indices = np.where(image > 0)
        if (indices[1].size > 0):
            x_sort = np.sort(indices[1])
            p = np.polyfit(indices[1], indices[0], 4)
            x = np.arange(x_sort[0], x_sort[-1], 1)
            y = np.polyval(p, x)
    
            points = np.column_stack((x, y)).astype(np.int32)
            return points
           
        return None
        
    
    def filter_image(image):
        global min_threshold, max_threshold, line_threshold, result, window
        image = image.copy()
        # Use canny edge detection
        image = cv2.Canny(image, min_threshold, max_threshold)
    
        # Closing operation elliptical shaped kernels
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))                     # use --> dilation = cv2.dilate(img,kernel,iterations = 1)
        # Closing operation / closing small holes
        image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
    
        # Minimum filter: Rauschen entfernen
    
        # Closing operation elliptical shaped kernels
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))                        # use another kernal shapes 
        # perform erosion on the image
        image = cv2.erode(image, kernel)
    
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
        #image = cv2.erode(image, kernel)
    
    
        points = find_points(image)
        overlay = np.zeros((image.shape[0], image.shape[1], 3), np.uint8)
        # draw a polygon on the image
        cv2.polylines(overlay, [points], False, (255, 0, 0), thickness=3)
    
        #lines = cv2.HoughLinesP(image, cv2.HOUGH_PROBABILISTIC, np.pi/360, line_threshold, minLineLength=10, maxLineGap=20)
    
        #if lines is not None:
        #    for line in lines:
        #        x1,y1,x2,y2 = line[0]
        #        pts = np.array([[x1, y1], [x2, y2]], np.int32)
        #        #cv2.polylines(overlay, [pts], False, (255, 0, 0), thickness=1)
    
        return overlay, image
    
    def sharpenImg(image):
        # median of all the pixels under the kernel area
        blur = cv2.medianBlur(image, 5)
        # adding tow images 
        sharp = cv2.addWeighted(image, 1.5, blur, -0.5, 0.0)
        return sharp
    
    
    # def img_callback(val):
    #     global img_threshold, result
    
    #     img_threshold = val
    #     if result is not None:
    #         filter_image(result)
    
    # def max_callback(val):
    #     global max_threshold, result
    
    #     max_threshold = val
    #     if result is not None:
    #         filter_image(result)
    
    def line_callback(val):
        global line_threshold, frame, diff
    
        line_threshold = val
        if diff is not None:
            overlay = filter_image(diff)
    
            result = overlay_imgs(frame, overlay)
            cv2.imshow(window, result)
    
    def calculate_function(overlay,image):
        indices = np.where(overlay > [0])
        if((indices[0].size>0)):
            p=np.polyfit(indices[0],indices[1], 4)
            x = np.arange(0, overlay.shape[1], 1)
            y=np.polyval(p,x)
            points=np.column_stack((x,y)).astype(np.int32)
            indices = np.where(points < 0)
            points = np.delete(points,indices,axis=0)
            print(points)
            cv2.polylines(image, [points], False, (255, 0, 0), thickness=3)
            # plt.figure()
            # plt.plot(x,y)
            # plt.savefig("test.png")
            # plt.show()
            # time.sleep(20)
            # print (p)
        return overlay
        
    # To control the Size of the Disply 
    cv2.namedWindow(window, cv2.WINDOW_AUTOSIZE)
    cv2.createTrackbar('Line Threshold: ', window, line_threshold, 100, line_callback)
    
    result = None
    while capture.isOpened():
        # ret is the stat of the reading
        ret, frame = capture.read()
        if ret == True:
            frame = cv2.resize(frame, (1280, 720))
    
            filter = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)        
            filter = sharpenImg(filter)
            
            if last is not None:
                diff = cv2.absdiff(filter, last) # Difference
                overlay, image = filter_image(diff)
    
                result = overlay_imgs(frame, overlay)
                
                #function_img=calculate_function(overlay,result)
    
                cv2.imshow(window, image)
                cv2.imshow('Rsult', result)
                
            last = filter
        
            code = cv2.waitKey(33)
            if code & 0xFF == ord('s'):
                now = datetime.now()
                str = now.strftime("%d_%m_%Y_%H_%M_%S")
                cv2.imwrite(f'Filter/IMG_{str}.png', result)
    
            if code & 0xFF == ord('q'):
                break
    
        else:
            break
    
    capture.release()
    
    cv2.destroyAllWindows()