From 1e7e12b3d1c859a310a58b79ecbd5eb40d68a4f3 Mon Sep 17 00:00:00 2001
From: Mohammad Khaleeliyeh <mohammad.khaleeliyeh@asinco.de>
Date: Fri, 9 Jun 2023 18:28:58 +0200
Subject: [PATCH] Code modified

---
 filter_images_V5.py |   2 +-
 filter_pipeline.py  |  23 ++++++---
 filters.py          | 114 ++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 128 insertions(+), 11 deletions(-)

diff --git a/filter_images_V5.py b/filter_images_V5.py
index 798d80d..b8f850f 100644
--- a/filter_images_V5.py
+++ b/filter_images_V5.py
@@ -5,7 +5,7 @@ import time
 from datetime import datetime
 import pandas as pd
 
-VIDEO = 'Videomaterial/WIN_20230414_13_41_55_Pro.mp4'
+VIDEO = 'Videomaterial/WIN_20230602_14_42_19_Pro.mp4'
 capture = cv2.VideoCapture(VIDEO)
 
 
diff --git a/filter_pipeline.py b/filter_pipeline.py
index 1c22f6f..8dae246 100644
--- a/filter_pipeline.py
+++ b/filter_pipeline.py
@@ -4,10 +4,11 @@ import matplotlib.pyplot as plt
 import time
 from datetime import datetime
 import pandas as pd
+import filters
 
-VIDEO = 'Videomaterial/WIN_20230414_13_41_55_Pro.mp4'
+VIDEO = 'Videomaterial/WIN_20230602_14_45_52_Pro.mp4'
 capture = cv2.VideoCapture(VIDEO)
-capture.set(cv2.CAP_PROP_POS_FRAMES, 30 * 40) # skip 40 seconds
+capture.set(cv2.CAP_PROP_POS_FRAMES, 30 * 0) # skip 40 seconds
 
 last = None
 frame = None
@@ -34,23 +35,29 @@ def overlay_imgs(base, top):
 
     return cv2.add(img1_bg, img2_fg)
 
+
 def nothing_cb(val):
     pass
 
 # To control the Size of the Disply 
-cv2.namedWindow(window, cv2.WINDOW_AUTOSIZE)
-cv2.createTrackbar('Canny_Min', window, 50, 255, nothing_cb)
-cv2.createTrackbar('Canny_Max', window, 100, 255, nothing_cb)
+cv2.namedWindow(window, cv2.WINDOW_NORMAL)
+cv2.createTrackbar('Canny_Min', window, 90, 255, nothing_cb)
+cv2.createTrackbar('Canny_Max', window, 150, 255, nothing_cb)
 cv2.createTrackbar('Diff_Mix', window, 40, 100, nothing_cb)
+cv2.createTrackbar('HSV_Min', window, 40, 255, nothing_cb)
+cv2.createTrackbar('HSV_Max', window, 100, 255, nothing_cb)
+
 
-import filters
 pipeline = [
     ('Original', filters.none),
+    #('GreenChannel', filters.greenfilter),
     ('Gray scale', filters.grayscale),
     ('Contrast and Blur', filters.medianBlur),
     ('Video Diff Multiple', filters.video_absdiff2),
+    ('Green filter abs', filters.green_absfilter),
     ('Canny edge detection', filters.filter_canny),
     ('Morph close', filters.filter_close),
+    ('gitter_filter', filters.gitter),
     #('Morph open', filters.filter_open),
     ('Point extraction', filters.points_extract),
     ('Polyfit lines', filters.points_overlay)
@@ -59,7 +66,7 @@ pipeline = [
 state = {} # Empty dictionary to store filters state
 
 info = {
-    'abs_diff': 3,       # 3 images for difference,
+    'abs_diff': 2,       # 3 images for difference,
     'dim': (1920, 1080), #
     'params': {}
 }
@@ -76,6 +83,8 @@ while capture.isOpened():
         info['params']['mix'] = cv2.getTrackbarPos('Diff_Mix', window) / 100.0
         info['params']['canny_min'] = cv2.getTrackbarPos('Canny_Min', window)
         info['params']['canny_max'] = cv2.getTrackbarPos('Canny_Max', window)
+        info['params']['hsv_min'] = cv2.getTrackbarPos('HSV_Min', window)
+        info['params']['hsv_max'] = cv2.getTrackbarPos('HSV_Max', window)
 
         result = frame
         for i, (name, filter) in enumerate(pipeline):
diff --git a/filters.py b/filters.py
index 35e4929..d743771 100644
--- a/filters.py
+++ b/filters.py
@@ -1,13 +1,110 @@
 import cv2 as cv
 import numpy as np
 import pandas as pd
-
+import matplotlib.pyplot as plt 
 from collections import deque
 
 
 def none(info, image, state):
+    if 'bufferOrginal' not in state:
+        state['bufferOrginal'] = deque(maxlen=info['abs_diff'])
+
+    bufferOrginal = state['bufferOrginal']
+    bufferOrginal.append(image)
+    
+    return image, False
+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 gitter(info, image, state):
+    list_of_lines = []
+    # image Range ((1280, 720))
+    fig = plt.figure()
+    ax = fig.add_subplot()
+    ax.set_xlim([0, 1280])
+    ax.set_ylim([720, 0])
+
+    ax.grid(True, which='major')
+    diff_x = 50
+    diff_y = 50
+    treshold = 50
+
+    ax.set_xticks(range(0,1280, diff_x))
+    ax.set_yticks(range(0,720, diff_y))
+
+    for x in range(0,1280,diff_x):
+        for y in range(0,720,diff_y):
+            subimg = image[y:y+diff_y,x:x+diff_x]
+            indices = np.where(subimg > 0)
+            if (indices[1].size > treshold):
+                x_so = indices[1] + x
+                y_so = indices[0] + y
+
+                list_xy = np.column_stack((x_so, y_so)).astype(np.int32)
+                poly_xy = polyfit2(list_xy, x, x + diff_x, y, y + diff_y)
+                list_of_lines.append(poly_xy)
+
+                ax.scatter(x_so, y_so, c='r')
+                ax.plot(poly_xy[:,0], poly_xy[:,1], 'b')
+
+                #fig.show()
+                
+                #plot_points(poly_xy[:,0], poly_xy[:,1], x_so, y_so)
+
+    plt.show()
     return image, False
 
+# Cut green kanal 
+def greenfilter(info, image, state):
+    # Umwandeln des Bildes in den Farbraum HSV
+    hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
+    # Definition des grünen Farbbereichs in HSV
+    lower_green = np.array([40, 10, 10])
+    upper_green = np.array([80, 240, 240])
+    # Erstellen einer Maske, die den grünen Farbbereich ausschließt
+    mask = cv.inRange(hsv, lower_green, upper_green)
+    # Invertieren der Maske, um den grünen Farbbereich auszuschließen
+    inverted_mask = cv.bitwise_not(mask)
+    # Anwenden der Maske auf das Bild
+    res = cv.bitwise_and(image, image, mask=inverted_mask)
+    return res,False
+
+def greenfilter_mask(image, info):
+    min_threshold = info['params']['hsv_min']
+    max_threshold = info['params']['hsv_max']
+
+    # Umwandeln des Bildes in den Farbraum HSV
+    hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
+    # Definition des grünen Farbbereichs in HSV
+    lower_green = np.array([min_threshold, 10, 10])
+    upper_green = np.array([max_threshold, 240, 240])
+    # Erstellen einer Maske, die den grünen Farbbereich ausschließt
+    mask = cv.inRange(hsv, lower_green, upper_green)
+    return mask
+
+def green_absfilter(info, image, state):
+    bufferOrginal = state['bufferOrginal']
+    
+    res = None
+    for img in bufferOrginal:
+        mask = greenfilter_mask(img, info)
+        if res is None:
+           res = mask
+        else:
+            res = cv.bitwise_or(res, mask)
+    
+    res_inv = cv.bitwise_not(res)
+    res_img = cv.bitwise_and(image, image, mask=res_inv)
+    return res_img,False
+
+
+
 # Resize image to 1280x720 pixels
 def resize(info, image, state):
     res = cv.resize(image, (1280, 720))
@@ -64,11 +161,22 @@ def find_points(image):
         return n_list
 
     return None
+
+def polyfit2(n_list, xs, xe, ys, ye):
+    if n_list is not None:
+        p = np.polyfit(n_list[:,0], n_list[:,1], 1)
+        x = np.arange(xs, xe, 1)
+        y = np.polyval(p,x)
+        points = np.column_stack((x, y)).astype(np.int32)
+        points = points[(y >= ys) & (y <= ye)]
+
+        return points 
     
+    return None
 
-def polyfit(n_list):
+def polyfit(n_list,n=4):
     if n_list is not None:
-        p = np.polyfit(n_list[:,0], n_list[:,1], 6)
+        p = np.polyfit(n_list[:,0], n_list[:,1], n)
         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)
-- 
GitLab