def test_subpixel(fig=None): np.random.seed(2019) ims = np.random.rand(40, )**2 + 1e1 smooth_ims = smoothImage(ims) mpos = peak_local_max(smooth_ims, min_distance=3).flatten() subpos, subval = subpixelmax(smooth_ims, mpos) np.testing.assert_array_almost_equal( subpos, np.array([34.48945739, 19.7971219, 14.04429215, 7.17665281]), decimal=8) np.testing.assert_array_almost_equal( subval, np.array([10.75670888, 10.46787185, 10.59470934, 10.70051573]), decimal=8) if fig: plt.figure(fig) plt.clf() plt.plot(np.arange(ims.size), ims, '.:r', label='data points') plt.plot(mpos, ims[mpos], 'om', label='integer maxima') plt.plot(subpos, subval, '.g', markersize=15, label='subpixel maxima') plt.legend(numpoints=1) plt.close('all')
def fitBackground(im, smooth=True, fig=None, order=3, verbose=1, removeoutliers=False, returndict=None): """ Fit smooth background to 1D or 2D image Args: im (array): input image Returns vv (array): estimated background """ kk = len(im.shape) im = np.array(im) ny = im.shape[0] ydata0 = np.arange(0., ny) is1d = kk == 1 if kk > 1: # 2d signal nx = im.shape[1] xdata0 = np.arange(0., nx) else: # 1D signal xdata0 = [0.] xx, yy = np.meshgrid(xdata0, ydata0) if smooth: ims = smoothImage(im) else: ims = im if verbose: print('fitBackground: is1d %d, order %d' % (is1d, order)) xxf = xx.flatten() yyf = yy.flatten() imsf = ims.flatten() s2d = polyfit2d(xxf, yyf, imsf, order=order) vv = polyval2d(xx, yy, s2d) if removeoutliers: ww = im - vv gidx = np.abs(ims.flatten() - vv.flatten()) < ww.std() if verbose: print('fitBackGround: inliers %d/%d (std %.2f)' % (gidx.sum(), gidx.size, ww.std())) s2d = polyfit2d(xxf[gidx], yyf[gidx], imsf[gidx], order=order) vv = polyval2d(xx, yy, s2d) if not fig is None: if kk == 1: plt.figure(fig) plt.clf() plt.subplot(2, 1, 1) plt.plot(im, '.b', label='signal') plt.plot(ims, 'og') # plt.plot(ims, '.c', label='signal'); plt.plot(vv, '.-r', label='background') # plt.axis('image') plt.legend() plt.title('fitBackground: image') plt.subplot(2, 1, 2) plt.plot(ww, '.m') plt.title('Diff plot') else: plt.figure(fig) plt.clf() plt.subplot(3, 1, 1) plt.imshow(im, interpolation='nearest') plt.axis('image') plt.title('fitBackground: image') plt.subplot(3, 1, 2) plt.imshow(vv, interpolation='nearest') plt.axis('image') plt.title('fitBackground: interpolation') plt.subplot(3, 1, 3) plt.imshow(im - vv, interpolation='nearest') plt.axis('image') plt.title('fitBackground: difference') if not returndict is None: warnings.warn('please do not use this feature any more') returndict['xx'] = xx returndict['yy'] = yy returndict['ims'] = ims return vv
def findCrossTemplate(imx, ksize=31, fig=None, istep=2, verbose=1, widthmv=6, lenmv=20., sepmv=2.0, dy=5): """ Find crosses in image using template match Arguments --------- istep : float sampling rate in mV/pixel widthmv, lenmv, sepmv : float parameters of the cross model Returns ------- ptsim : array fitted points rr : numpy array response of the filter results : dict more results """ samplesize = np.array([ksize, ksize + dy]) param = [ None, None, sepmv / istep, 3 * np.pi / 8, -7 * np.pi / 8, 11 * np.pi / 8, np.pi / 8 ] modelpatch, _ = createCross(param, samplesize, w=widthmv / istep, l=lenmv / istep, lsegment=lenmv / istep, H=100) imtmp = pmatlab.setregion(scaleImage(imx), scaleImage(modelpatch), [0, 0]) #rr=cv2.matchTemplate(imx, modelpatch.astype(np.float32), method=cv2.TM_SQDIFF) rr = cv2.matchTemplate(scaleImage(imx), scaleImage(modelpatch.astype(imx.dtype)), method=cv2.TM_CCORR_NORMED) #rr=cv2.matchTemplate(scaleImage(imx), scaleImage(modelpatch.astype(np.float32)), method=cv2.TM_SQDIFF); rr=-rr rr = smoothImage(rr) thr = .65 * rr.max() + .35 * rr.mean() pts = localMaxima(rr, thr=thr, radius=10 / istep) pts = np.array(pts) pts = pts[[1, 0], :] ptsim = pts + ((samplesize - 1.) / 2).reshape((2, 1)) if verbose: print('findCrossTemplate: threshold: %.1f, %d local maxima' % (thr, pts.shape[1])) if fig is not None: showIm(imtmp, fig=fig) plt.plot(ptsim[0], ptsim[1], '.m', markersize=22) showIm(rr, fig=fig + 1) plt.colorbar() plt.title('Template and image') plt.plot(pts[0], pts[1], '.m', markersize=22) plt.title('Template match') qtt.pgeometry.tilefigs([fig, fig + 1]) return ptsim, rr, dict({'modelpatch': modelpatch})