Exemple #1
0
def calculate_point(kps_left, sco_left, des_left, kps_right, sco_right,
                    des_right):
    FLANN_INDEX_KDTREE = 1
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)
    flann = cv2.FlannBasedMatcher(index_params, search_params)
    matches = flann.knnMatch(des_left, des_right, k=2)

    # matcher = cv2.DescriptorMatcher_create(cv2.DescriptorMatcher_FLANNBASED)
    # matches = matcher.knnMatch(des_left, des_right, 2)

    goodMatch = []
    locations_1_to_use = []
    locations_2_to_use = []
    dis = []
    # 匹配对筛选
    min_dist = 1000
    max_dist = 0
    disdif_avg = 0
    # 统计平均距离差
    for m, n in matches:
        disdif_avg += n.distance - m.distance
    disdif_avg = disdif_avg / len(matches)
    # print('disdif_avg:', disdif_avg)
    for m, n in matches:
        #自适应阈值
        if n.distance > m.distance + 1.5 * disdif_avg:
            # if m.distance < 0.9 * n.distance:
            goodMatch.append(m)
            p2 = cv2.KeyPoint(kps_right[m.trainIdx][0],
                              kps_right[m.trainIdx][1], 1)
            p1 = cv2.KeyPoint(kps_left[m.queryIdx][0], kps_left[m.queryIdx][1],
                              1)
            locations_1_to_use.append([p1.pt[0], p1.pt[1]])
            locations_2_to_use.append([p2.pt[0], p2.pt[1]])
            dis.append([n.distance, m.distance])
    #goodMatch = sorted(goodMatch, key=lambda x: x.distance)
    print('match num is %d' % len(goodMatch))
    locations_1_to_use = np.array(locations_1_to_use)
    locations_2_to_use = np.array(locations_2_to_use)
    dis = np.array(dis)
    # Perform geometric verification using RANSAC.
    _, inliers = measure.ransac((locations_1_to_use, locations_2_to_use),
                                transform.AffineTransform,
                                min_samples=3,
                                residual_threshold=_RESIDUAL_THRESHOLD,
                                max_trials=1000)

    # print('Found %d inliers' % sum(inliers))

    # 筛选距离最近的前60%个数据
    inlier_idxs = np.nonzero(inliers)[0]
    dis_R = dis[inliers]
    dis_idx = np.argsort(dis_R[:, 0] - dis_R[:, 1])
    dis_sorted = dis_R[dis_idx]
    l = dis_idx.shape[0]
    end = int(l * 0.6)
    #最终匹配结果
    inlier_idxs = inlier_idxs[dis_idx[:end]]

    print('sorted inliers:', end)

    #最终匹配结果
    matches = np.column_stack((inlier_idxs, inlier_idxs))
    # print('whole time is %6.3f' % (time.perf_counter() - start0))

    # Visualize correspondences, and save to file.
    #1 绘制匹配连线
    plt.rcParams['savefig.dpi'] = 100  #图片像素
    plt.rcParams['figure.dpi'] = 100  #分辨率
    plt.rcParams['figure.figsize'] = (4.0, 3.0)  # 设置figure_size尺寸
    _, ax = plt.subplots()
    plotmatch.plot_matches(ax,
                           image1,
                           image2,
                           locations_1_to_use,
                           locations_2_to_use,
                           np.column_stack((inlier_idxs, inlier_idxs)),
                           plot_matche_points=False,
                           matchline=True,
                           matchlinewidth=0.5)
    ax.axis('off')
    ax.set_title('')
    plt.show()
    print('inlier_idxs:', len(inlier_idxs))
    res = locations_2_to_use[inlier_idxs] - locations_1_to_use[inlier_idxs]
    return locations_1_to_use[inlier_idxs], locations_2_to_use[inlier_idxs]
    print(np.min(res[:, 0]))
    print(np.max(res[:, 0]))
    print(np.min(res[:, 1]))
    print(np.max(res[:, 1]))
Exemple #2
0
_, inliers = measure.ransac((locations_1_to_use, locations_2_to_use),
                            transform.AffineTransform,
                            min_samples=3,
                            residual_threshold=30,
                            max_trials=1000)
inlier_idxs = np.nonzero(inliers)[0]
print('Found %d inliers' % sum(inliers))

# Visualize correspondences, and save to file.
#1 绘制匹配连线, 1 일치하는 연결 그리기
plt.rcParams['savefig.dpi'] = 100  #图片像素 , 이미지 픽셀
plt.rcParams['figure.dpi'] = 100  #分辨率 , 해상도
plt.rcParams['figure.figsize'] = (4.0, 3.0
                                  )  # 设置figure_size尺寸 , figure_size 크기 설정
_, ax = plt.subplots()
plotmatch.plot_matches(
    ax,
    img1,
    img2,
    locations_1_to_use,
    locations_2_to_use,
    np.column_stack((inlier_idxs, inlier_idxs)),
    # 매칭 안된 포인트 안 그리기
    #plot_matche_points = False,
    # 매칭 안된 포인트 그리기
    plot_matche_points=True,
    matchline=True,
    matchlinewidth=0.3)
ax.axis('off')
ax.set_title('')
plt.show()
Exemple #3
0
_, inliers = measure.ransac((locations_1_to_use, locations_2_to_use),
                            transform.AffineTransform,
                            min_samples=3,
                            residual_threshold=_RESIDUAL_THRESHOLD,
                            max_trials=1000)

print('Found %d inliers' % sum(inliers))

inlier_idxs = np.nonzero(inliers)[0]
#最终匹配结果
matches = np.column_stack((inlier_idxs, inlier_idxs))
print('whole time is %6.3f' % (time.perf_counter() - start0))

# Visualize correspondences, and save to file.
#1 绘制匹配连线
plt.rcParams['savefig.dpi'] = 100  #图片像素
plt.rcParams['figure.dpi'] = 100  #分辨率
plt.rcParams['figure.figsize'] = (4.0, 3.0)  # 设置figure_size尺寸
_, ax = plt.subplots()
plotmatch.plot_matches(ax,
                       image1,
                       image2,
                       locations_1_to_use,
                       locations_2_to_use,
                       np.column_stack((inlier_idxs, inlier_idxs)),
                       plot_matche_points=False,
                       matchline=True,
                       matchlinewidth=0.5)
ax.axis('off')
ax.set_title('')
plt.show()
Exemple #4
0
locations_2_to_use = np.array(locations_2_to_use)
# Perform geometric verification using RANSAC.
_RESIDUAL_THRESHOLD = 30

_, inliers = measure.ransac((locations_1_to_use, locations_2_to_use),
                            transform.AffineTransform,
                            min_samples=3,
                            residual_threshold=_RESIDUAL_THRESHOLD,
                            max_trials=1000)
inlier_idxs = np.nonzero(inliers)[0]
# p1 = locations_2_to_use[inlier_idxs]
# p2 = locations_1_to_use[inlier_idxs]

# Visualize correspondences, and save to file.
#1 绘制匹配连线
plt.rcParams['savefig.dpi'] = 100  #图片像素
plt.rcParams['figure.dpi'] = 100  #分辨率
plt.rcParams['figure.figsize'] = (4.0, 3.0)  # 设置figure_size尺寸
_, ax = plt.subplots()
plotmatch.plot_matches(ax,
                       img_sar_canny,
                       img_optical_canny,
                       locations_1_to_use,
                       locations_2_to_use,
                       np.column_stack((inlier_idxs, inlier_idxs)),
                       plot_matche_points=False,
                       matchline=True,
                       matchlinewidth=0.5)
ax.axis('off')
ax.set_title('')
plt.show()