diff --git a/filter_images.py b/filter_images.py index fb6dc5a86aca39208479eaefa4be06a0e2562b00..348940186e882418603ed7dc5aabcc6c1b9522fd 100644 --- a/filter_images.py +++ b/filter_images.py @@ -79,7 +79,6 @@ cv2.namedWindow(window, cv2.WINDOW_AUTOSIZE) cv2.createTrackbar('Line Threshold: ', window, line_threshold, 100, line_callback) while capture.isOpened(): - ret, frame = capture.read() if ret == True: frame = cv2.resize(frame, (1280, 720)) diff --git a/filter_images_V1.py b/filter_images_V1.py index 39314aaaf182b92dbd2ea05c584df98fc724e5d5..29e006c36404b668d61df3eee3d7c4047289acf9 100644 --- a/filter_images_V1.py +++ b/filter_images_V1.py @@ -14,9 +14,9 @@ diff = None window = 'Filter' # Einstellungen -min_threshold = 30 -max_threshold = 110 -img_threshold = 100 +min_threshold = 30 +max_threshold = 110 +img_threshold = 100 line_threshold = 30 def overlay_imgs(base, top): @@ -31,6 +31,9 @@ def overlay_imgs(base, top): return cv2.add(img1_bg, img2_fg) + + + def plot_points(x, y, px, py): fig = plt.figure() ax = fig.add_subplot() @@ -60,16 +63,20 @@ def filter_image(image): # Use canny edge detection image = cv2.Canny(image, min_threshold, max_threshold) - # Closing operation - kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5)) + # 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 - kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3)) + # 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) 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) @@ -83,7 +90,9 @@ def filter_image(image): return overlay 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 @@ -131,13 +140,13 @@ def calculate_function(overlay,image): # 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)) @@ -145,8 +154,6 @@ while capture.isOpened(): filter = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) filter = sharpenImg(filter) - #cv2.imshow(window, filter) - if last is not None: diff = cv2.absdiff(filter, last) # Difference overlay = filter_image(diff) diff --git a/filter_images_V2.py b/filter_images_V2.py new file mode 100644 index 0000000000000000000000000000000000000000..f1a25c399dc6eec76e603d48fa5d5b8097d0e1cd --- /dev/null +++ b/filter_images_V2.py @@ -0,0 +1,176 @@ +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() + + + # construct a rectangular kernel from the current size / rect shaped kernel + kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3)) + + + # Opening operation + image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel) + + + # Closing operation / closing small holes + image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel) + # Closing operation / closing small holes + image = cv2.morphologyEx(image, cv2.MORPH_GRADIENT, kernel) + + + # dilation + kernel2 = np.ones((3,3),np.uint8) + image = cv2.dilate(image,kernel2,iterations = 3) + + + # perform erosion on the image + image = cv2.erode(image, kernel) + + # Use canny edge detection + image = cv2.Canny(image, min_threshold, max_threshold) + + 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=7) + + + return overlay + +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 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 = filter_image(diff) + + result = overlay_imgs(frame, overlay) + + #function_img=calculate_function(overlay,result) + + cv2.imshow(window, 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() \ No newline at end of file