Skip to content
Snippets Groups Projects
Commit ac26e0f9 authored by Mohammad Khaleeliyeh's avatar Mohammad Khaleeliyeh
Browse files

stabiles Verhalten

parent 1e2c7c68
Branches
Tags
No related merge requests found
......@@ -69,15 +69,15 @@ def filter_image(image):
image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
# Minimum filter: Rauschen entfernen
<<<<<<< HEAD
# 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)
>>>>>>> 15c1ee4b8b800ce409ac06c9128e25df62715fd1
points = find_points(image)
overlay = np.zeros((image.shape[0], image.shape[1], 3), np.uint8)
......@@ -168,6 +168,7 @@ while capture.isOpened():
#function_img=calculate_function(overlay,result)
cv2.imshow(window, image)
cv2.imshow('Rsult', result)
last = filter
......
......@@ -28,7 +28,18 @@ def overlay_imgs(base, top):
img2_fg = cv2.bitwise_and(top,top,mask = mask)
return cv2.add(img1_bg, img2_fg)
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()
......@@ -40,9 +51,9 @@ def filter_image(image):
image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
#lines = cv2.HoughLinesP(image, cv2.HOUGH_PROBABILISTIC, np.pi/360, line_threshold, minLineLength=10, maxLineGap=20)
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=2)
#if lines is not None:
# for line in lines:
# x1,y1,x2,y2 = line[0]
......@@ -102,7 +113,8 @@ while capture.isOpened():
result = overlay_imgs(frame, overlay)
cv2.imshow(window, image)
cv2.imshow('overlay', overlay)
cv2.imshow('result', result)
code = cv2.waitKey(33)
if code & 0xFF == ord('s'):
......
......@@ -93,7 +93,7 @@ def filter_image(image):
cv2.polylines(overlay, [points], False, (255, 0, 0), thickness=7)
return overlay
return overlay, image
def sharpenImg(image):
# median of all the pixels under the kernel area
......@@ -149,12 +149,12 @@ while capture.isOpened():
if last is not None:
diff = cv2.absdiff(filter, last) # Difference
overlay = filter_image(diff)
overlay, image = filter_image(diff)
result = overlay_imgs(frame, overlay)
#function_img=calculate_function(overlay,result)
# cv2.imshow(window, image)
cv2.imshow(window, result)
last = filter
......
import cv2
import numpy as np
import matplotlib.pyplot as plt
import time
from datetime import datetime
import pandas as pd
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
spanne = 2
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_so = indices[1]
y_so = indices[0]
list_xy = np.column_stack((x_so, y_so)).astype(np.int32)
# list_xy = np.sort(list_xy, axis=0)
# print( list_xy)
df = pd.DataFrame(list_xy,columns=['x','y'])
df = df.sort_values(by=['x'], ascending=True)
n_list = []
for el in df.x.unique():
med = (df.y.where(df.x >= el-spanne).where(el+spanne >= df.x)).median()
n_list.append([el,med])
n_list = np.array(n_list)
p = np.polyfit(n_list[:,0], n_list[:,1], 6)
x = np.arange(n_list[:,0][0], n_list[:,0][-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_ELLIPSE, (1,1))
# Opening operation
image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
# perform erosion on the image
image = cv2.erode(image, (3,3))
# Closing operation / closing small holes
# image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, (1,1))
image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, (5,5))
# # image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, (4,4))
# dilation
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2,2))
image = cv2.dilate(image,kernel2,iterations = 1)
# 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=6)
return overlay, image
def sharpenImg(image):
# median of all the pixels under the kernel area
blur = cv2.medianBlur(image, 7)
# 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)
# 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(frame)
if last is not None:
diff = cv2.absdiff(filter, last) # Difference
cv2.imshow("diff", diff)
overlay, image = filter_image(diff)
result = overlay_imgs(frame, overlay)
cv2.imshow("image", image)
cv2.imshow("result", result)
# time.sleep(0.1)
last = filter
code = cv2.waitKey(10)
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
import cv2
import numpy as np
import matplotlib.pyplot as plt
import time
from datetime import datetime
import pandas as pd
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
spanne = 2
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_so = indices[1]
y_so = indices[0]
list_xy = np.column_stack((x_so, y_so)).astype(np.int32)
# list_xy = np.sort(list_xy, axis=0)
# print( list_xy)
df = pd.DataFrame(list_xy,columns=['x','y'])
df = df.sort_values(by=['x'], ascending=True)
n_list = []
df_un = df.x.unique()
for el in df_un[::2]:
med = (df.y.where(df.x >= el-spanne).where(el+spanne >= df.x)).median()
n_list.append([el,med])
n_list = np.array(n_list)
p = np.polyfit(n_list[:,0], n_list[:,1], 6)
x = np.arange(n_list[:,0][0], n_list[:,0][-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_ELLIPSE, (1,1))
# Opening operation
image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
# perform erosion on the image
image = cv2.erode(image, (3,3))
# Closing operation / closing small holes
# image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, (1,1))
image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, (5,5))
# # image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, (4,4))
# dilation
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2,2))
image = cv2.dilate(image,kernel2,iterations = 1)
# 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=6)
return overlay, image
def sharpenImg(image):
# median of all the pixels under the kernel area
blur = cv2.medianBlur(image, 7)
# 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)
# 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(frame)
if last is not None:
diff = cv2.absdiff(filter, last) # Difference
cv2.imshow("diff", diff)
overlay, image = filter_image(diff)
result = overlay_imgs(frame, overlay)
cv2.imshow("image", image)
cv2.imshow("result", result)
# time.sleep(0.1)
last = filter
code = cv2.waitKey(10)
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment