Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cropping UIImage like camscanner

I am stuck in my application feature. I want cropping feature similar to Cam Scanner Cropping. The screens of CAM-SCANNER are: I have created similar crop view.

enter image description here

enter image description here

I have obtained CGPoint of four corners. But How can I obtained cropped image in slant.

Please provide me some suggestions if possible.

like image 226
iPhone Developer Avatar asked May 07 '12 05:05

iPhone Developer


2 Answers

This is a perspective transform problem. In this case they are plotting a 3D projection in a 2D plane.

As, the first image has selection corners in quadrilateral shape and when you transform it in a rectangular shape, then you will either need to add more pixel information(interpolation) or remove some pixels.

So now actual problem is to add additional pixel information to cropped image and project it to generate second image. It can be implemented in various ways:

<> you can implement it by your own by applying perspective tranformation matrix with interpolation.

<> you can use OpenGL .

<> you can use OpenCV. .. and there are many more ways to implement it.

I had solved this problem using OpenCV. Following functions in OpenCV will help you to achieve this.

  1. cvPerspectiveTransform

  2. cvWarpPerspective

First function will calculate transformation matrix using source and destination projection coordinates. In your case src array will have values from CGPoint for all the corners. And dest will have rectangular projection points for example {(0,0)(200,0)(200,150)(0,150)}.

Once you get transformation matrix you will need to pass it to second function. you can visit this thread.

There may be few other alternatives to OpenCV library, but it has good collection of image processing algorithms.

iOS application with opencv library is available at eosgarden.

like image 146
Ravin Avatar answered Nov 15 '22 22:11

Ravin


I see 2 possibilities. The first is to calculate a transformation matrix that slants the image, and installing it in the CATransform3D property of your view's layer.

That would be simple, assuming you knew how to form the transformation matrix that did the stretching. I've never learned how to construct transformation matrixes that stretch or skew images, so I can't be of any help. I'd suggest googling transformation matrixes and stretching/skewing.

The other way would be to turn the part of the image you are cropping into an OpenGL texture and map the texture onto your output. The actual texture drawing part of that would be easy, but there are about 1000 kilos of OpenGL setup to do, and a whole lot to learning in order to get anything done at all. If you want to pursue that route, I'd suggest searching for simple 2D texture examples using the new iOS 5 GLKit.

like image 44
Duncan C Avatar answered Nov 15 '22 23:11

Duncan C