def computescoreandthre(self): # filter = np.ones(self.leftgridsize) # self.leftgridkpoints = Func.conv2withstride(self.leftimg, filter, stride=self.leftgridsize, start=None, # gridnum=self.lgn) # Func.imagesc(self.leftgridkpoints) # print((self.leftgridkpoints>1).sum()) #*************测试卷积是否正确********************* self.check = [] # 计算阈值 self.thre1 = Func.conv2withstride(self.leftimg, self.kernel[:, :, 0, 0], stride=self.stride[1:3], start=self.start, gridnum=self.lgn) # De = self.neibor ** 2 # self.thre1 = self.TreshFactor * np.sqrt(self.thre1 / De) # 阈值计算公式 self.check.append(self.thre1.reshape(self.lgn, self.lgn, 1)) self.convset = [] self.cposition = np.asarray([[], [], [], []], np.int32) self.leftmap = [] # 计算阈值 h, w, c = self.img1.shape self.convset.append(self.leftimg.reshape(h, w, 1)) # 计算打分 self.score = np.zeros((self.lgn, self.lgn)) self.lgshape = (self.lgn, self.lgn) nnd = 1 for i in range(self.lgn): # r for j in range(self.lgn): # c leftvalue = Func.index2value((i, j), self.lgshape) + 1 bestmatchgrid = self.leftmatchgrid[self.leftimglabel == leftvalue] if bestmatchgrid.size < 100: continue # 点数小于阈值则不计算,默认为不匹配 number, n_counts = np.unique(bestmatchgrid, return_counts=True) rbestindex = number[np.argsort(n_counts)[-1]] index = (self.leftimglabel == leftvalue) & (self.leftmatchgrid == rbestindex) neiborsindex = (((self.leftimglabel - self.leftmatchgrid) == (leftvalue - rbestindex)) & self.leftimg.astype(np.bool)).astype( np.float32) self.convset.append(neiborsindex.reshape(h, w, 1)) self.leftmap.append(index) self.cposition = np.concatenate( (self.cposition, [[nnd], [i], [j], [0]]), 1) nnd += 1 neiborsindexconv = Func.conv2withstride( neiborsindex, self.kernel[:, :, 0, 0], stride=self.stride[1:3], start=self.start, gridnum=self.lgn) self.check.append( neiborsindexconv.reshape(self.lgn, self.lgn, 1)) # self.score[i, j] = neiborsindexconv[i, j] # if self.DEBUG: # print("calc grid(%d,%d)\n thre=%f,index.sum=%d,neiborsindex.sum=%d,score=%f" # % (i, j,self.thre[i,j],index.sum(),neiborsindex.sum(),self.score[i, j])) # if neiborsindexconv[i, j] < self.thre[i, j]: # continue # self.TrueMatches += index #卷积计算 self.convset = np.asarray(self.convset, np.float32) if self.shift[0] == 1: self.convset = np.roll(self.convset, int(self.leftgridsize[0] / 2), axis=1) if self.shift[1] == 1: self.convset = np.roll(self.convset, int(self.leftgridsize[1] / 2), axis=2) t = time.time() res = Func.batchconv4d(self.convset, np.asarray(self.kernel, np.float32), stride=self.stride) print("batchsize is %d,batch conv cost %fs" % (nnd, time.time() - t)) tmp = np.asarray(self.check, np.float32) - res # Func.imagesc(tmp.reshape(self.lgn,self.lgn),'conv sta whin 4D conv and 2Dconv') print("conv sta whin 4D conv and 2Dconv is:sum %f,mean %f,std %f" % (tmp.sum(), tmp.mean(), tmp.std())) self.thre = res[0, :, :, 0] De = self.neibor**2 self.thre = self.TreshFactor * np.sqrt(self.thre / De) # 阈值计算公式 self.cposition = list(self.cposition) tmp = res[self.cposition] sindex = self.cposition[1:3] for i in range(len(tmp)): if self.thre[sindex[0][i], sindex[1][i]] > tmp[i]: continue self.TrueMatches += self.leftmap[i]