Last active
August 24, 2022 20:35
-
-
Save welmends/8be70e26a85db10be3d169c55b24d7b6 to your computer and use it in GitHub Desktop.
Find Signature in Image (3 one above the other)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
from collections import Counter | |
import matplotlib.pyplot as plt | |
def check_countour(shape, contour): | |
# Get array X: | |
arr = list(dict(sorted(Counter(contour[:, 0, 0]).items())).values()) # sort by key and get values (histogram) | |
arr = arr[int(len(arr)*0.1):len(arr)-int(len(arr)*0.1)] # crop 10% of the begin and end of array | |
# Rect conditions: | |
# 1) array variance must be close to zero | |
# 2) array lenght must be greater than 25% of the image cols | |
# 3) contour area must be greater than 1% of the image area | |
if np.var(arr)<0.01 and len(arr)>shape[1]*0.25 and cv2.contourArea(contour)>(shape[0]*shape[1])*0.01: | |
return True | |
else: | |
return False | |
def pre_process(img): | |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
img = cv2.GaussianBlur(img,(5,5),0) | |
# _, img = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) | |
# cv2.erode(img, ) | |
return img | |
DISPLAY = False | |
def detect_boxes(input, img): | |
boxes = [] | |
img = cv2.medianBlur(img, 3) | |
canny = cv2.Canny(img, 10, 50) | |
contours, _ = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) | |
for contour in contours: | |
if DISPLAY and cv2.contourArea(contour)>900: | |
arr = list(dict(sorted(Counter(contour[:, 0, 0]).items())).values()) | |
arr = arr[int(len(arr)*0.1):len(arr)-int(len(arr)*0.1)] | |
if True: # if np.var(arr)<0.01 and len(arr)>img.shape[1]*0.25 and cv2.contourArea(contour)>(img.shape[0]*img.shape[1])*0.01: | |
print('{} - OK'.format(np.var(arr))) | |
cv2.drawContours(input,contour,-1,(0,255,0),2) | |
cv2.namedWindow('contour', cv2.WINDOW_NORMAL) | |
cv2.imshow('contour',input) | |
cv2.waitKey() | |
arr = list(dict(sorted(Counter(contour[:, 0, 0]).items())).values()) | |
plt.plot(arr) | |
plt.show() | |
if check_countour(img.shape, contour): | |
x_min, y_min, x_max, y_max = np.min(contour,0)[0][0], np.min(contour,0)[0][1], np.max(contour,0)[0][0], np.max(contour,0)[0][1] | |
boxes.append(input[y_min:y_max, x_min:x_max]) | |
print('Found {} boxes'.format(len(boxes))) | |
return boxes if len(boxes)==3 else [] | |
input = img = cv2.imread('imgs/1.png') | |
img = pre_process(img) | |
boxes = detect_boxes(input, img) | |
for box in boxes: | |
cv2.imshow('box', box) | |
cv2.waitKey() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment