def register(frames, template, nlevels=7): """ Use DTCWT registration to return warped versions of frames aligned to template. """ # Normalise template tmpl_min = template.min() norm_template = template - tmpl_min tmpl_max = norm_template.max() norm_template /= tmpl_max # Transform template transform = dtcwt.Transform2d() template_t = transform.forward(norm_template, nlevels=nlevels) warped_ims = [] i = 0 for frame_idx in xrange(frames.shape[2]): logging.info('Registering frame {0}/{1}'.format(frame_idx+1, frames.shape[2])) frame = frames[:,:,frame_idx] # Normalise frame norm_frame = frame - tmpl_min norm_frame /= tmpl_max # Transform frame frame_t = transform.forward(norm_frame, nlevels=nlevels) # Register reg = dtcwt.registration.estimatereg(frame_t, template_t) ex = dtcwt.registration.warp(frame, reg, method='bilinear') if i == 0: im = Image.fromarray(tonemap(ex).copy(), 'L') im.show() kernel = np.zeros( (13,13), np.float32) kernel[4,4] = 2.0 boxFilter = np.ones( (13,13), np.float32) / 169.0 #Subtract the two: kernel = kernel - boxFilter imgIn = Image.fromarray(tonemap(ex).copy(), 'L') custom = cv2.filter2D(ex, -1, kernel) data = np.asarray(custom) # Extract data and store as floating point data sharpened = np.array(data, dtype=np.float32).reshape(im.size[::-1]) ''' blurred_f = ndimage.gaussian_filter(ex, 3) filter_blurred_f = ndimage.gaussian_filter(blurred_f, 1) alpha = 30 sharpened = blurred_f + alpha * (blurred_f - filter_blurred_f) ''' final_img = scipy.signal.wiener(sharpened) if i == 0: im = Image.fromarray(tonemap(final_img).copy(), 'L') im.show() warped_ims.append(final_img) i += 1 return np.dstack(warped_ims)
def register(frames, template, nlevels=7): """ Use DTCWT registration to return warped versions of frames aligned to template. """ # Normalise template tmpl_min = template.min() norm_template = template - tmpl_min tmpl_max = norm_template.max() norm_template /= tmpl_max # Transform template transform = dtcwt.Transform2d() template_t = transform.forward(norm_template, nlevels=nlevels) warped_ims = [] for frame_idx in xrange(frames.shape[2]): logging.info('Registering frame {0}/{1}'.format( frame_idx + 1, frames.shape[2])) frame = frames[:, :, frame_idx] # Normalise frame norm_frame = frame - tmpl_min norm_frame /= tmpl_max # Transform frame frame_t = transform.forward(norm_frame, nlevels=nlevels) # Register reg = dtcwt.registration.estimatereg(frame_t, template_t) warped_ims.append( dtcwt.registration.warp(frame, reg, method='bilinear')) return np.dstack(warped_ims)
def magnify_motions_2d(data, k=8., width=70): nlevels = 8 tr = dtcwt.Transform2d() pyramids = list() n = np.shape(data)[0] print('Forward DTCWT...', end=' ', flush=1) for i in range(0, n): pyramids.append(tr.forward(data[i, :, :], nlevels=nlevels)) print('DONE') print('Modifying phase...', end=' ', flush=1) for level in range(0, nlevels): phase = get_phases(pyramids, level) phase0 = flattop_filter1d(phase, width, axis=0, mode='reflect') phase = phase0 + (phase - phase0) * k phase = flattop_filter1d(phase, 2.0, axis=0, mode='reflect') for i in range(0, n): h = pyramids[i].highpasses[level] abs_value = np.abs(h).flatten() h = abs_value * np.exp(1j * phase[i, :]) pyramids[i].highpasses[level][:] = np.reshape( h, np.shape(pyramids[i].highpasses[level][:])) result = np.empty_like(data) print('DONE') print('Inverse DTCWT...', end=' ', flush=1) for i in range(0, n): result[i, :, :] = tr.inverse(pyramids[i]) print('DONE') return (result)
def test_batch_input_tuple(nlevels, include_scale, batch_size): in_ = tf.placeholder(tf.float32, [batch_size, 512, 512]) t = dtcwt.Transform2d() if include_scale: Yl, Yh, Yscale = unpack( t.forward_channels(in_, "nhw", nlevels, include_scale), "tf") else: Yl, Yh = unpack(t.forward_channels(in_, "nhw", nlevels, include_scale), "tf") # At level 1, the lowpass output will be the same size as the input. At # levels above that, it will be half the size per level extent = 512 * 2**(-(nlevels - 1)) assert Yl.get_shape().as_list() == [batch_size, extent, extent] assert Yl.dtype == tf.float32 for i in range(nlevels): extent = 512 * 2**(-(i + 1)) assert Yh[i].get_shape().as_list() == [batch_size, extent, extent, 6] assert Yh[i].dtype == tf.complex64 if include_scale: assert (Yscale[i].get_shape().as_list() == [ batch_size, 2 * extent, 2 * extent ]) assert Yscale[i].dtype == tf.float32
def procChanDTCWT(img, wavelet, nLevels): fv = [] transform = dtcwt.Transform2d() wt = transform.forward(img, nlevels=nLevels) for highpass in wt.highpasses: fv.append(np.std(highpass)) fv.append(np.mean(np.abs(highpass - np.mean(highpass)))) return fv
def dtcwt2d(img_input, level=6): ''' Input: 2D image (img_input). number of decomposition levels (level). Output: ''' trans = dtcwt.Transform2d() img_trans = trans.forward(img_input, nlevels=level) return img_trans
def dtcwt3d(mat_input, level=6): ''' Input: Output: ''' depth, _, _ = np.shape(mat_input) trans = dtcwt.Transform2d() output = list() for cross in range(depth): output.append(trans.forward(mat_input[cross], nlevels=level)) return output
def merge_dtcwt(first, second, levels=None, sigma=None): """Focus-stack two images using the dual-tree complex wavelet transform (DTCWT). Parameters ---------- first : array_like First input image. second : array_like Second input image. levels : int Number of levels to use in the DTCWT. sigma : float Standard deviation of lowpass filter used to smooth the mixing weights. Returns ------- stacked : numpy.ndarray Focus-stacked result. """ if levels is None: levels = 3 if sigma is None: sigma = 1.5 transform = dtcwt.Transform2d() def merge_channel(fc, sc): fc = transform.forward(fc, nlevels=levels) sc = transform.forward(sc, nlevels=levels) fc.lowpass[:] = 0.5 * (fc.lowpass + sc.lowpass) for f, s in zip(fc.highpasses, sc.highpasses): mix = 1.0 * (np.abs(f) > np.abs(s)) mix = np.median(mix, axis=2) mix = ndimage.gaussian_filter(mix, sigma=sigma) mix = mix[:, :, np.newaxis] f[:] = mix * f + (1 - mix) * s return transform.inverse(fc) if first.ndim == 2: return merge_channel(first, second) return np.dstack([ merge_channel(first[:, :, c], second[:, :, c]) for c in range(first.ndim) ])
def transform_frames(frames, nlevels=7): # Transform each registered frame storing result lowpasses = [] highpasses = [] for idx in xrange(nlevels): highpasses.append([]) transform = dtcwt.Transform2d() for frame_idx in xrange(frames.shape[2]): logging.info('Transforming frame {0}/{1}'.format(frame_idx+1, frames.shape[2])) frame = frames[:,:,frame_idx] frame_t = transform.forward(frame, nlevels=nlevels) lowpasses.append(frame_t.lowpass) for idx in xrange(nlevels): highpasses[idx].append(frame_t.highpasses[idx][:,:,:,np.newaxis]) return np.dstack(lowpasses), tuple(np.concatenate(hp, axis=3) for hp in highpasses)
def rsmwt3d(mat_input, level=6): ''' Variant version from DT-CWT. Check it at: https://dtcwt.readthedocs.org/en/0.11.0/variant.html Input: Output: ''' depth, _, _ = np.shape(mat_input) trans = dtcwt.Transform2d(biort='near_sym_b_bp', qshift='qshift_b_bp') output = list() for cross in range(depth): output.append(trans.forward(mat_input[cross], nlevels=level)) return output
def apply_dt_wavelets(img, param): # Compute 5 levels of dtcwt with the antonini/qshift settings input_shape = img.shape transform = dtcwt.Transform2d(biort='antonini', qshift='qshift_06') t = transform.forward(img, nlevels=len(param)) for level, ratio in param.items(): data = t.highpasses[level - 1] if ratio < 1: norm = np.absolute(data) # 1 keeps 100% of the coefficients, 0 keeps 0% of the coeff thresh = np.percentile(norm, 100 * (1 - ratio)) # Proximity operator for L1,2 norm data[:, :, :] = np.where(norm < thresh, 0, (norm - thresh) * np.exp(1j * np.angle(data))) else: # Just applying gain for this level data *= ratio ret = transform.inverse(t) # in some cases dtcwt does reshape the image for performance purpose return ret[:input_shape[0], :input_shape[1]]
def test_multichannel(nlevels, channels): in_ = tf.placeholder(tf.float32, [None, 512, 512, channels]) t = dtcwt.Transform2d() Yl, Yh, Yscale = unpack( t.forward_channels(in_, "nhwc", nlevels, include_scale=True), "tf") # At level 1, the lowpass output will be the same size as the input. At # levels above that, it will be half the size per level extent = 512 * 2**(-(nlevels - 1)) assert Yl.get_shape().as_list() == [None, extent, extent, channels] assert Yl.dtype == tf.float32 for i in range(nlevels): extent = 512 * 2**(-(i + 1)) assert (Yh[i].get_shape().as_list() == [ None, extent, extent, channels, 6 ]) assert Yh[i].dtype == tf.complex64 assert Yscale[i].get_shape().as_list() == [ None, 2 * extent, 2 * extent, channels ] assert Yscale[i].dtype == tf.float32
def test_batch_input(nlevels, include_scale, batch_size): in_ = tf.placeholder(tf.float32, [batch_size, 512, 512]) t = dtcwt.Transform2d() p = t.forward_channels(in_, "nhw", nlevels, include_scale) # At level 1, the lowpass output will be the same size as the input. At # levels above that, it will be half the size per level extent = 512 * 2**(-(nlevels - 1)) assert p.lowpass_op.get_shape().as_list() == [batch_size, extent, extent] assert p.lowpass_op.dtype == tf.float32 for i in range(nlevels): extent = 512 * 2**(-(i + 1)) assert (p.highpasses_ops[i].get_shape().as_list() == [ batch_size, extent, extent, 6 ]) assert p.highpasses_ops[i].dtype == tf.complex64 if include_scale: assert (p.scales_ops[i].get_shape().as_list() == [ batch_size, 2 * extent, 2 * extent ]) assert p.scales_ops[i].dtype == tf.float32
# dtBinMeans[i]=np.mean(imageLn[np.absolute(dt-i)<1.0e-3]) #dtBinMeans[np.isnan(dtBinMeans)]=0.0 # #imageDD=np.zeros_like(imageLn) #dtMeanFlat=np.mean(dtBinMeans[-1]) #for i in range(dtRange): # imageDD[dt.astype(np.int64)==i]=dtBinMeans[i] #imageDD[dt>=dtRange]=dtMeanFlat # ## Subtract the dry drift from the image before DTCWT, to remove some of the bias in the power estimates #imageLn-=imageDD # Do / do not use modified wavelets to achieve directional invariance at the cost of # inaccurate reconstruction. #transform = dtcwt.Transform2d(biort='near_sym_b_bp', qshift='qshift_b_bp') transform = dtcwt.Transform2d(biort='near_sym_b', qshift='qshift_b') dataT = transform.forward(imageLn, nlevels=nLevels) dataRndT = transform.forward(imageLnRnd, nlevels=nLevels) coeffs = dataT.highpasses coeffsRnd = dataRndT.highpasses ln2Scales = [] # Adjustment of noise image using fraction of power in each sub-band. fracPowInOri = np.empty((nLevels, 6)) fracPowInOriRnd = np.empty((nLevels, 6)) for iLev in range(nLevels): mask = np.sum(np.absolute(dataT.highpasses[iLev][:, :, :]), axis=2) < 1.0e-4 powInLev = np.mean(np.square(np.absolute( dataT.highpasses[iLev][~mask, :])))
def reconstruct(lowpass, highpasses): transform = dtcwt.Transform2d() t = dtcwt.Pyramid(lowpass, highpasses) return transform.inverse(t)
import dtcwt import dtcwt.registration as registration from utility.cv_utils import * transform2d = dtcwt.Transform2d() # warped_src = registration.warp(src, reg, method='bilinear') # vxs, vys = registration.velocityfield(reg, ref.shape[:2], method='bilinear') # vxs = vxs*ref.shape[1] # vys = vys*ref.shape[0] # figure() # X, Y = np.meshgrid(np.arange(ref.shape[1]), np.arange(ref.shape[0])) # imshow(ref, cmap=cm.gray, clim=(0,1)) # step = 8 # quiver(X[::step,::step], Y[::step,::step],vxs[::step,: # :step], vys[::step,::step],color='g', angles='xy', scale_units='xy', scale=0.25) def transform_dtcwt(ref, src): ref_t = transform2d.forward(ref, nlevels=4) src_t = transform2d.forward(src, nlevels=4) reg = registration.estimatereg(src_t, ref_t) vxs, vys = registration.velocityfield(reg, ref.shape[:2], method='nearest') mesh = np.sqrt(vxs * vxs + vys * vys) return mesh def dtcwt3d_layer(input_shape): f = lambda X: transform2d.forward(X, 2).lowpass
def get_vecs(self,im): thres = self.thres good_sz = 2**np.round(np.log2(im.shape)) im = imresize(im,[ int(x) for x in good_sz ]) #plt.figure(0,figsize=(2,2)) #plt.imshow(im,interpolation='none') #plt.show() T = dtcwt.Transform2d() Im = T.forward(im,nlevels=3) M = Im.highpasses[0].shape[1] # max size block = np.zeros([M,M,3,6]) vecs = [] Ihp = Im.highpasses for i, hp in enumerate(Ihp): for x in range(hp.shape[0]): for y in range(hp.shape[1]): cur_i = [ [i,x,y] ] neighs = [ [i,x+xx,y+yy] for xx,yy in zip ([1,1,-1,-1],[1,-1,1,-1])] sons = [ [i-1,2*x+xx,2*y+yy] for xx,yy in zip([1,0,1,0],[0,0,1,1]) ] parent = [ [i+1,x/2,y/2] ] all = [cur_i, neighs, sons, parent] vec = [] for j in all: for k in j: #print 'k',k ii,xx,yy = k if ii>=0 and ii<len(Ihp) and xx>=0 and xx<Ihp[ii].shape[0] and yy>=0 and yy<Ihp[ii].shape[1]: vec.append(Ihp[ii][xx,yy,:]) #else: # vec.append([]) if len(vec)==10: # full neighborhood vecs.append(vec) # now we have the vectors of the elements where each item contains 10 complex entries for each orientation mags = [] angs = [] #print vecs for v in vecs: mags.append( [ np.abs(x) for x in v ]) angs.append( [ np.angle(x) for x in v ]) mags = np.array(mags) angs = np.array(angs) # fit gmm mags0 =np.reshape(mags[:,0,:],[-1,1]) smags = np.sort(mags0,axis=0) #print smags thressed = smags[int(smags.shape[0]*(1.0-thres))] #print thres angs = angs[np.mean(mags[:,0,:],axis=1)>thressed,:,:] #print angs.shape #plt.hist(mags0) #plt.gca().invert_yaxis() #plt.show() #print mags0 return angs
#print(path) lenaR = cv2.imread(path) #print(lena.shape) lena = cv2.imread(path, 0) #print(lena) x = 1 lena = lena.astype('float32') # Parse the lena file and rescale to be in the range (0,1] lena = lena / 225 lena -= np.mean(lena, axis=0) lena /= np.std(lena, axis=0) #s=np.shape(lena) #print (lena.shape) nlevels = 3 orientation = 6 transform = dtcwt.Transform2d('near_sym_b', 'qshift_b') t = transform.forward(lena, nlevels) p = [] for i in range(nlevels): for slice_idx in range(orientation): p.append(np.abs(t.highpasses[i][:, :, slice_idx])) #print(np.abs(t.highpasses[i][:,:,slice_idx]).shape) XR = np.array(p) #print (XL[23].shape) ########################################RL################################################################### pathr = ( '/mnt/e/documents/NSSADNN_IQA-master/data_NSS_test/distortedimage_L/' + line1)
def wlsZeros(noiseIn, thresh0, maxSc, minSc=1, qmin=0., qmax=8., nq=81, decompmode='dtcwt', minZeroDist=10, mode='once', zeroMask=False): d = 2. ny = noiseIn.shape[0] nx = noiseIn.shape[1] nLevelsMax = int(np.floor(max(np.log2(ny), np.log2(nx)))) minScale = minSc maxScale = min(nLevelsMax, maxSc) qstep = (qmax - qmin) / nq qvals = np.linspace(qmin, qmax, num=nq, endpoint=True) noise = np.copy(noiseIn) # Compute distance transform from zero mask # Set "zeros" to nan, so that that zero-contaminated coefficients can be identified if zeroMask == True: nzMask = noise < thresh0 noise[ noise < thresh0] = thresh0 # for those values that don't exceed minZeroDist dt = scipy.ndimage.morphology.distance_transform_edt(nzMask) # Only set to nan if distance is greater than minZeroDist from a non-zero noise[dt > minZeroDist] = np.nan ln2Scales = [] oriMax = [] if decompmode == 'dtcwt': # Compute DTDWT # Do not use rotationally-invariant transform. Adantage is much shorter filters. Disadvantage is that max mod over ori may not be meaningful? # Use length 10 'a' filters here, rather than the length 14 'b' (or length 18 'b_bp'). # transform = dtcwt.Transform2d(biort='near_sym_b_bp', qshift='qshift_b_bp') transform = dtcwt.Transform2d(biort='near_sym_a', qshift='qshift_a') dataT = transform.forward(noise, nlevels=maxScale + 1) for iLev in range(maxScale + 1): hp = dataT.highpasses[iLev] ln2Scales.append(iLev + 1) # first set of detail coeffs has scale=2 with warnings.catch_warnings(): warnings.filterwarnings('ignore') oriMax.append(np.nanmax(np.absolute(hp), axis=2)) elif decompmode == 'dwt': dataT = pywt.wavedec2(noise, 'sym4', mode='periodization') for iPos in range(len(dataT) - 1, 0, -1): iLev = len(dataT) - 1 - iPos arrs = dataT[iPos] arrsStack = np.abs(np.stack(arrs, axis=2)) tmp = np.nanmax(np.abs(arrsStack), axis=2) # Now do single-level approxiation to the 'leaders' bit tmp = scipy.ndimage.maximum_filter(tmp, size=3) oriMax.append(tmp) ln2Scales.append(iLev + 1) # first set of detail coeffs has scale=2 else: raise Exception("Invalid mode:", mode) # Convert ln2Scales to np.array ln2Scales = np.array(ln2Scales) # Compute maxima over orientations for each dyadic cube oriMaxValid = [] for iLev in range(maxScale + 1): if decompmode == 'dtcwt': validAtThisScale = np.absolute( oriMax[iLev][~np.isnan(oriMax[iLev])]) elif decompmode == 'dwt': # This should be replaced with a neighbourhood maximum validAtThisScale = np.absolute( oriMax[iLev][oriMax[iLev] > 1.0e-10]) else: pass validAtThisScale /= np.mean(validAtThisScale) oriMaxValid.append(validAtThisScale) # Compute D(h) using thermodynamical approach (Partition Function, Free Energy, Entropy) U = np.zeros((maxScale + 1, nq)) V = np.zeros((maxScale + 1, nq)) tmpu1 = np.zeros((maxScale + 1, nq)) tmpu2 = np.zeros((maxScale + 1, nq)) tmpv = np.zeros((maxScale + 1, nq)) nj = np.zeros((maxScale + 1)) S = np.zeros((maxScale + 1, nq)) SNN = np.zeros((maxScale + 1, nq)) sz = np.zeros((maxScale + 1)) for iLev in range(maxScale + 1): epsArr = oriMaxValid[iLev] sz[iLev] = epsArr.size for iq, q in enumerate(qvals[:nq]): # Compute qth order structure functions qthMomArr = np.power(epsArr, q) S[iLev, iq] = np.sum(qthMomArr) # Compute intermediate quantities for h,D(h) s = S[iLev, iq] tmpv[iLev, iq] = np.sum(qthMomArr * np.log2(epsArr)) tmpu1[iLev, iq] = np.sum(qthMomArr * np.log2(qthMomArr)) tmpu2[iLev, iq] = np.sum(qthMomArr) tmpu = tmpu1[iLev, iq] - np.log2(s) * tmpu2[iLev, iq] U[iLev, iq] = tmpu / (s) + np.log2(sz[iLev]) V[iLev, iq] = tmpv[iLev, iq] / (s) if mode == 'once': # Display h(q,a) vs log2(a) function at selected scales order = 1 hfit = np.empty((nq, order + 1)) dfit = np.empty((nq, order + 1)) for iq in range(nq): hfit[iq, :] = np.polyfit(ln2Scales[minScale:maxScale + 1], V[minScale:maxScale + 1, iq], order) dfit[iq, :] = np.polyfit(ln2Scales[minScale:maxScale + 1], U[minScale:maxScale + 1, iq], order) return hfit, dfit elif mode == 'batch': return sz, S, tmpv, tmpu1, tmpu2, ln2Scales