agens-no / AGGeometryKit

Quadrilaterals on CALayer, CGGeometry-functions, UIView/CALayer properties and other invaluable tools.
Other
1.27k stars 130 forks source link

imageWithPerspectiveCorrectionFromQuad corrupts source image #34

Closed Maxtws closed 8 years ago

Maxtws commented 8 years ago

Hi! I've been playing with 'imageWithPerspectiveCorrectionFromQuad' function to perform perspective correction for images and faced lots of cases when source image was corrupted. I spent some time debugging and found out that "aspectRatioForQuad" returned 'NaN' sometimes. Further digging led me to 'NaN' in sqrt operation caused by negative 'f2' param in '' f2 = step1 * (step2 + step3) ", where 'step1' and 'step2' were positive and 'step3' was negative. Here's the quad: AGKQuad { tl: {212.51999437014263, 233.44500000000016}, tr: {877.67997674942012, 217.19250000000011}, br: {1023.9599728743235, 1668.0975000000001}, bl: {91.079997587203991, 1693.2150000000001} } Source image size is: (width = 1242, height = 1773))

Unfortunately, this is a gamestopper for me. I hope this info is usefull.

hfossli commented 8 years ago

Have you seen this pull request? https://github.com/gavrix/AGGeometryKit/commit/4bd09ed645f4e5a3869964345dca351d58f91786

Also, we know that the resampling could be improved (https://github.com/hfossli/AGGeometryKit/issues/23). Currently we are using nearest neighbor which results in a bit more pixelated images than necessary.

Maxtws commented 8 years ago

Nope. But I've made almost the same changes in the code. It's good for development purposes, however due to company policy I just can not use pods with my modifications. Any chance to see an updated version with that fix any time soon? As for resampling it is not an issue for me but it's good to know you're working on it! P.S.: btw, AGGeometryKit is awesome!

hfossli commented 8 years ago

^ ^, That's a good company policy. I support that! You don't want changes like that discarded on each pod install.

That pull request is live on master and published as version 1.2.3. A pod update should normally be enough.

We're sadly not working on improving the resampling. I don't have time for that, but I hope others would like to contribute on that part. It will be very interesting, fun and educational.

Maxtws commented 8 years ago

Ahhh, man. I just needed to update the pod. I was using 1.2.2. Sorry for wasting your time! Thanks!

I've just noticed an issue with UIImage scale and "imageWithPerspectiveCorrectionFromQuad". If I pass an image with scale factor 3.0f (iPhone6 Plus) it crops only portion of an image. As far as I traced the rects it appears to act weird here:

(Line 164:) UIImage correctedImage = [self imageWithTransform:transform anchorPoint:CGPointZero]; (Line 165:) UIImage resultImage = [correctedImage imageByCroppingToRect:destinationRect];

Here, imageWithTransform returns an image with scale 1.0f (so its size in points = size in pixels) and destinationRect is still in points. So the result image is 1/3 upper part of the source image.

Is this something you're aware of and I'm wasting your time again?

hfossli commented 8 years ago

No problem! :) 1.2.3 was released very recently – good timing.

I'm not aware of this. A pull request on the scale issues would be highly appreciated.

hfossli commented 8 years ago

And also perfect if you can open a separate issue for that.