def detect_objects_heatmap(heatmap):
data = 256 * heatmap
data_max = filters.maximum_filter(data, 3)
maxima = (data == data_max)
data_min = filters.minimum_filter(data, 3)
diff = ((data_max - data_min) > 0.3)
maxima[diff == 0] = 0
labeled, num_objects = ndimage.label(maxima)
slices = ndimage.find_objects(labeled)
objects = np.zeros((num_objects, 2), dtype=np.int32)
pidx = 0
for (dy, dx) in slices:
pos = [(dy.start + dy.stop - 1) // 2, (dx.start + dx.stop - 1) // 2]
if heatmap[pos[0], pos[1]] > config.CENTER_TR:
objects[pidx, :] = pos
pidx += 1
return objects[:pidx]
python类minimum_filter()的实例源码
def extrema(mat,mode='wrap',window=10): # function to find the pressure extrema
"""
Find the indices of local extrema (min and max) in the input array.
Parameters
mat (input array)
mode
window (sensitivity)
Returns
Indices of extrema
"""
mn = minimum_filter(mat, size=window, mode=mode)
mx = maximum_filter(mat, size=window, mode=mode)
# (mat == mx) true if pixel is equal to the local max
# (mat == mn) true if pixel is equal to the local in
# Return the indices of the maxima, minima
return np.nonzero(mat == mn), np.nonzero(mat == mx)
#function to interpolate data to given level(s) using np.interp
def r_erosion(image,size,origin=0):
"""Erosion with rectangular structuring element using maximum_filter"""
return filters.minimum_filter(image,size,origin=origin)
def rg_erosion(image,size,origin=0):
"""Grayscale erosion with maximum/minimum filters."""
return filters.minimum_filter(image,size,origin=origin)
def _findObject(self, img):
'''
Create a bounding box around the object within an image
'''
from imgProcessor.imgSignal import signalMinimum
# img is scaled already
i = img > signalMinimum(img) # img.max()/2.5
# filter noise, single-time-effects etc. from mask:
i = minimum_filter(i, 4)
return boundingBox(i)
def result(self):
return self._m.avg
# return minimum_filter(self._m.avg,self.ksize)
def mask(self):
return self._m.n > 0
# return minimum_filter(self._m.n>0,self.ksize)
def autofind_pois(self, neighborhood_size=1, min_threshold=10000, max_threshold=1e6):
"""Automatically search the xy scan image for POIs.
@param neighborhood_size: size in microns. Only the brightest POI per neighborhood will be found.
@param min_threshold: POIs must have c/s above this threshold.
@param max_threshold: POIs must have c/s below this threshold.
"""
# Calculate the neighborhood size in pixels from the image range and resolution
x_range_microns = np.max(self.roi_map_data[:, :, 0]) - np.min(self.roi_map_data[:, :, 0])
y_range_microns = np.max(self.roi_map_data[:, :, 1]) - np.min(self.roi_map_data[:, :, 1])
y_pixels = len(self.roi_map_data)
x_pixels = len(self.roi_map_data[1, :])
pixels_per_micron = np.max([x_pixels, y_pixels]) / np.max([x_range_microns, y_range_microns])
# The neighborhood in pixels is nbhd_size * pixels_per_um, but it must be 1 or greater
neighborhood_pix = int(np.max([math.ceil(pixels_per_micron * neighborhood_size), 1]))
data = self.roi_map_data[:, :, 3]
data_max = filters.maximum_filter(data, neighborhood_pix)
maxima = (data == data_max)
data_min = filters.minimum_filter(data, 3 * neighborhood_pix)
diff = ((data_max - data_min) > min_threshold)
maxima[diff is False] = 0
labeled, num_objects = ndimage.label(maxima)
xy = np.array(ndimage.center_of_mass(data, labeled, range(1, num_objects + 1)))
for count, pix_pos in enumerate(xy):
poi_pos = self.roi_map_data[pix_pos[0], pix_pos[1], :][0:3]
this_poi_key = self.add_poi(position=poi_pos, emit_change=False)
self.rename_poi(poikey=this_poi_key, name='spot' + str(count), emit_change=False)
# Now that all the POIs are created, emit the signal for other things (ie gui) to update
self.signal_poi_updated.emit()
def r_erosion(image,size,origin=0):
"""Erosion with rectangular structuring element using maximum_filter"""
return filters.minimum_filter(image,size,origin=origin)
def rg_erosion(image,size,origin=0):
"""Grayscale erosion with maximum/minimum filters."""
return filters.minimum_filter(image,size,origin=origin)
def r_erosion(image,size,origin=0):
"""Erosion with rectangular structuring element using maximum_filter"""
return filters.minimum_filter(image,size,origin=origin)
def rg_erosion(image,size,origin=0):
"""Grayscale erosion with maximum/minimum filters."""
return filters.minimum_filter(image,size,origin=origin)
def addImg(self, img, maxShear=0.015, maxRot=100, minMatches=12,
borderWidth=3): # borderWidth=100
"""
Args:
img (path or array): image containing the same object as in the reference image
Kwargs:
maxShear (float): In order to define a good fit, refect higher shear values between
this and the reference image
maxRot (float): Same for rotation
minMatches (int): Minimum of mating points found in both, this and the reference image
"""
try:
fit, img, H, H_inv, nmatched = self._fitImg(img)
except Exception as e:
print(e)
return
# CHECK WHETHER FIT IS GOOD ENOUGH:
(translation, rotation, scale, shear) = decompHomography(H)
print('Homography ...\n\ttranslation: %s\n\trotation: %s\n\tscale: %s\n\tshear: %s'
% (translation, rotation, scale, shear))
if (nmatched > minMatches
and abs(shear) < maxShear
and abs(rotation) < maxRot):
print('==> img added')
# HOMOGRAPHY:
self.Hs.append(H)
# INVERSE HOMOGRSAPHY
self.Hinvs.append(H_inv)
# IMAGES WARPED TO THE BASE IMAGE
self.fits.append(fit)
# ADD IMAGE TO THE INITIAL flatField ARRAY:
i = img > self.signal_ranges[-1][0]
# remove borders (that might have erroneous light):
i = minimum_filter(i, borderWidth)
self._ff_mma.update(img, i)
# create fit img mask:
mask = fit < self.signal_ranges[-1][0]
mask = maximum_filter(mask, borderWidth)
# IGNORE BORDER
r = self.remove_border_size
if r:
mask[:r, :] = 1
mask[-r:, :] = 1
mask[:, -r:] = 1
mask[:, :r] = 1
self._fit_masks.append(mask)
# image added
return fit
return False
def addImg(self, i):
img = self._read(i)
if self._first:
img = self._firstImg(img)
elif self.scale_factor != 1:
img = rescale(img, self.scale_factor)
try:
f = FitHistogramPeaks(img)
except AssertionError:
return
#sp = getSignalPeak(f.fitParams)
mn = getSignalMinimum(f.fitParams)
# non-backround indices:
ind = img > mn # sp[1] - self.nstd * sp[2]
# blur:
# blurred = minimum_filter(img, 3)#remove artefacts
#blurred = maximum_filter(blurred, self.ksize)
# blurred = img
# gblurred = gaussian_filter(img, self.ksize)
# ind = minimum_filter(ind, self.ksize)
nind = np.logical_not(ind)
gblurred = maskedFilter(img, nind, ksize=2 * self.ksize,
fill_mask=False,
fn="mean")
#blurred[ind] = gblurred[ind]
# scale [0-1]:
mn = img[nind].mean()
if np.isnan(mn):
mn = 0
mx = gblurred[ind].max()
gblurred -= mn
gblurred /= (mx - mn)
# img -= mn
# img /= (mx - mn)
# ind = np.logical_and(ind, img > self._m.avg)
self._m.update(gblurred, ind)
self.bglevel.append(mn)
self._mx += mx
self._n += 1
# import pylab as plt
# plt.imshow(self._m.avg)
# plt.show()