Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CIPerspectiveCorrection filter returns image flipped and inverted

I'm using the CIPerspectiveCorrection Filter and my problem is that my returned image results are mirrored, upside down, and the points used for the perspective correction seem to be referencing the wrong axis, or axis direction.

In order to isolate the issue I have been working with a test image that is 1024 x 1024 and I am passing in a perfectly rectangular area. I'm still ending up with images flipped vertically and horizontally.

Here is my function that returns a cropped CIImage instance given an image and set of points:

 private func _getCroppedImageWithImage(image:CIImage, topLeft:CGPoint, topRight:CGPoint, botLeft:CGPoint, botRight:CGPoint) -> CIImage {
    var rectCoords = NSMutableDictionary(capacity: 4)
    rectCoords["inputTopLeft"] = CIVector(CGPoint:topLeft)
    rectCoords["inputTopRight"] = CIVector(CGPoint:topRight)
    rectCoords["inputBottomLeft"] = CIVector(CGPoint:botLeft)
    rectCoords["inputBottomRight"] = CIVector(CGPoint:botRight)
    return image.imageByApplyingFilter("CIPerspectiveCorrection", withInputParameters: rectCoords)
}

And here is where I am calling this function:

func testCrop() {

    let ciInputImage = CIImage(image:UIImage(named:"test-pattern.jpg")!)

    println("source image is \(ciInputImage)") //<CIImage: 0x170212290 extent [0 0 1024 1024]>

    let ptBotLeft = CGPointMake(32.0,992.0)
    let ptBotRight = CGPointMake(992.0,992.0)
    let ptTopRight = CGPointMake(992.0,32.0)
    let ptTopLeft = CGPointMake(32.0,32.0)

    let croppedImage = _getCroppedImageWithImage(ciInputImage, topLeft: ptTopLeft, topRight: ptTopRight, botLeft: ptBotLeft, botRight: ptBotRight)
    println("cropped image \(croppedImage)") //<CIImage: 0x174204a60 extent [0 0 960 960]>

    let croppedImageCG = CIContext(options: nil).createCGImage(croppedImage, fromRect: croppedImage.extent())

    let imageVC = ImageViewController(image: UIImage(CGImage: croppedImageCG))
    presentViewController(imageVC, animated: true, completion: nil)
}

Has anyone encountered problems like this before?

Here is the source image

enter image description here

And here is the final image displayed in a UIImageView with contentMode set to scaleAspectFit

enter image description here

like image 405
nwales Avatar asked Mar 05 '15 03:03

nwales


1 Answers

OK, my issue, I am pretty sure, is that CoreImage uses the Cartesian coordinate system. Y is up. (zero, zero) is at the bottom left.

like image 194
nwales Avatar answered Nov 03 '22 22:11

nwales