Skip to content Skip to sidebar Skip to footer

How To Calculate The Internal Area(count Of Pixels) Inside A Ring-like Shape?

I have the following image, and I do like to count the pixels inside the ring to get the area. I did some morphological operations as a kind of post-processing to make the imag

Solution 1:

I am sure I am missing something, but why can't you just threshold, label the image, and compute your areas with regionprops?

enter image description here

#!/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()

Post a Comment for "How To Calculate The Internal Area(count Of Pixels) Inside A Ring-like Shape?"