【问题标题】:Calculate the mean gray value of specific areas in an image with scikit-image使用 scikit-image 计算图像中特定区域的平均灰度值
【发布时间】:2020-02-28 22:41:24
【问题描述】:

我正在尝试计算下图所示图像中 5 个区域的灰度。但是我没有设法在skimage 中找到任何好的命令。

我首先通过处理以下代码来屏蔽图像:

import numpy as np
import matplotlib as mpl
from matplotlib.path import Path
from matplotlib import patches
import matplotlib.pyplot as plt

import skimage.io as io
from skimage import data_dir 

img = io.imread('/media/rene/Windows8_OS/PROMON/Recorded Sequences/PNG/0rpm_p000.png')

vertices = np.asarray([( 947, 1959),
                       (1762, 1959),
                       (1762, 2241),
                       ( 947, 2241),
                       ( 947, 1089),
                       (1762, 1089),
                       (1762, 1371),
                       ( 947, 1371),
                       ( 947,  797),
                       (1762,  797),
                       (1762, 1079),
                       ( 947, 1079),
                       ( 947,  505),
                       (1762,  505),
                       (1762,  787),
                       ( 947,  787),
                       ( 947,  213),
                       (1762,  213),
                       (1762,  495),
                       ( 947,  495)])

# reshape into smaller path for faster debugging
# vertices = vertices // 20

# matplotlib path
path = Path(vertices)
xmin, ymin, xmax, ymax = np.asarray(path.get_extents(), dtype=int).ravel()

# create a mesh grid of the shape of the final mask
x, y = np.mgrid[:img.shape[1], :img.shape[0]]
# mesh grid to points
points = np.vstack((x.ravel(), y.ravel())).T

# mask for the point included in the path
mask1 = path.contains_points(points)
path_points = points[np.where(mask1)]

# reshape mask for display 
img_mask1 = mask1.reshape(x.shape).T

# selecting all but black pixels
# black_pixels_mask = np.all(img_mask1 == [0, 0, 0], axis=-1)
# non_black_pixels_mask = ~black_pixels_mask

# plots
f, ax = plt.subplots()
# if more thn one plot 
# gs = mpl.gridspec.GridSpec(2,2)
# gs.update(wspace=0.2, hspace= 0.2)
# masked image 
ax.imshow(img * img_mask1, cmap="gray")
ixs = np.indices(img.shape)

但现在我不知道如何获取 5 个区域中每个区域的灰度值。我的结果应该看起来像这样: Finding the average pixel values of a list of blobs identified by scikit-image's blob_log (Laplacian of Gaussian) method

蒙面图像:

【问题讨论】:

  • 图像应该是numpy数组所以得到面积img[y1:y2,x1:x2]然后转换为灰色并计算mean()
  • 嘿,感谢您的快速回答!所以我应该在区域 1 到区域 5 中而不是顶点,对吧? img[y1:y2,x1:x2] 不是一行吗?
  • 不,它是带角的矩形 (y1, x1) 和 (y2, x2)。有关详细信息,请参阅此文档:numpy.org/devdocs/reference/arrays.indexing.html
  • 我不明白。你能在我图片中的一个矩形区域做一个例子吗?会有很大帮助...

标签: python image image-processing scikit-image grayscale


【解决方案1】:

根据您的要求,这是一个玩具示例,它向您展示如何计算由对角坐标定义的两个矩形区域的平均值:

import numpy as np
from skimage import io
import matplotlib.pyplot as plt
import matplotlib.patches as patches

img = io.imread('https://i.stack.imgur.com/2YAwu.png')

vertices = [(200, 200),  # upper left corner
            (400, 400),  # lower right corner
            (200, 1200), # and so on...
            (800, 1400)]

fig, ax = plt.subplots(1)
ax.imshow(img, cmap='gray')

for (upper, left), (lower, right) in zip(vertices[0:-1:2], vertices[1::2]):
    subimg = img[upper:lower, left:right]
    avg = subimg.mean()
    print(f'Mean of img[{upper}:{lower}, {left}:{right}]={avg}')
    rect = patches.Rectangle((left, upper), right-left, lower-upper, 
                             linewidth=2, edgecolor='r', facecolor='none')
    ax.add_patch(rect)

plt.show()

输出

Mean of img[200:400, 200:400]=134.5452
Mean of img[200:800, 1200:1400]=0.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    相关资源
    最近更新 更多