3

I have an image which contains a trapezoid section which I need to transform into a square shape (deskew). I'm am really struggling to understand 3D transformation matrices and have no idea where to start with this.

At present I have 4 CGPoints which represent a skewed shape, I also have 4 CGPoints which represent a uniform rectangle. How would I convert the initial 4 CGPoints into a 3D transform matrix to deskew the image?

I'm basically looking for the inverse of this: iPhone image stretching (skew)

Where the first image would be the input shape and a square image would be the output.

Can anyone point me in the right direction?

Community
  • 1
  • 1
JWood
  • 2,804
  • 2
  • 39
  • 64

1 Answers1

0

Find the 2 highest CGPoints, and level them, whilst doing this level the 2 lowest CGPoints, the 2 left most CGPoints and the 2 right most CGPoints.

//assuming skewpoint1 & skewpoint2 are the highest points
if (skewpoint1.y < skewpoint2.y) 
   skewpoint1.y += speed;
   skewpoint2.y -= speed;
else
   skewpoint1.y -= speed;
   skewpoint2.y += speed;

do similar the for lowest points and the x values for the 2 left most and 2 right most CGPoints.

You could also add a snap-to function for when the between the points is less than 2xspeed to snap to the point central to both, i.e.

if (abs(skewpoint1.y - skewpoint2.y) < (speed*2))
    //depending on which CGPoint is higher
    skewpoint1.y += (abs(skewpoint1.y - skewpoint2.y)/2);
    skewpoint2.y -= (abs(skewpoint1.y - skewpoint2.y)/2);
CurtisJC
  • 680
  • 3
  • 9
  • 23
  • Thanks for the answer but that will just move the shape into a square rather than deskew the image won't it? Or have I misunderstood? I need to correct the image to become square. – JWood Aug 07 '12 at 17:51
  • OK, 'I also have 4 CGPoints which represent a uniform rectangle.' through me off :P i'll re-answer, still seems pretty straight forward using a similar method. – CurtisJC Aug 07 '12 at 17:57
  • Sorry, one of us is misunderstanding something here. I have both the starting points and the end points, what I need to do is transform the image to fit the regular rectangle box. So the image in the linked post would end up looking like a rectangular image rather than the skewed shape that it's in now. It's the image manipulation I'm trying to figure out, not the points. – JWood Aug 07 '12 at 18:28
  • Ahh, sorry yes I completely misunderstood. I didn't realise an image was involved. – CurtisJC Aug 08 '12 at 08:57