Skip to content
Snippets Groups Projects
Commit f85245c2 authored by Andrey  ARISTOV's avatar Andrey ARISTOV
Browse files

rename module

parent 5b5495a7
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
File moved
File moved
File moved
File moved
File moved
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
### Use: ### Use:
`python -m nd2shrink path_to_nd2` `python -m nd2tif path_to_nd2`
# Bin 16x stitched nd2: # Bin 16x stitched nd2:
......
import cv2
import numpy as np
from skimage.measure import regionprops, label
from scipy.ndimage import gaussian_filter, binary_erosion
import matplotlib.pyplot as plt
import matplotlib
import logging
matplotlib.logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def findSpheroid(
imCropped: np.ndarray,
sigma: float = 5,
erode: int = 3,
threshold: float = 0.5,
max_ecc: float = 0.95,
lim_major_axis_length: tuple = (100, 400),
plot: bool = False,
):
"""
We find the spheroid by thresholding the intensity
and area filling. Sph. must have a dark border around
it.
returns mask
"""
result1, result2 = np.gradient(imCropped)
grad = np.sqrt(result1 ** 2 + result2 ** 2)
if plot:
f, (ax1, ax2, ax3, ax4) = plt.subplots(
1, 4, sharey=True, sharex=True, figsize=(10, 3)
)
ax1.imshow(imCropped, cmap="gray")
ax1.set_title("raw")
ax2.imshow(grad)
ax2.set_title("grad")
toThresh = gaussian_filter(grad, sigma=sigma)
imThresh = toThresh > np.max(toThresh) * threshold
cnts, h = cv2.findContours(
imThresh.astype("uint8"), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
)
mask = cv2.drawContours(
imThresh.astype("uint8"),
cnts,
-1,
(255, 255, 255),
thickness=cv2.FILLED,
)
eroded_mask = binary_erosion(
mask, structure=np.ones((erode, erode))
).astype(mask.dtype)
temp = eroded_mask
imLabel = label(temp)
logger.debug(f"found {imLabel.max()} regions")
if plot:
ax3.imshow(imLabel)
ax3.set_title("all regions")
for i, region in enumerate(regionprops(imLabel)):
logger.debug(f"filtering region {i}")
if region.eccentricity > max_ecc:
# check it is inside or outside
logger.debug(f"eccentricity {region.eccentricity} > {max_ecc}")
temp[imLabel == region.label] = 0
# region given same value as sph. border
if (l := region.major_axis_length) < (lim := lim_major_axis_length)[
0
] or l > lim[1]:
logger.debug(f"major_axis_length {l} outside limits {lim}")
temp[imLabel == region.label] = 0
if plot:
ax4.imshow(temp)
ax4.set_title("Selected regions")
return temp, f
return temp
def get_props(mask: np.ndarray, **kwargs):
lab = label(mask)
# assert lab.max() == 1, f'number of segments is {lab.max()}'
reg = regionprops(lab)
return [
{
"area": r.area,
"eccentricity": r.eccentricity,
"major_axis_length": r.major_axis_length,
**kwargs,
}
for r in reg
]
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import numpy as np import numpy as np
from numpy.testing import assert_array_equal from numpy.testing import assert_array_equal
from nd2shrink import transform, read from nd2tif import transform, read
from scipy.ndimage import binary_erosion from scipy.ndimage import binary_erosion
import logging import logging
......
from nd2shrink import save from nd2tif import save
import numpy import numpy
from nd2shrink import read from nd2tif import read
import os, sys import os, sys
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment