使用scikit图像计算图像中特定区域的平均灰度值

问题描述 投票:1回答:1

我正在尝试计算下面显示的图像中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

蒙版图像:“蒙版图像”“>

我正在尝试计算下面显示的图像中5个区域的灰度。但是我没有在skimage中找到任何好的命令。我首先通过处理以下代码来掩盖图像:...

python image image-processing scikit-image grayscale
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.