-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
94 lines (83 loc) · 2.71 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
"""
@version: 1.0
@license: Apache Licence
@author: kht,cking616
@contact: [email protected]
@software: PyCharm Community Edition
@file: main.py
@time: 2018/5/15
"""
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
import time
from matplotlib.font_manager import FontProperties
font_song = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)
def test_img(jpg):
img = cv2.imread(jpg)
sp = img.shape
start = time.clock()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.threshold(gray, 32, 100, cv2.THRESH_BINARY, gray)
gray = cv2.Canny(gray, 0, 0)
gray = np.uint8(np.absolute(gray))
cv2.GaussianBlur(gray, (7, 7), 2.5, gray, 2.5)
"""
plt.subplot(121)
plt.imshow(gray, 'gray')
plt.xticks([])
plt.yticks([])
plt.subplot(122)
plt.imshow(img)
plt.xticks([])
plt.yticks([])
plt.show()
plt.close()
"""
circles1 = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, sp[1] / 2,
param1=100, param2=90, minRadius=0, maxRadius=300)
end = time.clock()
if isinstance(circles1, np.ndarray):
circles = circles1[0, :, :]
circles = np.uint16(np.around(circles))
for i in circles[:]:
cv2.circle(img, (i[0], i[1]), i[2], (255, 0, 0), 5)
cv2.circle(img, (i[0], i[1]), 2, (255, 0, 255), 10)
cv2.rectangle(img, (i[0]-i[2], i[1]+i[2]), (i[0]+i[2], i[1]-i[2]), (255, 255, 0), 5)
plt.subplot(121)
plt.imshow(gray, 'gray')
plt.xticks([])
plt.yticks([])
message = "Picture:" + jpg + ":"
print(message)
message = "Hight:" + str(sp[0]) + " Width:" + str(sp[1])
print(message)
xp = sp[1] / 2 - i[0]
yp = sp[0] / 2 - i[1]
print(" FOUP Center Position: X:", i[0], "Y:", i[1])
message = "此FOUP圆心离图片中心偏移\nX坐标:" + str(xp) + " Y坐标:" + str(yp)
print(message)
print("Computation cost:", end - start)
plt.title(message, fontproperties=font_song) # title包含中文
plt.subplot(122)
plt.imshow(img)
plt.xticks([])
plt.yticks([])
plt.show()
plt.close()
else:
print("Don't find any FOUP!")
input("Enter to continue")
if __name__ == '__main__':
if not os.path.exists('img'):
os.mkdir('img')
print('第一步将测试图像放到img文件夹下')
print('注意:请使用jpg格式的图片')
input("Enter键继续")
for (root, dirs, files) in os.walk('.\img'):
for filename in files:
if filename.endswith('.jpg'):
test_img(os.path.join(root, filename))
input("Enter退出")