diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..ad61a4e62384c2f4bda3ecc95eb7e2695645a508 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Videos NICHT hochladen +/Videomaterial \ No newline at end of file diff --git a/Filter/IMG_28_04_2023_15_27_08.png b/Filter/IMG_28_04_2023_15_27_08.png new file mode 100644 index 0000000000000000000000000000000000000000..b1b8604463c469dff3aa7025100321333172f8ac Binary files /dev/null and b/Filter/IMG_28_04_2023_15_27_08.png differ diff --git a/filter_images.py b/filter_images.py new file mode 100644 index 0000000000000000000000000000000000000000..fb6dc5a86aca39208479eaefa4be06a0e2562b00 --- /dev/null +++ b/filter_images.py @@ -0,0 +1,114 @@ +import cv2 +import numpy as np +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 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) + + # kernel = np.ones((5,5),np.uint8) + kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5)) + image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel) + + lines = cv2.HoughLinesP(image, cv2.HOUGH_PROBABILISTIC, np.pi/360, line_threshold, minLineLength=10, maxLineGap=20) + + overlay = np.zeros((image.shape[0], image.shape[1], 3), np.uint8) + + 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=3) + + return overlay + +# 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) + + +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)) + + filter = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) + + #frame = frame[:,:,2] # Blue Channel + filter = cv2.medianBlur(filter, 3) + if last is not None: + diff = cv2.absdiff(filter, last) # Difference + overlay = filter_image(diff) + + result = overlay_imgs(frame, overlay) + 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 diff --git a/filter_images_V1.py b/filter_images_V1.py new file mode 100644 index 0000000000000000000000000000000000000000..c143801e209a713fc0a995e73691ae496c90456a --- /dev/null +++ b/filter_images_V1.py @@ -0,0 +1,175 @@ +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 + kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5)) + image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel) + + # Minimum filter: Rauschen entfernen + 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) + 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 + +def sharpenImg(image): + blur = cv2.medianBlur(image, 5) + 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 + + +cv2.namedWindow(window, cv2.WINDOW_AUTOSIZE) +cv2.createTrackbar('Line Threshold: ', window, line_threshold, 100, line_callback) + +result = None +while capture.isOpened(): + + ret, frame = capture.read() + if ret == True: + frame = cv2.resize(frame, (1280, 720)) + + 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) + + 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 diff --git a/test.txt b/test.txt deleted file mode 100644 index 28cf67640e502fe8e879a863bd1bbcd4366689e8..0000000000000000000000000000000000000000 --- a/test.txt +++ /dev/null @@ -1 +0,0 @@ -Hallo Welt!