Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 2969eec

Browse files
authored
Added Arrow.py
Created Folder Arrow_Angle_detector and added Arrow.py
1 parent cce8274 commit 2969eec

File tree

1 file changed

+107
-0
lines changed
  • Scripts/Miscellaneous/Arrow_Angle_detector

1 file changed

+107
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import cv2
2+
import numpy as np
3+
4+
5+
def nothing(x):
6+
pass
7+
8+
font = cv2.FONT_HERSHEY_COMPLEX
9+
cap = cv2.VideoCapture(0)
10+
cv2.namedWindow("Adjust")
11+
cv2.createTrackbar("min","Adjust",110,255,nothing)
12+
#resized = cv2.resize(image, (width, height), interpolation=cv2.INTER_AREA)
13+
text = 'Left'
14+
while True:
15+
ret ,frame = cap.read()
16+
frame = cv2.resize(frame, (640,480), interpolation=cv2.INTER_AREA)
17+
cropped = frame[100:400,100:500]
18+
cropped = cv2.flip(cropped,1)
19+
temp = cropped
20+
cropped = cv2.cvtColor(cropped, cv2.COLOR_BGR2GRAY)
21+
mi = cv2.getTrackbarPos("min","Adjust")
22+
23+
_,threshold = cv2.threshold(cropped,mi,255,cv2.THRESH_BINARY)
24+
kernal = np.zeros([10,10],np.uint8)
25+
threshold = cv2.erode(threshold,kernal)
26+
contours,_=cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
27+
28+
for cnt in contours :
29+
area = cv2.contourArea(cnt)
30+
if area > 400 :
31+
approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
32+
approx_area = cv2.contourArea(approx)
33+
if(len(approx)==7):
34+
n = approx.ravel()
35+
x1 = n[0]
36+
y1 = n[1]
37+
x2 = n[2]
38+
y2 = n[3]
39+
x3 = n[6]
40+
y3 = n[7]
41+
distance1 = (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)
42+
distance2 = (x1 - x3)*(x1 - x3) + (y1 - y3)*(y1 - y3)
43+
ratio = distance1/distance2
44+
if(2500<approx_area<25000 and (0.2<ratio<0.3 or ratio <0.1)):
45+
cv2.drawContours(temp,[approx],0,(0,0,255),5)
46+
47+
x = approx.ravel()[0]
48+
y = approx.ravel()[1]
49+
50+
#x1 = n[0]
51+
#y1 = n[1]
52+
#x2 = n[2]
53+
#y2 = n[3]
54+
#x3 = n[6]
55+
#y3 = n[7]
56+
#distance1 = (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)
57+
#distance2 = (x1 - x3)*(x1 - x3) + (y1 - y3)*(y1 - y3)
58+
if(0.2 < distance1/distance2 < 0.3):
59+
cv2.putText(temp,"Arrow tip",(x,y),font,0.5,(0,0,255))
60+
endx = (n[6]+n[8])/2
61+
endy = (n[7]+n[9])/2
62+
topx = x1
63+
topy = y1
64+
length = np.sqrt((topx-endx)*(topx-endx) + (topy-endy)*(topy-endy))
65+
y_v = endy-length
66+
if((topx-endx)==0):
67+
print(0)
68+
else:
69+
tan = (topy - y_v)/(topx - endx)
70+
print(np.arctan(tan)*57.3*2)
71+
#between 90 and -90
72+
else :
73+
cv2.putText(temp,"Arrow end",(x,y),font,0.5,(0,0,255))
74+
# below 90 and -90
75+
if(distance1/distance2 <0.1):
76+
endx = (n[2]+n[0])/2
77+
endy = (n[3]+n[1])/2
78+
topx = n[8]
79+
topy = n[9]
80+
length = np.sqrt((topx-endx)*(topx-endx) + (topy-endy)*(topy-endy))
81+
y_v = endy - length
82+
if((topx-endx)==0):
83+
print(180)
84+
else:
85+
tan = (topy - y_v)/(topx - endx)
86+
print(np.arctan(tan)*57.3*2)
87+
else:
88+
topx = n[6]
89+
topy = n[7]
90+
endx = (n[12]+n[0])/2
91+
endy = (n[13]+n[1])/2
92+
length = np.sqrt((topx-endx)*(topx-endx) + (topy-endy)*(topy-endy))
93+
y_v = endy - length
94+
if((topx-endx)==0):
95+
print(180)
96+
else:
97+
tan = (topy - y_v)/(topx - endx)
98+
print(np.arctan(tan*57.3)*2)
99+
100+
cv2.imshow('temp',temp)
101+
cv2.imshow('threshold',threshold)
102+
#print(text)
103+
if cv2.waitKey(1) & 0xFF == ord('q'):
104+
break
105+
106+
cap.release()
107+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)