-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_regions.py
55 lines (44 loc) · 1.8 KB
/
2_regions.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
import pathlib
import numpy as np
import pandas as pd
from PIL import Image
from skimage import measure
import tqdm
regions = []
for part in ['train', 'test']:
for sequence_path in tqdm.tqdm(list(pathlib.Path(f'data/spotGEO/{part}').glob('*')), position=0):
if sequence_path.name == '.DS_Store': continue
sequence = int(sequence_path.name)
for frame_path in sequence_path.glob('*.png'):
frame = int(frame_path.stem)
# Access the necessary data
img = Image.open(frame_path)
pixels = np.asarray(img)
img.close()
med = np.load(f'data/medians/{part}/{sequence}/{frame}.npy')
# Determine which pixels are brighter than their surroundings
interesting = pixels > med + 6
# Label each pixel
labels = measure.label(interesting)
# Group identically labeled pixels into regions
rs = measure.regionprops(label_image=labels, intensity_image=pixels)
centers = np.asarray([
r.coords[np.take(pixels, np.ravel_multi_index(r.coords.T, pixels.shape)).argmax()]
for r in rs
])
for region, (r, c) in zip(rs, centers):
regions.append({
'part': part,
'sequence': sequence,
'frame': frame,
'r': r,
'c': c,
'area': region.area,
'extent': region.extent,
'perimeter': region.perimeter,
'diameter': region.equivalent_diameter,
'eccentricity': region.eccentricity,
'solidity': region.solidity
})
df = pd.DataFrame.from_records(regions)
df.to_pickle('data/interesting.pkl')