是否有一个OpenCV (安卓)实现“滚动球”背景减法算法,发现在ImageJ: Process->减法背景?
OpenCV有一个BackgroundSubtractorMOG类,但是它用于视频流而不是单个独立的图像。
这是这个方法所做的一个例子:http://imgur.com/8SN2CFz
下面是这个过程的文档:背景
发布于 2015-05-06 01:02:23
据我所知,OpenCV C库中没有实现,而Android包装器只是主要库的包装器。
尽管如此,ImageJ实现的源代码是在线的这里,因此您应该能够将其直接集成到安卓图像处理管道中。
有一些关于滚动球与使用磁盘结构元素(在OpenCV中可用) 这里的相对优点的讨论。
如果你绝对需要滚球和OpenCV,那么不幸的是它没有“开箱即用”。
发布于 2017-10-11 08:22:08
编辑:在本文中使用该方法之前,阅读下面的评论,并考虑@renat和@David的答案。
万一有人还在寻找巨蟒中滚动的背景校正。对我来说,下面的工作效果很好。
以下是单色图像的一些代码:
import scipy.ndimage as scim
from scipy.misc import imsave
from skimage.morphology import ball
# Read image
im = scim.imread("path")[:, :, 0].astype(int)
# Create 3D ball with radius of 50 and a diameter of 2*50+1
s = ball(50)
# Take only the upper half of the ball
h = s.shape[1] // 2 + 1 # 50 + 1
# Flatten the 3D ball to a weighted 2D disc
s = s[:h, :, :].sum(axis=0)
# Rescale weights into 0-255
s = (255 * (s - s.min())) / (s.max() - s.min())
# Use im-opening(im,ball) (i.e. white tophat transform) (see original publication)
im_corr = scim.white_tophat(im, structure=s)
# Save corrected image
imsave('outfile', im_corr)这给出的结果与imagej实现不完全相同,但结果非常相似。在我的例子中,既有好的也有坏的校正区域。整体颜色强度较高。
发布于 2018-05-08 19:52:37
基于@Xenthor的回答--这是我想出的:
import numpy as np
import scipy.ndimage as ndi
from scipy.ndimage._ni_support import _normalize_sequence
def rolling_ball_filter(data, ball_radius, spacing=None, top=False, **kwargs):
"""Rolling ball filter implemented with morphology operations
This implenetation is very similar to that in ImageJ and uses a top hat transform
with a ball shaped structuring element
https://en.wikipedia.org/wiki/Top-hat_transform
Parameters
----------
data : ndarray
image data (assumed to be on a regular grid)
ball_radius : float
the radius of the ball to roll
spacing : int or sequence
the spacing of the image data
top : bool
whether to roll the ball on the top or bottom of the data
kwargs : key word arguments
these are passed to the ndimage morphological operations
Returns
-------
data_nb : ndarray
data with background subtracted
bg : ndarray
background that was subtracted from the data
"""
ndim = data.ndim
if spacing is None:
spacing = 1
spacing = _normalize_sequence(spacing, ndim)
radius = np.asarray(_normalize_sequence(ball_radius, ndim))
mesh = np.array(np.meshgrid(*[np.arange(-r, r + s, s) for r, s in zip(radius, spacing)], indexing="ij"))
structure = 2 * np.sqrt(1 - ((mesh / radius.reshape(-1, *((1,) * ndim)))**2).sum(0))
structure[~np.isfinite(structure)] = 0
if not top:
# ndi.white_tophat(data, structure=structure, output=background)
background = ndi.grey_erosion(data, structure=structure, **kwargs)
background = ndi.grey_dilation(background, structure=structure, **kwargs)
else:
# ndi.black_tophat(data, structure=structure, output=background)
background = ndi.grey_dilation(data, structure=structure, **kwargs)
background = ndi.grey_erosion(background, structure=structure, **kwargs)
return data - background, backgroundhttps://stackoverflow.com/questions/29320954
复制相似问题