-
Notifications
You must be signed in to change notification settings - Fork 1
/
center.py
executable file
·84 lines (72 loc) · 2.33 KB
/
center.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import os
from pprint import pprint
import matplotlib.pyplot as plt
dstdirname = 'centered'
dirname = 'cells'
files = [fname for fname in os.listdir(dirname) if fname.endswith('.png')]
files = sorted(files, key=lambda f: int(f.split('_')[0]))
for fname in files:
img = cv2.imread(os.path.join(dirname, fname))
height, width = img.shape[:2]
threshold = 150
element = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
bw = cv2.dilate(
img, element,
borderType=cv2.BORDER_CONSTANT,
borderValue=(255, 255, 255))
# fig, ax = plt.subplots()
# fig.canvas.set_window_title(fname)
# ax.imshow(img)
# ax.axis('off')
# mng = plt.get_current_fig_manager()
# mng.full_screen_toggle()
# plt.show()
# fig, ax = plt.subplots()
# fig.canvas.set_window_title(fname)
# ax.imshow(bw)
# ax.axis('off')
# mng = plt.get_current_fig_manager()
# mng.full_screen_toggle()
# plt.show()
# continue
bw = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
bw = cv2.threshold(bw, threshold, 255, cv2.THRESH_BINARY)[1]
min_point = (np.inf, np.inf)
max_point = (-np.inf, -np.inf)
black_pix = False
for y, row in enumerate(bw):
for x, pix in enumerate(row):
if not pix:
min_point = (
min(min_point[0], x),
min(min_point[1], y),
)
max_point = (
max(max_point[0], x),
max(max_point[1], y),
)
black_pix = True
if black_pix:
min_point = np.asarray(min_point)
max_point = np.asarray(max_point)
center = (min_point + max_point) / 2
center = np.rint(center)
print(fname, center)
image_center = np.asarray([width, height]) / 2
center -= image_center
M = [
[1, 0, -center[0]],
[0, 1, -center[1]],
]
M = np.matrix(M)
img = cv2.warpAffine(
img, M, (width, height),
cv2.INTER_LINEAR,
borderMode=cv2.BORDER_CONSTANT,
borderValue=(255, 255, 255),
)
cv2.imwrite(os.path.join(dstdirname, fname), img)