tags:

views:

412

answers:

4

Hi,

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, Dhana.

A: 

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"];
VoidPointer
Yes, Using CoreImage filters works fine.But below mentioned defect arises.http://stackoverflow.com/questions/1553793/coreimage-patches-problems-in-10-6So i want to avoid using this to get rid of the bug.
Dhanaraj
Sorry. I read that you wanted to use CoreImage not that you specifically didn't want to use it.
VoidPointer
+1  A: 

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.

VoidPointer
Thanks VP.Is there any way to tackle patches issues with CoreImage in 10.6?
Dhanaraj
Have you tried disabling hardware rendering? See: http://stackoverflow.com/questions/1553793/coreimage-patches-problems-in-10-6/1554740#1554740
VoidPointer
A: 

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.

Brad Larson
A: 

Hello, I have the Core Image Problems in 10.6. I try this:

// Create CIImage from CGImageRef (contents CALayer)
CIImage *myCIImage = [CIImage imageWithCGImage:(CGImageRef) sublayer.contents];

// Create PerspectiveTransform filter
CIFilter *perspectiveFilter = [CIFilter filterWithName:@"CIPerspectiveTransform"];
// Set filter's parameter
[perspectiveFilter setDefaults];
[perspectiveFilter setValue:myCIImage forKey:@"inputImage"];
[perspectiveFilter setValue:topLeft forKey:@"inputTopLeft"];
// And the other ones using CIVector
[perspectiveFilter setName:@"perspective"];
// Set filter's parameter
[perspectiveFilter setDefaults];
[perspectiveFilter setValue:myCIImage forKey:@"inputImage"];
[perspectiveFilter setValue:topLeft forKey:@"inputTopLeft"];
// And the other ones using CIVector
[perspectiveFilter setName:@"perspective"];

// Apply CIPerspectiveTransform
sublayer.filters = [NSArray arrayWithObject:perspectiveFilter];

I compile and build and when I apply filter in runtime, console print this:

CoreImage: ROI is not tilable:
APPLY perspective DOD [50,50 50x50] ROI [50,50 26x26] RGBA_H
AFFINE [1 -0 -0 1 -16517.9 -16564] DOD [-16517.9,-16564 32768x32768] ROI [-16518,-16564   17041x17041] ARGB_8 ...

I have read this post: Core Image Patches Problems

How I could disable the hardware renderer? Where I have to read and apply CIContext? Excuse my bad english, thank.

soulless
This does not answer this question. Please post it as a separate question.
Peter Hosey