Select Git revision
Forked from
Peter Gerwinski / hp
Source project has a limited visibility.
filter_images.py 3.08 KiB
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()