gpt4 book ai didi

python - 在 python 中使用图像处理计算粒子

转载 作者:太空狗 更新时间:2023-10-29 21:43:43 28 4
gpt4 key购买 nike

是否有任何好的算法来检测背景强度不断变化的粒子?例如,如果我有以下图像:

fluorescence

即使左下角出现明显不同的背景,有没有办法计算白色小颗粒的数量?

为了更清楚一点,我想标记图像并使用发现这些粒子很重要的算法对粒子进行计数:

labeled_particles

我用 PILcvscipynumpy 等模块尝试了很多东西。我从 this very similar SO question 得到了一些提示,乍一看,您似乎可以像这样采用一个简单的阈值:

im = mahotas.imread('particles.jpg')
T = mahotas.thresholding.otsu(im)

labeled, nr_objects = ndimage.label(im>T)
print nr_objects
pylab.imshow(labeled)

但是由于不断变化的背景,你会得到这个: bad_threshold_image

我也尝试过其他的想法,比如a technique I found for measuring paws ,我是这样实现的:

import numpy as np
import scipy
import pylab
import pymorph
import mahotas
from scipy import ndimage
import cv


def detect_peaks(image):
"""
Takes an image and detect the peaks usingthe local maximum filter.
Returns a boolean mask of the peaks (i.e. 1 when
the pixel's value is the neighborhood maximum, 0 otherwise)
"""

# define an 8-connected neighborhood
neighborhood = ndimage.morphology.generate_binary_structure(2,2)

#apply the local maximum filter; all pixel of maximal value
#in their neighborhood are set to 1
local_max = ndimage.filters.maximum_filter(image, footprint=neighborhood)==image
#local_max is a mask that contains the peaks we are
#looking for, but also the background.
#In order to isolate the peaks we must remove the background from the mask.

#we create the mask of the background
background = (image==0)

#a little technicality: we must erode the background in order to
#successfully subtract it form local_max, otherwise a line will
#appear along the background border (artifact of the local maximum filter)
eroded_background = ndimage.morphology.binary_erosion(background, structure=neighborhood, border_value=1)

#we obtain the final mask, containing only peaks,
#by removing the background from the local_max mask
detected_peaks = local_max - eroded_background

return detected_peaks

im = mahotas.imread('particles.jpg')
imf = ndimage.gaussian_filter(im, 3)
#rmax = pymorph.regmax(imf)
detected_peaks = detect_peaks(imf)
pylab.imshow(pymorph.overlay(im, detected_peaks))
pylab.show()

但这也没有运气,显示了这个结果:

bad_result_from_detect_peaks

使用区域最大函数,我得到的图像几乎似乎给出了正确的粒子识别,但根据我的高斯滤波,错误点上的粒子太多或太少(图像的高斯滤波器为 2, 3, & 4):

gaussian of 2 gaussian of 3 gaussian of 4

此外,它还需要处理与此类似的图像:

fluorescence

这与上面的图像类型相同,只是粒子密度高得多。

编辑:已解决的解决方案:我能够使用以下代码为这个问题找到一个体面的工作解决方案:

import cv2
import pylab
from scipy import ndimage

im = cv2.imread('particles.jpg')
pylab.figure(0)
pylab.imshow(im)

gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5,5), 0)
maxValue = 255
adaptiveMethod = cv2.ADAPTIVE_THRESH_GAUSSIAN_C#cv2.ADAPTIVE_THRESH_MEAN_C #cv2.ADAPTIVE_THRESH_GAUSSIAN_C
thresholdType = cv2.THRESH_BINARY#cv2.THRESH_BINARY #cv2.THRESH_BINARY_INV
blockSize = 5 #odd number like 3,5,7,9,11
C = -3 # constant to be subtracted
im_thresholded = cv2.adaptiveThreshold(gray, maxValue, adaptiveMethod, thresholdType, blockSize, C)
labelarray, particle_count = ndimage.measurements.label(im_thresholded)
print particle_count
pylab.figure(1)
pylab.imshow(im_thresholded)
pylab.show()

这将显示如下图像:

particles_given(这是给定的图像)

counted_particles

(这是计数的粒子)

并计算粒子数为 60。

最佳答案

我通过使用称为自适应对比度的技术调整差异阈值解决了“背景中的可变亮度”问题。它的工作原理是将灰度图像与自身的模糊版本进行线性组合(在本例中为差异),然后对其应用阈值。

  1. 使用合适的统计运算符对图像进行卷积。
  2. 从卷积图像中减去原始图像,必要时校正强度标度/ Gamma 。
  3. 用常数对差异图像进行阈值处理。

( original paper )

我在浮点域中使用 scipy.ndimage 非常成功地做到了这一点(比整数图像处理的结果更好),如下所示:

original_grayscale = numpy.asarray(some_PIL_image.convert('L'), dtype=float)
blurred_grayscale = scipy.ndimage.filters.gaussian_filter(original_grayscale, blur_parameter)
difference_image = original_grayscale - (multiplier * blurred_grayscale);
image_to_be_labeled = ((difference_image > threshold) * 255).astype('uint8') # not sure if it is necessary

labelarray, particle_count = scipy.ndimage.measurements.label(image_to_be_labeled)

希望这对您有所帮助!

关于python - 在 python 中使用图像处理计算粒子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16110649/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com