cha*_*ase 8 python opencv image python-imaging-library
在变化的背景强度下检测粒子有什么好的算法吗?例如,如果我有以下图像:
有没有办法计算小的白色颗粒,即使左下方出现明显不同的背景?
为了更清楚一点,我想标记图像并使用一种算法来计算粒子,这些算法会发现这些粒子很重要:
我已经尝试了许多事情的PIL
,cv
,scipy
,numpy
,等模块.我从这个非常相似的SO问题中得到了一些提示,乍看之下你可以采取一个简单的阈值:
im = mahotas.imread('particles.jpg')
T = mahotas.thresholding.otsu(im)
labeled, nr_objects = ndimage.label(im>T)
print nr_objects
pylab.imshow(labeled)
Run Code Online (Sandbox Code Playgroud)
但由于背景的变化,你得到了这个:
我也尝试了其他的想法,比如我用来测量爪子的技术,我用这种方式实现了:
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()
Run Code Online (Sandbox Code Playgroud)
但这也没有运气,显示了这个结果:
使用区域最大函数,我得到几乎看起来正确的粒子识别的图像,但根据我的高斯滤波,图像中有太多或太少的粒子在错误的位置(图像有高斯滤波器2,3,& 4):
此外,它还需要处理与此类似的图像:
这是上面相同类型的图像,只是在更高密度的粒子上.
编辑:解决方案: 我能够使用以下代码获得一个体面的解决此问题的解决方案:
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()
Run Code Online (Sandbox Code Playgroud)
这将显示如下图像:
(这是给定的图像)
和
(这是计算的粒子)
并计算粒子数为60.
我已经通过使用一种称为“自适应对比度”的技术,通过调整差异阈值解决了“背景中的可变亮度”问题。它通过对本身具有模糊版本的灰度图像执行线性组合(在情况下有所不同),然后对其应用阈值来工作。
(原纸)
我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)
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!!
归档时间: |
|
查看次数: |
11397 次 |
最近记录: |