darktable-org / darktable

darktable is an open source photography workflow application and raw developer
https://www.darktable.org
GNU General Public License v3.0
9.54k stars 1.13k forks source link

Automatic rotation or perspective correction using camera metadata #17384

Open rl-king opened 3 weeks ago

rl-king commented 3 weeks ago

Some Leica cameras have a feature called "Perspective control", which automatically corrects the perspective or keystone distortion. Article with pictures here. This is directly applied to in-camera jpg files, and for raw files the data is stored in the camera metadata. Which looks like:

Xmp.xmpDSA.Version                           XmpText     5  1.0.0
Xmp.xmpDSA.CorrectionAlreadyApplied          XmpText     4  True
Xmp.xmpDSA.PitchAngle                        XmpText    12  0.9129999876
Xmp.xmpDSA.RollAngle                         XmpText    12  1.2159999608
Xmp.xmpDSA.FocalLength35mm                   XmpText    13  50.0000000000
Xmp.xmpDSA.TargetAspectRatio                 XmpText    12  1.5000000000
Xmp.xmpDSA.ScalingFactorHeight               XmpText    12  0.9687359333
Xmp.xmpDSA.ValidCropCorners                  XmpText     4  True
Xmp.xmpDSA.ApplyAutomatically                XmpText     4  True
Xmp.xmpDSA.NormalizedCropCorners             XmpSeq      8  0.0106867747, 0.0314993262, 0.9756190180, 0.0007762801, 0.9929105639, 0.9690485000, 0.0208023600, 1.0000000000

I could not find any documentation or information if this data is used by darktable, but it would be nice to be able to. It could even just be used for rotation.

Let me know if I can provide any other information or image samples.

jenshannoschwalm commented 3 weeks ago

Unfortunately those exiv tags are not documented for "how to apply" so no chance without Leica help.

rl-king commented 2 weeks ago

I can't find anything online either, just some basic value type information. What docs or specification do we need from Leica to be able to use these values?

jenshannoschwalm commented 2 weeks ago

The maths. :-)

pehar1 commented 2 weeks ago

All the nice numbers that are stored in the Xmp.xmpDSA tags are parameters that are used in a proprietary algorithm from Leica to correct the image data. I don't know of any manufacturer that has ever made its algorithms public. The only way would be backwards engineering, involving a lot of work and time.

Donatzsky commented 1 week ago

The numbers seem fairly "sensible". Enough so that I suspect it shouldn't be too hard. Mostly a question of figuring out the coordinate system. Try to create a thread over on discuss.pixls.us with several samples (raw + corrected jpeg + exif). Many of the regulars there are good with math.

Donatzsky commented 4 days ago
Xmp.xmpDSA.PitchAngle                        XmpText    12  0.9129999876
Xmp.xmpDSA.RollAngle                         XmpText    12  1.2159999608

I guess that if the camera is perfectly level, then both pitch and roll would 1.

Xmp.xmpDSA.TargetAspectRatio                 XmpText    12  1.5000000000

Can you set the camera to record in different aspect ratios? And does this change accordingly?

Xmp.xmpDSA.ScalingFactorHeight               XmpText    12  0.9687359333

Don't actually know anything about keystone correction algorithms, but presumably this is to correct for distortion caused by straightening the trapezoid or to fit the aspect ratio.

Xmp.xmpDSA.ValidCropCorners                  XmpText     4  True

Is it possible to get False here?

Xmp.xmpDSA.NormalizedCropCorners             XmpSeq      8  0.0106867747, 0.0314993262, 0.9756190180, 0.0007762801, 0.9929105639, 0.9690485000, 0.0208023600, 1.0000000000

This appears to be the corners of the trapezoid, with the coordinates going from 0 to 1. Wouldn't be surprised if each number is just a multiplication factor applied to the pixel dimensions.

At a minimum, the following sample shots are needed:

All shots should be done in both horizontal and vertical orientation, so ten total. Raw + camera JPEG.