我确信我遗漏了一些东西,但是为什么你不能只设定阈值,给图像贴上标签,然后用
regionprops
?
#!/usr/bin/env python
"""
Determine areas in image of ring.
SO: https://stackoverflow.com/q/61681565/2912349
"""
import numpy as np
import matplotlib.pyplot as plt
from skimage.io import imread
from skimage.filters import threshold_otsu
from skimage.measure import label, regionprops
from skimage.color import label2rgb
if __name__ == '__main__':
raw = imread('prediction.png', as_gray=True)
threshold = threshold_otsu(raw)
thresholded = raw > threshold
# Label by default assumes that zeros correspond to "background".
# However, we actually want the background pixels in the center of the ring,
# so we have to "disable" that feature.
labeled = label(thresholded, background=2)
overlay = label2rgb(labeled)
fig, axes = plt.subplots(1, 3)
axes[0].imshow(raw, cmap='gray')
axes[1].imshow(thresholded, cmap='gray')
axes[2].imshow(overlay)
convex_areas = []
areas = []
for properties in regionprops(labeled):
areas.append(properties.area)
convex_areas.append(properties.convex_area)
# take the area with the smallest convex_area
idx = np.argmin(convex_areas)
area_of_interest = areas[idx]
print(f"My area of interest has {area_of_interest} pixels.")
# My area of interest has 714 pixels.
plt.show()