|
a |
|
b/10x/batch_old.py |
|
|
1 |
import os |
|
|
2 |
|
|
|
3 |
import numpy as np |
|
|
4 |
|
|
|
5 |
import skimage as ski |
|
|
6 |
from skimage import io, filter, color, exposure, morphology, feature, measure, transform |
|
|
7 |
|
|
|
8 |
from sklearn.linear_model import BayesianRidge |
|
|
9 |
from sklearn.mixture import GMM |
|
|
10 |
|
|
|
11 |
from scipy import spatial, ndimage, signal, stats |
|
|
12 |
|
|
|
13 |
import matplotlib.pyplot as plt |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
dirq = "data/" |
|
|
19 |
files = [] |
|
|
20 |
for i in os.listdir(dirq) : |
|
|
21 |
if i.endswith(".jpg") : |
|
|
22 |
files.append(dirq + i) |
|
|
23 |
|
|
|
24 |
|
|
|
25 |
for f in files : |
|
|
26 |
|
|
|
27 |
image = exposure.equalize_adapthist(io.imread(f)) |
|
|
28 |
|
|
|
29 |
binary = filter.threshold_adaptive(exposure.adjust_sigmoid(image[:, :, 0], cutoff=0.4, gain = 30), 301).astype(bool) |
|
|
30 |
clean = morphology.binary_closing(binary, morphology.disk(3)).astype(bool) |
|
|
31 |
clean = morphology.remove_small_objects(clean, 200) |
|
|
32 |
clean = morphology.remove_small_objects( (1-clean).astype(bool), 200) |
|
|
33 |
|
|
|
34 |
local_density = filter.gaussian_filter(clean, 61) |
|
|
35 |
|
|
|
36 |
ent = filter.gaussian_filter(filter.rank.entropy(local_density, morphology.disk(3)), 75) |
|
|
37 |
|
|
|
38 |
ent -= ent.min() |
|
|
39 |
ent /= ent.max() |
|
|
40 |
|
|
|
41 |
local_density -= local_density.min() |
|
|
42 |
local_density /= local_density.max() |
|
|
43 |
|
|
|
44 |
info = ent * (1 + local_density) |
|
|
45 |
|
|
|
46 |
bw = (info) > filter.threshold_otsu(info) |
|
|
47 |
|
|
|
48 |
|
|
|
49 |
C = measure.find_contours(bw, 0.5) |
|
|
50 |
centroid = [] |
|
|
51 |
vals = [] |
|
|
52 |
|
|
|
53 |
for c in C : |
|
|
54 |
centroid.append(np.linalg.norm([c[:, 1].mean() - bw.shape[1] / 2, c[:, 0].mean() - bw.shape[0] / 2])) |
|
|
55 |
vals.append(local_density.T[c.astype(int)].sum()) |
|
|
56 |
|
|
|
57 |
cent = C[np.argmin(centroid / np.array(vals))] |
|
|
58 |
|
|
|
59 |
|
|
|
60 |
fix, ax = plt.subplots() |
|
|
61 |
plt.imshow(image) |
|
|
62 |
ax.plot(cent[:, 1], cent[:, 0], lw=5, c="k", alpha = 0.7) |
|
|
63 |
|
|
|
64 |
plt.savefig(f.split("Sheep")[1]) |
|
|
65 |
print ("Done " + f.split("Sheep")[1]) |