Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS objective C converting coordinates from Absolute image location to my views coordinate system

I am using CIDetector to find faces in a picture.

The coordinates of faces it returns are the absolute coordinates in the image file (The image dimensions are much larger than the screen size obviously).

I tried to use the converRect:toView command. The image itself is not a UIView so the command doesn't work, also I have a few views embedded inside each other where finally the image is being shown.

I want to convert the bounds of the found faces in the image to the exact location of the face being shown on the screen in the embedded image.

How can this be accomplished?

Thanks! The image being shown on the phone - the image is scaled to fit the screen with aspect fit

like image 567
Avner Barr Avatar asked Feb 20 '23 13:02

Avner Barr


1 Answers

The coordinates from CIDetecter (CoreImage) are flipped relative to UIKit coordinates. There are a bunch of tutorials out there on iOS Face Detection but most of them are either incomplete or mess up the coordinates. Here's one that is correct: http://nacho4d-nacho4d.blogspot.com/2012/03/coreimage-and-uikit-coordinates.html

One thing to note: the tutorial uses a small image so the resulting coordinates do not have to be scaled to the on-screen (UIImageView) representation of the image. Assuming you use a photo taken with the iPad camera, you will have to scale the coordinates by the amount the source image is scaled (unless you reduce its size before running the face detection routine -maybe not a bad idea). You may also need to rotate the image for the correct orientation.

There is a routine in one of the answers here for rotating/scaling: UIImagePickerController camera preview is portrait in landscape app

And this answer has a good routine for finding the scale of an image when presented by a UIImageView using 'aspect fit': How to get the size of a scaled UIImage in UIImageView?

You will need to use the scale in order to map the CIDetector coordinates from the full size image to the scaled down image shown in a UIImageView.

like image 134
spring Avatar answered Feb 22 '23 01:02

spring