ekelner/templateGenerator.py

53 lines
1.9 KiB
Python

# from imutils.perspective import four_point_transform
# from imutils import contours
#import imutils
import numpy as np
import argparse
import cv2
import csv
from photoAnalysisUtils import *
# Utworzenie plików z koordynatami do punktu odniesienia, oraz poszczegolnymi checkboxami
f= open("referencePoint.txt","w+")
f_check = open("checkBoxesCords.txt", "w+")
image = cv2.imread("emptyTemplate.jpg")
# Funkcja wyszukujaca punkt odniesienia
image, x, y = searchReferencePointCorner(image)
f.write(str(x) + " " + str(y) + "\n")
#Szukanie konturów oraz koordynatów checkboxów
edged = cv2.Canny(image, 20, 200)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(edged, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
threshold_max_area = 5000
threshold_min_area = 1000
cnts = cv2.findContours(edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
i = 1
last_x = -1
last_y = -1
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.035 * peri, True)
x,y,w,h = cv2.boundingRect(approx)
aspect_ratio = w / float(h)
area = cv2.contourArea(c)
if len(approx) == 4 and area < threshold_max_area and area > threshold_min_area and (aspect_ratio >= 0.9 and aspect_ratio <= 1.1):
# Odiltrowanie dublowania konturów oraz zapisywanie ich koordynatów do pliku
if ( abs(x - last_x) > 0.01*last_x ) or ( abs(y - last_y) > 0.01*last_y ):
last_x = x
last_y = y
f_check.write(str(x)+' '+str(y)+' '+str(w)+' '+str(h)+''+"\n")
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
print("X: "+str(x)+" Y: "+str(y)+" I:", str(i))
cv2.putText(image, str(i), (x+50,y+50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,255,0),2)
i=i+1
cv2.drawContours(image, [c], 0, (0, 255, 0), 3)
cv2.imwrite("templateProcess.jpg", image)
f.close();
f_check.close();