-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreadDots.py
More file actions
70 lines (60 loc) · 1.87 KB
/
Copy pathreadDots.py
File metadata and controls
70 lines (60 loc) · 1.87 KB
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
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
#Based on http://answers.opencv.org/question/58775/detect-fiducial-of-dots/
import cv2
import time
start = time.time()
import math
def sqdist( p1, p2):
return abs(p1[0]-p2[0])**2 + abs(p1[1]-p2[1])**2
MAXSQUAREAREA = 7000
MINSQUAREAREA = 1
im_col = cv2.imread('2017-10-12-193434.jpg',cv2.IMREAD_COLOR)
#https://docs.opencv.org/trunk/d7/d4d/tutorial_py_thresholding.html
im = cv2.cvtColor(im_col,cv2.COLOR_BGR2GRAY)
(threshold,bw) = cv2.threshold(im,190,255,cv2.THRESH_BINARY)
#(threshold,bw) = cv2.threshold(im,200,255,cv2.THRESH_OTSU)
#Take a look at that b/w:
cv2.namedWindow("res")
cv2.imshow("res",bw)
#cv2.waitKey()
try:
conts, hier = cv2.findContours(bw,cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
except ValueError:
#Python3-opencv3
image, conts, hier = cv2.findContours(bw,cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
cleaned = []
points = []
sizes = []
#Filter by area size:
for c in conts:
a = cv2.contourArea(c)
#Explore this..
#print(eval(raw_input('>')))
print (a)
if a <= MAXSQUAREAREA and a >= MINSQUAREAREA:
cleaned.append(c)
x,y,w,h = cv2.boundingRect(c)
#Green show rectangle:
cv2.rectangle(im_col,(x,y),(x+w,y+h),(0,255,0),2)
points.append( (x+w/2.0, y+w/2.0) )
sizes.append( w )
print( points )
for (i, pt) in enumerate(points):
print (pt, i)
nearby = 0
w = sizes[i]
for p2 in points:
if sqdist(pt, p2) < 2.8*w*2.8*w:
nearby+= 1
print( pt, w )
print( 'has %s neighbors' % (nearby,))
if nearby > 6:
#print((int(i) for i in pt))
#RED points 0,0,255
cv2.circle( im_col, tuple(int(i) for i in pt) , 2, (0,0,255),2 )
cv2.imshow("res", im_col)
#cv2.waitKey()
print( 'that took %f' % (time.time() - start) )
#cv2.drawContours(im_col,cleaned,-1,(0,0,0), 3)
cv2.imshow("res", im_col)
cv2.waitKey()