def __findkp__(self, givenpoint): boxsize = 15 refmatcher = ORB_matcher(nfeatures=20, scaleFactor=1.1, nlevels=5) # 重要参数:推荐nfeature为tolerence的两倍 defmatcher = ORB_matcher(nfeatures=20, scaleFactor=1.1, nlevels=5) while boxsize < 100: refbox = [(givenpoint[0] - boxsize, givenpoint[1] - boxsize), (givenpoint[0] + boxsize + 1, givenpoint[1] + boxsize + 1)] if any(refbox) < 0: break refblock = self.refimg[refbox[0][1]:refbox[1][1], refbox[0][0]:refbox[1][0]] kp1, des1 = refmatcher.detect(refblock, show=0) if des1 is not None: defbox = [(givenpoint[0] - boxsize - 50, givenpoint[1] - boxsize - 50), (givenpoint[0] + boxsize + 51, givenpoint[1] + boxsize + 51)] defblock = self.defimg[defbox[0][1]:defbox[1][1], defbox[0][0]:defbox[1][0]] if any(defbox) < 0: print("out of range") break kp2, des2 = defmatcher.detect(defblock, show=0) defmatcher.match(des1, des2) if len( defmatcher.matches ) > 10: # 重要参数:增大会增加匹配精度,但会减少匹配数量;根据特征点稀疏状况调整,特征点稀疏位置不方便使用太大 return kp1, kp2, des1, defmatcher.matches, boxsize boxsize += 5 print("Key Point Not Found!") return None, None, None, None, None
def __refreshdisp__(self, show): matcher = ORB_matcher(nfeatures=100, scaleFactor=1.2, nlevels=10) tempshowimg = self.showimg.copy() for i in range(len(self.refpoints)): defbox = [(int(self.refpoints[i][0][0]) - 200, int(self.refpoints[i][0][1]) - 200), (int(self.refpoints[i][0][0]) + 200, int(self.refpoints[i][0][1]) + 200)] defblock = self.defimg[defbox[0][1]:defbox[1][1], defbox[0][0]:defbox[1][0]] if any(defbox) < 0: print("out of range") break kp2, des2 = matcher.detect(defblock, show=0) des1 = self.refpoints[i][1].astype("uint8") matcher.match(des1, des2) if matcher.matches is not None: distance = matcher.matches[0].distance for j in range(len(matcher.matches)): id2 = matcher.matches[0].trainIdx if matcher.matches[j].distance < distance: distance = matcher.matches[j].distance id2 = matcher.matches[j].trainIdx x_ref = self.refpoints[i][0][0] y_ref = self.refpoints[i][0][1] x_def = kp2[id2].pt[0] - 200 + int(self.refpoints[i][0][0]) y_def = kp2[id2].pt[1] - 200 + int(self.refpoints[i][0][1]) u = x_def - x_ref v = y_def - y_ref self.refpoints[i][2] = [u, v] cv2.circle(tempshowimg, (int(x_ref / 3), int(y_ref / 3)), 1, (255, 255, 255), 2) cv2.circle(tempshowimg, (int(x_def / 3), int(y_def / 3)), 1, (255, 255, 255), 2) if show: position1 = (int(self.refpoints[i][0][0] / 3) + 5, int(self.refpoints[i][0][1] / 3) - 10) position2 = (int(self.refpoints[i][0][0] / 3) + 5, int(self.refpoints[i][0][1] / 3) + 10) text1 = "U=" + str(np.round(u, 3)) text2 = "V=" + str(np.round(v, 3)) cv2.putText(tempshowimg, text1, position1, fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=0.5, color=(255, 255, 255), thickness=1) cv2.putText(tempshowimg, text2, position2, fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=0.5, color=(255, 255, 255), thickness=1) self.tempshowimg = tempshowimg