-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain2.py
30 lines (24 loc) · 1.19 KB
/
main2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import cv2
import numpy as np
image_file = 'D:\REP\projects\drawing_text_recognition\images\sxema65-1.jpg'
img = cv2.imread(image_file)
crop_img = img[img.shape[0] - 1200:img.shape[0] - 470, img.shape[1] - 1200:img.shape[1]-200] #Вырезать определенную область
gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)
img_erode = cv2.erode(thresh, np.ones((3, 3), np.uint8), iterations=1)
# Get contours
contours, hierarchy = cv2.findContours(img_erode, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
output = crop_img.copy()
for idx, contour in enumerate(contours):
(x, y, w, h) = cv2.boundingRect(contour)
print("R", idx, x, y, w, h, cv2.contourArea(contour), hierarchy[0][idx])
# hierarchy[i][0]: the index of the next contour of the same level
# hierarchy[i][1]: the index of the previous contour of the same level
# hierarchy[i][2]: the index of the first child
# hierarchy[i][3]: the index of the parent
if hierarchy[0][idx][3] == 0:
cv2.rectangle(output, (x, y), (x + w, y + h), (0, 0, 255), 1)
cv2.imshow("Input", crop_img)
cv2.imshow("Enlarged", img_erode)
cv2.imshow("Output", output)
cv2.waitKey(0)