File size: 3,228 Bytes
c4731f0
dfa545b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from larvaecount.gradient import (
    contour_thresh,
    component_thesh,
    component_filter_thresh
)
from os import PathLike
from PIL import Image
from pillow_heif import register_heif_opener
from typing import Optional

import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
import fire

register_heif_opener()

def filter_connected_components(
    img_dir: PathLike,
    color_thresh: int = 75,
    avg_area: float = 800,
    vis: bool = False,
    save_loc: PathLike = "",
    kernal_size: tuple[int, int] = (3, 3),
    max_eggs: Optional[int] = None
) -> None:
    # Open image, supports apple HEIC format
    pil_img = Image.open(img_dir)

    # Convert to standard RGB Image
    img = np.array(pil_img)
    res = component_filter_thresh(
        img,
        color_thresh = color_thresh,
        avg_area = avg_area,
        kernal_size = kernal_size,
        max_eggs = max_eggs
    )

    res_vis = res["vis"]
    res_stats = res["stats"]

    for label, stat in res_stats.items():
        print(f"{label.replace('-', ' ')}: {stat}")

    if vis:
        for label, curr_img in res_vis.items():
            plt.imshow(curr_img)
            plt.show()

    if save_loc:
        for label, curr_img in res_vis.items():
            save_path = os.path.join(save_loc, label + ".png")
            plt.imsave(save_path, curr_img)
    

def connected_components(
    img_dir: PathLike,
    color_thresh: int = 75,
    avg_area: float = 800,
    vis: bool = False,
    save_loc: PathLike = "",
    max_eggs: Optional[int] = None
) -> None:
    # Open Image
    pil_img = Image.open(img_dir)

    # Convert to standard RGB Image
    img = np.array(pil_img)

    res = component_thesh(
        img,
        color_thresh = color_thresh,
        avg_area = avg_area,
        max_eggs = max_eggs
    )
    res_vis = res["vis"]
    res_stats = res["stats"]

    for label, stat in res_stats.items():
        print(f"{label.replace('-', ' ')}: {stat}")

    if vis:
        for label, curr_img in res_vis.items():
            plt.imshow(curr_img)
            plt.show()

    if save_loc:
        for label, curr_img in res_vis.items():
            save_path = os.path.join(save_loc, label + ".png")
            plt.imsave(save_path, curr_img)

def contour(
    img_dir: PathLike,
    color_thresh: int = 75,
    avg_area: float = 800,
    vis: bool = False,
    save_loc: PathLike = "",
    kernal_size: tuple[int, int] = (3, 3)
) -> None:
    # Open image, supports apple HEIC format
    pil_img = Image.open(img_dir)

    # Convert to standard RGB Image
    img = np.array(pil_img)
    res = contour_thresh(
        img,
        color_thresh = color_thresh,
        avg_area = avg_area,
        kernal_size = kernal_size
    )

    res_vis = res["vis"]
    res_stats = res["stats"]

    for label, stat in res_stats.items():
        print(f"{label.replace('-', ' ')}: {stat}")

    if vis:
        for label, curr_img in res_vis.items():
            plt.imshow(curr_img)
            plt.show()

    if save_loc:
        for label, curr_img in res_vis.items():
            save_path = os.path.join(save_loc, label + ".png")
            plt.imsave(save_path, curr_img)

if __name__ == "__main__":
    fire.Fire()