1

I have tried, but not been able to figure it out how to resize an image into a correct shape. I have a cropped the image and I want to convert it into a proportion like rectangle or square but haven't found a solution. Can anyone help me with this?

enter image description here

into kinda this form

enter image description here

sduplooy
  • 14,340
  • 9
  • 41
  • 60
Retro
  • 3,985
  • 2
  • 17
  • 41

4 Answers4

2

There's no api for this with apple builtin docs. I've found api for this type of perspective transform with opencv.framework. Opencv contain many api for the perspective transform. I've tested below c++ api(find in imgproc.hpp) and working fine for me. check with this.

void warpPerspective( InputArray src, OutputArray dst,
                                   InputArray M, Size dsize,
                                   int flags=INTER_LINEAR,
                                   int borderMode=BORDER_CONSTANT,
                                   const Scalar& borderValue=Scalar());

Here they also provide api for converting UIImage object into InputArray(src). This might be long process for you to get idea about this.

Note: You should spent some time to learn about opencv.

Mani
  • 17,549
  • 13
  • 79
  • 100
  • your answer is helpful but i am stuck with transformation matrix. do you have more idea about the transformation matrix calculation? – Retro Aug 27 '14 at 10:18
  • @Retro http://stackoverflow.com/questions/13269432/perspective-transform-crop-in-ios-with-opencv - accepted answer should help you. – Timur Kuchkarov Aug 28 '14 at 09:43
  • @TimurKuchkarov Excellent finding and more helpful for future visitors. I think, you haven't seen this line `cv::warpPerspective(original, undistorted, cv::getPerspectiveTransform(src, dst), cvSize(maxWidth, maxHeight));` which is same as above answer. – Mani Aug 28 '14 at 09:54
  • 1
    @Mani True, warpPerspective is there, but that answer has some samples which may help. – Timur Kuchkarov Aug 28 '14 at 10:17
0

I doubt it will be possible for you to do as you would have to find the dimensions and full xyz of the origional imagge then make the adjustments to the new image. This will deffinatly distort the image, more than likely making the image non usable.

We tried a similar approach to scanning documents with barcodes and software reading them, due to pixel distortion we had to drop the project.

Christopher Shaw
  • 763
  • 6
  • 19
0

I absolutely stand behind what Mani says. OpenCV is a great place to be. It will probably help you detecting edges as well.

If you don't want to dig too far into c++ land – AGGeometryKit is an option (use the develop branch). Disclaimer: I'm the maintainer of the library.

AGGeometryKit

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
hfossli
  • 22,616
  • 10
  • 116
  • 130
0

You could do this with CGAffineTransforms. You would chain a series of CGAffineTransforms to get to the eventual shape but the trick to the process is that you rotate the image so that the axis of distortion is exactly in line with the x or y axis then use a scale with the other axis fixed to 1. This code comes from an app where you use two fingers to distort and image with a pinch gesture:

        CGAffineTransform rotate = CGAffineTransformMakeRotation(-self.pinchAngle);
        [self.bPath applyTransform:rotate];

        CGAffineTransform scale = CGAffineTransformMakeScale(prop , 1);
        [self.bPath applyTransform:scale];


        CGAffineTransform rotate2 = CGAffineTransformMakeRotation(self.pinchAngle);
        [self.bPath applyTransform:rotate2];

Where self.pinchAngle here is the angle to the x axis and prop is the proportional change, 1 being no change. You could either use Core Graphics to work on the actual UIImage or you could apply this to a UIImageView. If you're feeling really brave you could do the matrix maths and end up with a single transformation matrix.

Martin
  • 1,135
  • 1
  • 8
  • 19