-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrectangle_alignment.py
60 lines (38 loc) · 1.76 KB
/
rectangle_alignment.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import cv2
import numpy as np
'''
Task 2: Align(make the rectangle image straight) all the given images of the rectangle.
Things I tried to do: To make each rectangle straight, first, I tried to detect the four
rectangles on the image by contours detection. My plan was to get the positions/coordinates,
and the angle of each rectangle so that using them I could rotate them first, and then align.
I tried to rotate the specific rectangle inside the image using cv2.getRotationMatrix2D and
cv2.drawContours.
'''
img = cv2.imread('images/treeleaf_task.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
ret, thres = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(
thres, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img_d = img.copy()
output = np.zeros(img.shape[:2])
for i, con in enumerate(contours):
if hierarchy[0][i][3] == 0: #only getting the contours of the rectangles.
print(i)
rect = cv2.minAreaRect(con)
# print(rect)
# trying to get the (x,y),(height, width) and angle of each rectangle.
(x, y), (height, width), angle = rect
# x and y give us the center point of every rectangle
box = cv2.boxPoints(rect).astype(int)
print(box)
# this will return the coordinates of the rectangles
height, width = height//2, width//2
R = cv2.getRotationMatrix2D((x, y), 90, 1)
con = cv2.warpAffine(con, R, dsize=(int(height),int(width)))
# cv2.rectangle(output,(255,110),(750,210),(1,1,1),-1 )
#I tried to apply the rotation but could not do it.
cv2.imshow('original img', img_d[291:370])
break
cv2.waitKey(0)
cv2.destroyAllWindows()