【问题标题】:How to calculate the internal area(count of pixels) inside a ring-like shape?如何计算环状形状内的内部面积(像素数)?
【发布时间】:2020-08-24 03:39:49
【问题描述】:
  • 我有以下图像,我确实想计算环内的像素以获得面积。

  • 我做了一些形态学操作作为一种后处理,以使图像尽可能地具有清晰平滑的边缘。
  • 我尝试以不同的方式来实现这一点,正如您在下面的代码中看到的那样,但它们都不是最佳的。
  • 你能告诉我如何计算圆的像素内部区域吗? 注意:内部的一些像素不是全黑的,它们的强度很低,这就是我尝试进行 Otsu 阈值处理的原因。
  • 提前致谢
import numpy as np
import matplotlib.pyplot as plt
from skimage.io import imread, imsave
# import scipy.ndimage as ndi 
from skimage import morphology, filters, feature

seg = np.squeeze(imread('prediction.png')[...,:1])
# meijering alpha=None,
# rem2 = morphology.remove_small_objects(seg, 4)
resf = filters.meijering(seg, sigmas=range(1, 3, 1),  black_ridges=False)

sobel = filters.sobel(resf)
# diam = morphology.diameter_closing(sobel, 64, connectivity=2)
gaussian = filters.gaussian(sobel, sigma= 1)
val = filters.threshold_otsu(gaussian)
resth = gaussian < val 

# Morphology
SE = morphology.diamond(2)
# SE = np.ones((3,3))
# SE = morphology.disk(2)
# SE = square(7)
# SE = rectangle(3,3)
# SE = octagon(3, 3)

erosion  = morphology.binary_erosion( resth, SE).astype(np.uint8)
dilation = morphology.binary_dilation(resth, SE).astype(np.uint8)
opening  = morphology.binary_opening( resth, SE).astype(np.uint8)
closing  = morphology.binary_closing( resth, SE).astype(np.uint8)
#thinner = morphology.thin(erosion, max_iter=4)

rem  = morphology.remove_small_holes(resth, 2)

# entropy  = filters.rank.entropy(resth, SE) 
# print(seg.shape)

plt.figure(num='PProc')
# 1
plt.subplot('335')
plt.imshow(rem,cmap='gray')
plt.title('rem')
plt.axis('off')
# 2
plt.subplot('336')
plt.imshow(dilation,cmap='gray')
plt.title('dilation')
plt.axis('off')
# 3
plt.subplot('337')
plt.imshow(opening,cmap='gray')
plt.title('opening')
plt.axis('off')
# 4
plt.subplot('338')
plt.imshow(closing,cmap='gray')
plt.title('closing')
plt.axis('off')
# 5
plt.subplot('332')
plt.imshow(seg,cmap='gray')
plt.title('segmented')
plt.axis('off')
# 6
plt.subplot('333')
plt.imshow(resf,cmap='gray')
plt.title('meijering')
plt.axis('off')
# 7
# 8
plt.subplot('334')
plt.imshow(resth,cmap='gray')
plt.title('threshold_otsu')
plt.axis('off')
# 9
plt.subplot('339')
plt.imshow(erosion,cmap='gray')
plt.title('erosion')
plt.axis('off')
#
plt.show()

【问题讨论】:

  • 这是唯一的图像吗/您总是每张图像都有一个戒指吗?
  • @PaulBrodersen 不,实际上我有不同的图像,形状几乎相似,更小或更大,但是是的,它应该只有一个环。
  • 另外,你的问题有点模棱两可。您想要构成环的浅色像素,还是想要环内的黑色像素?
  • @PaulBrodersen 我想计算环内黑色像素的数量。

标签: python-3.x image-processing image-segmentation scikit-image image-morphology


【解决方案1】:

我确定我遗漏了一些东西,但你为什么不能只用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()

【讨论】:

  • 你没有遗漏任何东西,你的答案很完美,谢谢。
  • 乐于助人。作为(不请自来的)建议的一般要点:根据我的经验,面对真实数据时,一长串形态学操作很少是稳健的。尝试找到您感兴趣的对象的其他一些可以完成繁重工作的属性。在这里,我只是使用阈值图像中的连通性来分离区域。但是,您也可以利用您正在处理圈子的事实。这些可以使用霍夫圆变换找到。
猜你喜欢
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
  • 1970-01-01
  • 2018-10-06
  • 1970-01-01
  • 2013-03-14
  • 2014-02-16
相关资源
最近更新 更多