-
Notifications
You must be signed in to change notification settings - Fork 2
/
draw_folder.py
107 lines (77 loc) · 2.49 KB
/
draw_folder.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
import numpy as np
import matplotlib.pyplot as plt
from shapely.geometry.polygon import LinearRing, Polygon
from sklearn.linear_model import LinearRegression
import pandas as pd
import os
import cv2 as cv
"""
Copy this file to the folder containing the tifs you want to convert
run from command line like this: python draw_folder.py
"""
tifs = [x for x in os.listdir('./T152_Full') if '.tif' in x]
def make_binary_image(image_file):
"""
image_file:
(str), the location of the tif file
outputs a binarized image with points of interest highlighted
"""
print('processing: ' + image_file)
#Importing image
imarray = cv.imread('./T152_Full/' + image_file,0)
#getting dimensions
h, w = imarray.shape
#shrinking output dimensions (remove later)
h /= 10 ; w /= 10
#creating threshold
mn = imarray.mean() + 3 * imarray.std()
#binarizing image
tf = np.where(imarray > mn)
ret,thresh1 = cv.threshold(imarray,mn,255,cv.THRESH_BINARY)
#creating output image
print('writing: ' + image_file)
cv.imwrite('./output/'+image_file, thresh1)
def outline_shape(csvfile):
"""
csvfile:
(str), the target csv file. csv file should have x and y coordinates and
no header
takes a collection of points and turns them into a picture with an outline
returns None
"""
#importing csvfile
df = pd.read_table(csvfile, header=None, delimiter=',')
#defining the general slope for the object
lr = LinearRegression()
lr.fit(df[0].reshape(-1, 1), df[1].reshape(-1, 1))
#determining if points are above or below the line
df[2] = [lr.predict(a) for a in df[0]]
df[3] = df[1] > df[2]
#sorting by 1.) above or below line, 2.) x coords
df = df.sort_values([3, 0])
#putting coordinates in correct order
coords = []
for i, r in df[df[3] == False].iterrows():
coords.append([r[0], r[1]])
for i, r in df[df[3] == True][::-1].iterrows():
coords.append([r[0], r[1]])
#prepping points
ring = LinearRing(coords)
x, y = ring.xy
#plotting figure
fig = plt.figure(1, figsize=(15,15), dpi=90)
ax = fig.add_subplot(111)
ax.plot(x, y)
xrange = [0, 5000]
yrange = [0, 1000]
ax.set_xlim(*xrange)
ax.set_ylim(*yrange)
ax.set_aspect(1)
plt.axis('off')
#saving image
nmsplt = csvfile.split('.')
newname = nmsplt[-2] + '.png'
plt.savefig(newname)
if __name__ == '__main__':
for t in tifs:
make_binary_image(t)