2
votes

My app uses the camera, and it processes every frame, looking for some elements inside the image (such as faces).

When I find something on the image, I want to draw on the screen showing the video using drawRect:(CGRect)rect of custom view.

I'm not drawing only rectangles, and I'm not using GPU and GLKView. When I run video frames with 1920*1080 I can draw the exact location by dividing screen width and height with video frame size.

However when I change to 480*360 video resolution, the elements are not drawn at the correct location, I believe due to aspect ratio differences. Any idea what conversion I need to do here, perhaps AffineTransfrom?

1

1 Answers

3
votes

Yes. it is due to the aspect ratio. In one of my project I had faced a similar problem and I did the following.

  1. Find the scale factor

    float xScale = destionation.size.width / imageSize.width;//destination is the max image drawing area.

    float yScale = destionation.size.height / imageSize.height;

    float scaleFactor = xScale < yScale ? xScale : yScale;

  2. Find the drawing rectangle by

    float destinationHeight = destionation.size.height * scaleFactor;

    float destinationWidth = destionation.size.width * scaleFactor;

    this will give you a rectangle which can hold the image in aspect ratio. And draw your image using this rectangle. Also you can align this rectangle to view's center by multiplying the center point with scale factor.

  3. Now convert your face rectangle from Image size to drawing rectangles by multiplying with scale factor.

  4. Use the converted points to draw the rectangle on destination rectangle.