Reputation:
While doing face detection work with CIFaceFeature
, I ran into an issue with the bounds. While trying to put a box around the recognized face, the frame would always be misplaced. Other questions on Stack Overflow point out that the Core Image and UIKit coordinate systems are inverted.
(These images are from https://nacho4d-nacho4d.blogspot.com/2012/03/coreimage-and-uikit-coordinates.html)
Obviously, this coordinate system difference is the reason for the frame misplacement. Now, the x-axis, width, and height remain the same. The only difference is the y. Other answers on Stack Overflow suggest (image height - face y) / 2
as the solution. This generally works, but for some faces, you'll find that it's wildly wrong.
Upvotes: 2
Views: 1217
Reputation:
First, remove that divide by 2. Set the origin.y
by doing image.size.height - face.bounds.origin.y
. You'll notice that the top of the frame just touches the chin. All we need to do is account for the height of the face:
image.size.height - face.bounds.height - face.bounds.origin.y
This gives you perfectly aligned frames every time.
The question remains – why are those / 2 answers wrong? This coordinate question was popular on Stack Overflow around 2012-2013. People would answer with this code: image.size.height - face.bounds.origin.y
. As we discussed earlier, the top of this frame touches the chin. Somewhere along the line, some suggested dividing the whole value by 2. That seemed to center the frame pretty well on the face. That ended up becoming the accepted answer on quite a few questions.
In my testing, I found that this sometimes worked. Other times, however, the frame would be wildly off. After doing a little thinking, I came upon the right answer. Hopefully, this sets things straight and helps anyone else who has the same issue.
Upvotes: 3