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

Chromatic aberrations fix example ? #17

Closed
pypoulp opened this issue Jul 14, 2018 · 6 comments
Closed

Chromatic aberrations fix example ? #17

pypoulp opened this issue Jul 14, 2018 · 6 comments
Labels

Comments

@pypoulp
Copy link

pypoulp commented Jul 14, 2018

Hello,

Those python bindings to lensfun look pretty cool!

What is the workflow for fixing chromatic aberration / vignetting? (I need to fix chromatic aberrations, but not large distortion). It's not really clear in the API docs.

Thanks.

Paul.

@letmaik
Copy link
Owner

letmaik commented Jul 18, 2018

Vignetting and chromatic aberration are different things. Vignetting corrections (via lensfun's lfModifier::ApplyColorModification(..)) are not exposed through lensfunpy yet, feel free to open a separate issue if you need this functionality.

For correcting chromatic aberrations you would have to use undist_coords = mod.apply_subpixel_distortion() which gives you the undistortion coordinates per image channel. I'm not sure if you can use cv2.remap directly here or whether you have to apply it separately per-channel, probably the latter. If you figure it out, please post it here and I'll include it in the samples.

@pypoulp
Copy link
Author

pypoulp commented Jul 19, 2018

Thanks for reply,
For some reason, the undist_coords = mod.apply_subpixel_distortion() method gives me a numpy array full of 0. tried multiple lens / cameras.
code used :

mod = lensfunpy.Modifier(lens, 1.0, width, height)
mod.initialize(focal_length, aperture, distance,  flags=lensfunpy.ModifyFlags.ALL)
undist_coords = mod.apply_subpixel_distortion()
print(np.max(undist_coords))  # => 0.
print(np.min(undist_coords))  # => 0.

@letmaik
Copy link
Owner

letmaik commented Jul 19, 2018

Can you check at http://lensfun.sourceforge.net/lenslist/ whether your lens has a "yes" at the TCA column? If not, then that's the reason, and I guess this could be surfaced better with a useful error message. If yes, then it's a bug I think.

@pypoulp
Copy link
Author

pypoulp commented Jul 19, 2018

You're right, TCA wasn't available for this lens. undist_coords seems correct with a valid TCA calibrated lens.

It's kind of wierd, but with subpixel coords, cv2.remap (per channel) is going pretty crazy, rotating the green channel 90°. need a bit more investigation to get it working.

# OpenCV is BGR order

r = im[..., 2]
g = im[..., 1]
b = im[..., 0]

r_undistorted = cv2.remap(r, undist_coords[..., 0], None, cv2.INTER_LANCZOS4)
g_undistorted = cv2.remap(g, undist_coords[..., 1], None, cv2.INTER_LANCZOS4)
b_undistorted = cv2.remap(b, undist_coords[..., 2], None, cv2.INTER_LANCZOS4)

im[..., 2] = r_undistorted
im[..., 1] = g_undistorted
im[..., 0] = b_undistorted


cv2.imwrite(undistorted_image_path, im)

@CapPow
Copy link

CapPow commented Jun 10, 2019

@paulparneix, I may have a (late) solution for you. It seems swapping the axes on the green channel solves the issue, although I don't know why.

r = im[..., 2]
g = im[..., 1].swapaxes(0,1)
b = im[..., 0]

@letmaik
Copy link
Owner

letmaik commented Feb 26, 2023

Fixed in #44 and will be part of the next release. With that fix, it would be (note undist_coords slicing):

# OpenCV is BGR order

r = im[..., 2]
g = im[..., 1]
b = im[..., 0]

r_undistorted = cv2.remap(r, undist_coords[..., 0, :], None, cv2.INTER_LANCZOS4)
g_undistorted = cv2.remap(g, undist_coords[..., 1, :], None, cv2.INTER_LANCZOS4)
b_undistorted = cv2.remap(b, undist_coords[..., 2, :], None, cv2.INTER_LANCZOS4)

im[..., 2] = r_undistorted
im[..., 1] = g_undistorted
im[..., 0] = b_undistorted

cv2.imwrite(undistorted_image_path, im)

@letmaik letmaik closed this as completed Feb 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants