Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The RANSAC example uses the ransac function in an inappropriate way #5396

Closed
KojiOchiai opened this issue May 14, 2021 · 4 comments
Closed

Comments

@KojiOchiai
Copy link

KojiOchiai commented May 14, 2021

Description

In the example of the RANSAC function, there is a part where the coordinates calculated by corner_peaks and corner_subpix are used as arguments of AffineTransform.estimate and ransac.

# estimate affine transform model using all coordinates
model = AffineTransform()
model.estimate(src, dst)

# robustly estimate affine transform model with RANSAC
model_robust, inliers = ransac((src, dst), AffineTransform, min_samples=3,
                               residual_threshold=2, max_trials=100)
outliers = inliers == False

The output of corner_peaks is in the form (row, col), but the arguments of estimate and ransac are in the form (col, row).

Since the necessary coordinate conversion has not been done, the coordinates are exchanged and minus is added to the rotation direction in the print section.

# compare "true" and estimated transform parameters
print("Ground truth:")
print(f"Scale: ({tform.scale[1]:.4f}, {tform.scale[0]:.4f}), "
      f"Translation: ({tform.translation[1]:.4f}, "
      f"{tform.translation[0]:.4f}), "
      f"Rotation: {-tform.rotation:.4f}")
print("Affine transform:")
print(f"Scale: ({model.scale[0]:.4f}, {model.scale[1]:.4f}), "
      f"Translation: ({model.translation[0]:.4f}, "
      f"{model.translation[1]:.4f}), "
      f"Rotation: {model.rotation:.4f}")
print("RANSAC:")
print(f"Scale: ({model_robust.scale[0]:.4f}, {model_robust.scale[1]:.4f}), "
      f"Translation: ({model_robust.translation[0]:.4f}, "
      f"{model_robust.translation[1]:.4f}), "
      f"Rotation: {model_robust.rotation:.4f}")

I think it would be less confusing for the reader if we exchange row and col just before passing to estimate and ransac, and just print the parameters directly.
Like this.

# exchange coordinates
def flipxy(points):
    return np.stack([points[:, 1], points[:, 0]]).T

# estimate affine transform model using all coordinates
model = AffineTransform()
model.estimate(flipxy(src), flipxy(dst))

# robustly estimate affine transform model with RANSAC
model_robust, inliers = ransac((flipxy(src), flipxy(dst)),
                               AffineTransform, min_samples=3,
                               residual_threshold=2, max_trials=100)
def print_tform(tform):
    print(f"Scale: ({tform.scale[0]:.4f}, {tform.scale[1]:.4f}), "
          f"Translation: ({tform.translation[0]:.4f}, "
          f"{model.translation[1]:.4f}), "
          f"Rotation: {tform.rotation:.4f}")

# compare "true" and estimated transform parameters
print("Ground truth:")
print_tform(tform)
print("Affine transform:")
print_tform(model)
print("RANSAC:")
print_tform(model_robust)

By the way, the plot_matches used at the end of the example assumes the format (row, col), so I think it is better to leave the src and dst in the global space in the current format (row, col).

@KojiOchiai
Copy link
Author

KojiOchiai commented May 15, 2021

As I checked, funcions in skimage.transform assume (col, row), and skimage.feature assume (row, col) in IO.

@KojiOchiai
Copy link
Author

This was an issue that was already mentioned in the following issues, so I will close.
#1749, #1752, #1789

@jni
Copy link
Member

jni commented May 17, 2021

Thank you @KojiOchiai for investigating and apologies that we haven't tackled that inconsistency yet. Hopefully we can do so soon!

@KojiOchiai
Copy link
Author

@jni Thank you for your kind attention and daily maintenance, and I wish scikit-image all the best in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants