-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrop.py
More file actions
64 lines (58 loc) · 1.61 KB
/
crop.py
File metadata and controls
64 lines (58 loc) · 1.61 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
import cv2
import numpy as np
import os
import params
from PIL import Image
import matplotlib.pyplot as plt
count = {
'r_kin': 0,
'r_man': 0,
'r_ele': 0,
'r_hor': 0,
'r_roo': 0,
'r_paw': 0,
'r_can': 0,
'b_kin': 0,
'b_man': 0,
'b_ele': 0,
'b_hor': 0,
'b_roo': 0,
'b_paw': 0,
'b_can': 0
}
def generate_rotation(roi, kind, num):
start = count[kind]
roi = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)
dif = 360/num
roi = cv2.resize(roi, params.size)
img = Image.fromarray(roi)
for i in range(start,start+num):
temp = img.rotate(i*dif)
temp.save(f'data/noModify/{kind}/{i}.jpg', quality=100, subsampling=0)
count[kind] += num
# 读取图像
image = cv2.imread('chess.jpg')
# 检测象棋的圆形
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=20, maxRadius=31)
circles = np.uint16(np.around(circles))
os.makedirs('data', exist_ok=True)
for kind in params.types:
os.makedirs(f'data/noModify/{kind}', exist_ok=True)
# 在原图像上标出识别出的圆
cnt = 0
for (x, y, r) in circles[0, :]:
# print(x, y, r)
r -= 4
roi = image[y-r:y+r+1, x-r:x+r+1]
for i in range(2*r+1):
for j in range(2*r+1):
if (i-r)**2 + (j-r)**2 > r**2:
roi[i, j] = 0
kind = params.mark[cnt]
generate_rotation(roi, kind, 360//params.duplicates[kind])
cv2.circle(image, (x, y), r, (0, 255, 0), 2)
cv2.circle(image, (x, y), 2, (0, 0, 255), 3)
cnt += 1
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.show()