开发者

How to set Image Perspective

开发者 https://www.devze.com 2022-12-08 20:54 出处:网络
I am working in Mac OS X, 10.6 How can i set the image perspective of any image? I do not want to use CoreImage.

I am working in Mac OS X, 10.6

How can i set the image perspective of any image?

I do not want to use CoreImage.

Is it possible to do it via NSAffineTransforms.

Regards, D开发者_StackOverflow中文版hana.


For a solution that does not use CoreImage, you'll need to implement the transformation yourself. It cannot be done as an affine transform. This paper explains the process pretty good.

If you can't code it yourself, you could look at other third-party libraries that implement perspective transform. One such option would be ImageMagick. They offer perspective transform as a command line utility and they also have a C API that you could use to get the same functionality in your own program.


You can't express a perspective transform with an affine transformation. However, in CoreImage you can use an ImageUnit (CoreImage's name for what you would normally call a "filter") to do a perspective transform on an image (and many other cool things).

See CIPerspectiveTransform and check out the section about CoreImage Filters in the CoreImage developer's guide. That should get you going.

Basically what you do is...

perspectiveTransform = [CIFilter filterWithName:@"CIPerspectiveTransform"];
[perspectiveTransform setDefaults];
[perspectiveTransform setValue: myCIImage forKey: @"inputImage"];
[perspectiveTransform setValue: myToLeft forKey: @"inputTopLeft"];
// ... also set inputTopRight, inputBottomLeft and inputBottomRight
// if you have the coordinates of the corner points you can create
// CIVector instances with 
// + (CIVector *)vectorWithX:(CGFloat)x Y:(CGFloat)y
// ...
result = [perspectiveTransform valueForKey: @"outputImage"];


Another way to handle perspective on your image would be to apply a 3-D transformation, with perspective, using Core Animation. You could place your image within a CALayer (as that layer's contents), or layer-back your NSImageView. I describe how to create and apply a perspective CATransform3D to a layer in this answer, but the key is to set the m34 element of the CATransform3D to a negative fraction in order to create a perspective effect. Mike Lee has a writeup on this here, along with some sample code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号