Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translation from GUI settings to Python API not always completely clear #371

Open
oanegros opened this issue Dec 13, 2024 · 1 comment
Open
Labels
question Further information is requested

Comments

@oanegros
Copy link

Hi,

I'm having an issue with the translation of our pipeline as we're executing it in the plantseg 2 GUI to a python script. Namely, I can't find the equivalent function call for 'Remove objects with low foreground probability'. Does this exist, and which function is this?

Also, some other parts of this are still somewhat untransparent, such as the naming renaming agglomeration modes between camelcase and snakecase in GUI vs Python.

Thanks :D

@qin-yu qin-yu added the question Further information is requested label Dec 13, 2024
@qin-yu
Copy link
Collaborator

qin-yu commented Dec 13, 2024

Hey Oane,

GUI widgets correspond to tasks which wraps functionals for resource management. In IDEs or on GitHub, you can usually click into a call and see what is underneath.

def remove_false_positives_by_foreground_probability(
segmentation: np.ndarray,
foreground: np.ndarray,
threshold: float,
) -> np.ndarray:
"""
Removes false positive regions in a segmentation based on a foreground probability map.
1. Labels are not preserved.
2. If the mean(an instance * its own probability region) < threshold, it is removed.
Args:
segmentation (np.ndarray): Segmentation array where each unique non-zero value indicates a distinct region.
foreground (np.ndarray): Foreground probability map of the same shape as `segmentation`.
threshold (float): Probability threshold below which regions are considered false positives.
Returns:
np.ndarray: Segmentation array with false positives removed.
"""
# TODO: make a channel for removed regions for easier inspection
# TODO: use `relabel_sequential` to recover the original labels
if segmentation.shape != foreground.shape:
raise ValueError("Segmentation and probability map must have the same shape.")
if foreground.max() > 1:
raise ValueError("Foreground must be a probability map with values in [0, 1].")
instances, _, _ = relabel_sequential(segmentation) # The label 0 is assumed to denote the bg and is never remapped.
regions = regionprops(instances) # Labels with value 0 are ignored.
pixel_count = np.zeros(len(regions) + 1)
pixel_value = np.zeros(len(regions) + 1)
pixel_count[0] = 1 # Avoid division by zero: pixel_count[0] and pixel_value[0] are fixed throughout.
for region in tqdm.tqdm(regions):
bbox = region.bbox
if instances.ndim == 3:
slices = (slice(bbox[0], bbox[3]), slice(bbox[1], bbox[4]), slice(bbox[2], bbox[5]))
else:
slices = (slice(bbox[0], bbox[2]), slice(bbox[1], bbox[3]))
region_mask = instances[slices] == region.label
prob = foreground[slices]
pixel_count[region.label] = region.area
pixel_value[region.label] = (region_mask * prob).sum()
likelihood = pixel_value / pixel_count
to_remove = likelihood < threshold
instances[np.isin(instances, np.nonzero(to_remove)[0])] = 0
instances, _, _ = relabel_sequential(instances)
return instances

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants