-
Notifications
You must be signed in to change notification settings - Fork 1
/
straightener.py
118 lines (92 loc) · 4.16 KB
/
straightener.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import imagej
from scyjava import jimport
from jpype import JArray, JInt
import cv2
import numpy as np
import os
def left_right_flip(image):
flipped_image = np.flip(image, axis=1)
return flipped_image
def Straighten_img(ij, img_path, xpoints, ypoints, line):
imp = ij.IJ.openImage(img_path)
#################################
# xpoints = [124,126,131,137,131,128,121,114]
xpoints_int = JArray(JInt)(xpoints)
# ypoints = [44,63,105,128,148,172,194,206]
ypoints_int = JArray(JInt)(ypoints)
###################################
straightener = jimport('ij.plugin.Straightener')
polyRoi = jimport('ij.gui.PolygonRoi')
roi = jimport('ij.gui.Roi')
new_polyRoi = polyRoi(xpoints_int,ypoints_int,len(xpoints), int(roi.POLYLINE))
imp.setRoi(new_polyRoi)
straightened_img = straightener().straightenLine(imp,line)
straightened_img = ij.py.from_java(straightened_img.getFloatArray())
straightened_img = np.asarray(straightened_img)
straightened_img.astype(int)
blank_img = np.zeros_like(cv2.imread(img_path))
x_start = round((blank_img.shape[0] - straightened_img.shape[0])/2)
y_start = round((blank_img.shape[1] - straightened_img.shape[1])/2)
for channel in range(blank_img.shape[2]):
blank_img[x_start:x_start+straightened_img.shape[0],y_start:y_start+straightened_img.shape[1],channel]= straightened_img
blank_img = left_right_flip(blank_img)
# print(blank_img.shape)
return blank_img
def Straighten_img_white(ij, img, xpoints, ypoints, line):
path = './buffer.jpg'
cv2.imwrite(path, img)
imp = ij.IJ.openImage(path)
#################################
# xpoints = [124,126,131,137,131,128,121,114]
xpoints_int = JArray(JInt)(xpoints)
# ypoints = [44,63,105,128,148,172,194,206]
ypoints_int = JArray(JInt)(ypoints)
###################################
straightener = jimport('ij.plugin.Straightener')
polyRoi = jimport('ij.gui.PolygonRoi')
roi = jimport('ij.gui.Roi')
new_polyRoi = polyRoi(xpoints_int,ypoints_int,len(xpoints), int(roi.POLYLINE))
imp.setRoi(new_polyRoi)
straightened_img = straightener().straightenLine(imp,line)
straightened_img = ij.py.from_java(straightened_img.getFloatArray())
straightened_img = np.asarray(straightened_img)
straightened_img.astype(int)
blank_img = np.ones_like(img) * 255
x_start = round((blank_img.shape[0] - straightened_img.shape[0])/2)
y_start = round((blank_img.shape[1] - straightened_img.shape[1])/2)
blank_img[x_start:x_start+straightened_img.shape[0],y_start:y_start+straightened_img.shape[1]]= straightened_img
os.remove(path)
blank_img = left_right_flip(blank_img)
# print(blank_img.shape)
return blank_img
def Straighten_img_black(ij, img, xpoints, ypoints, line):
path = './buffer.jpg'
cv2.imwrite(path, img)
imp = ij.IJ.openImage(path)
#################################
# xpoints = [124,126,131,137,131,128,121,114]
xpoints_int = JArray(JInt)(xpoints)
# ypoints = [44,63,105,128,148,172,194,206]
ypoints_int = JArray(JInt)(ypoints)
###################################
straightener = jimport('ij.plugin.Straightener')
polyRoi = jimport('ij.gui.PolygonRoi')
roi = jimport('ij.gui.Roi')
new_polyRoi = polyRoi(xpoints_int,ypoints_int,len(xpoints), int(roi.POLYLINE))
imp.setRoi(new_polyRoi)
straightened_img = straightener().straightenLine(imp,line)
straightened_img = ij.py.from_java(straightened_img.getFloatArray())
straightened_img = np.asarray(straightened_img)
straightened_img.astype(int)
blank_img = np.zeros_like(img)
x_start = round((blank_img.shape[0] - straightened_img.shape[0])/2)
y_start = round((blank_img.shape[1] - straightened_img.shape[1])/2)
blank_img[x_start:x_start+straightened_img.shape[0],y_start:y_start+straightened_img.shape[1]]= straightened_img
os.remove(path)
blank_img = left_right_flip(blank_img)
# print(blank_img.shape)
return blank_img
if __name__ == '__main__':
image = cv2.imread('test_straigh.jpg')
thresh = cv2.threshold(image, 255, 255, cv2.THRESH_BINARY)[1]
cv2.imwrite('test.jpg', thresh)