Python 查找由scikit image'识别的斑点列表的平均像素值;s blob_log(高斯拉普拉斯)方法

Python 查找由scikit image'识别的斑点列表的平均像素值;s blob_log(高斯拉普拉斯)方法,python,image-segmentation,scikit-image,scientific-computing,Python,Image Segmentation,Scikit Image,Scientific Computing,输入为uint16灰度.tif图像,512 x 512像素。正如这个问题的标题所暗示的,我想计算由blob_log方法(参见:)识别的blob的平均像素强度,但我不确定如何访问每个blob的像素值。平均强度值必须在uint16范围(0到65535)内返回 以下是我到目前为止的情况。如果我不够清楚,请提前道歉。我已经学习python(3.6.x)大约三个月了,这是我的第一个项目。任何帮助都将不胜感激 from skimage import io from math import sqrt fro

输入为uint16灰度.tif图像,512 x 512像素。正如这个问题的标题所暗示的,我想计算由blob_log方法(参见:)识别的blob的平均像素强度,但我不确定如何访问每个blob的像素值。平均强度值必须在uint16范围(0到65535)内返回

以下是我到目前为止的情况。如果我不够清楚,请提前道歉。我已经学习python(3.6.x)大约三个月了,这是我的第一个项目。任何帮助都将不胜感激

from skimage import io

from math import sqrt
from skimage.feature import blob_log

import numpy as np

from pandas import DataFrame


def start():
    while True:
        us_input = input(
            "Please enter the name of the file you'd like to analyze.\n> "
        )

        try:
            im = io.imread(us_input)
            break
        except FileNotFoundError:
            print(
                "That file doesn't seem to exist or has been entered incorrectly."
            )

    neur_detect(im)


def neur_detect(im):    
    neurons = blob_log(im, min_sigma=1, max_sigma=30, threshold=.02, overlap=.1)

    neur_props(neurons)


def neur_props(blobs):
    num_neurons = len(blobs)
    print("\nNumber of potential neurons detected: {}\n".format(num_neurons))

    results = []

    for blob in blobs:
        y_row, x_col, r = blob
        properties = []
        properties.append(x_col / .769230769230769) # convert pixels to um
        properties.append(y_row / .769230769230769) # convert pixels to um
        properties.append(r * sqrt(2)) # compute radii in 3rd column of DataFrame

        mean_intensity = ????
        properties.append(mean_intensity)

        results.append(properties)

    results = DataFrame(results, columns = ['x_coor', 'y_coor', 'radius', 'mean_intensity'])
    results.index = results.index + 1

    print(results)


start()

日志blob检测器返回一个数组,其中每一行表示一个blob。它给出了中心坐标(行和列)以及高斯博客宽度sigma。二维水滴的半径约为
sqrt(2)*sigma

import numpy as np
from skimage import draw, feature
import matplotlib.pyplot as plt

# Create a test image
image = np.zeros((200, 200))

# Parameters for test image blobs
positions_r, positions_c = (50, 100, 150), (50, 100, 150)
radii = (20, 15, 30)
values = (.5, .75, 1)

# We'll make the blobs vary a bit with noise, to make it
# more realistic.  Here we'll store their known average values.
blob_avg = []

for (r, c, radius, val) in zip(positions_r, positions_c,
                               radii, values):

    # Get the coordinates for a circle at the specified coordinates
    r_ix, c_ix = draw.circle(r, c, radius)

    # Generate values for all pixels inside the circle, varying
    # between val/2 and val.
    noisy_vals = val * 0.5 + np.random.random(size=len(r_ix)) / 2

    # Put those values into our test image
    image[r_ix, c_ix] = noisy_vals

    # Save the average value of this blob
    blob_avg.append(noisy_vals.mean())

# Compute the blobs in the image, setting the desired sigma range,
# and lowering the threshold so that we also grab our faintest blob
blobs_log = feature.blob_log(image, min_sigma=5, max_sigma=50,
                             threshold=0.3, num_sigma=50)

# `blob_log` returns the blobs in reverse order (in this case),
# so to compare with our test data we reverse the list of blob
# averages
blob_avg = blob_avg[::-1]

# Compute each blob's radius, by multiplying its sigma by sqrt(2)
blobs_log[:, 2] = blobs_log[:, 2] * np.sqrt(2)

# Create a plot, and display our test data
f, ax = plt.subplots(figsize=(15, 10))
ax.imshow(image, cmap='gray');

# Generate all row and column coordinates for our test image
# For an `(N, M)` test image, `ixs` will have shape `(N, M, 2)`,
# since it stores row and column coordinates.
ixs = np.indices(image.shape)

# Now, we plot each detected blob and estimate its average intensity

for i, blob in enumerate(blobs_log):
    y, x, r = blob
    c = plt.Circle((x, y), r, color='red', linewidth=2, fill=False)
    ax.add_patch(c)

    # Define an array of shape `[2, 1, 1]`, containing
    # the center of the blob
    blob_center = np.array([y, x])[:, np.newaxis, np.newaxis]

    # Using the formula for a circle, `x**2 + y**2 < r**2`,
    # generate a mask for this blob.
    mask = ((ixs - blob_center)**2).sum(axis=0) < r**2

    # Calculate the average intensity of pixels under the mask
    blob_avg_est = image[mask].mean()

    print(f'Blob {i} average value: true={blob_avg[i]:.2f}, estimated={blob_avg_est:.2f}')

嗨,Stefan,谢谢你的代码!消化需要一些时间,但我会马上开始的。嗨,斯蒂芬!我有别的事情要做,但我想为此感谢你。我已经能够在我的代码中成功地适应这一点,并且非常感谢您花时间帮助一个绝对的初学者。谢谢-肖恩
Blob 0 average value: true=0.75, estimated=0.75
Blob 1 average value: true=0.63, estimated=0.63
Blob 2 average value: true=0.50, estimated=0.49