From b11e5057bae461476c4392482e23dee9f38c0599 Mon Sep 17 00:00:00 2001
From: wangzhibo <wangzhibo@shsening.com>
Date: 星期四, 24 三月 2022 09:53:58 +0800
Subject: [PATCH] 识别宽的情况 人工拆分

---
 debug/2_split_digit_from_video.py |   26 ++++++++++++++++----------
 1 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/debug/2_split_digit_from_video.py b/debug/2_split_digit_from_video.py
index cbadf7c..653bb59 100644
--- a/debug/2_split_digit_from_video.py
+++ b/debug/2_split_digit_from_video.py
@@ -4,30 +4,36 @@
 from cv2 import waitKey
 import uuid
 from _image_clip_setting import image_clip
-
+import sys
 
 def save_images(frame):
     image = frame[image_clip[0]:image_clip[1], image_clip[2]:image_clip[3]]
     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
     thresh = cv2.threshold(gray, 0, 255,
                            cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
-    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
+    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (6, 6))
     counter = cv2.morphologyEx(thresh.copy(), cv2.MORPH_OPEN, kernel)
+    image_h = image.shape[0]
+    image_w = image.shape[1]
     contours, hierarchy = cv2.findContours(
         counter, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
     contours_count = 0
-    #print(contours)
-    print(hierarchy)
+    return_numbers = []
     for c in contours:
         x, y, w, h = cv2.boundingRect(c)
-        print((x, y, w, h))
-        print(hierarchy[0][contours_count][3])
-        if hierarchy[0][contours_count][3] ==0 :
-            #cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
-            cv2.imwrite("./images52/"+str(uuid.uuid4())+".jpg",cv2.threshold(cv2.resize(gray[y:y+h,x:x+w],(20,30)), 0, 255,
+        if hierarchy[0][contours_count][3] == 0 and h > image_h * 0.3:
+            if w > image_w/4:
+                half_width = int(w/2)
+                cv2.imwrite("./images52/"+str(uuid.uuid4())+".jpg",cv2.threshold(cv2.resize(gray[y:y+h,x:x+half_width],(20,30)), 0, 255,
                        cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
+                cv2.imwrite("./images52/"+str(uuid.uuid4())+".jpg",cv2.threshold(cv2.resize(gray[y:y+h,x+half_width:x+w],(20,30)), 0, 255,
+                       cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
+            else:
+                cv2.imwrite("./images52/"+str(uuid.uuid4())+".jpg",cv2.threshold(cv2.resize(gray[y:y+h,x:x+w],(20,30)), 0, 255,
+                       cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
+        contours_count += 1
         
-cap = cv2.VideoCapture("new.mp4")  # for testing
+cap = cv2.VideoCapture(sys.argv[1])  # for testing
 while(cap.isOpened()):
     ret, frame = cap.read()
     if ret==True:

--
Gitblit v1.9.1