CIPerspectiveCorrection滤镜返回翻转和倒置的图像

10

我正在使用CIPerspectiveCorrection Filter,但我的问题是返回的图像结果镜像、上下颠倒,并且用于透视校正的点似乎引用了错误的轴或轴方向。

为了隔离问题,我一直在使用一个大小为1024 x 1024的测试图像,并传入一个完全矩形的区域。但我最终得到的图像仍然会垂直和水平翻转。

这是我的函数,它根据图像和一组点返回裁剪后的CIImage实例:

 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)
}

这里是我调用这个函数的地方:

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)
}

有人以前遇到过类似的问题吗?

这是原始图像:

enter image description here

这是使用contentMode设置为scaleAspectFit显示的最终图像:

enter image description here

1个回答

15

好的,我的问题,我相当确定,就是CoreImage使用笛卡尔坐标系。 Y轴向上。 (0,0) 在左下角。


9
func cartesianForPoint(point:CGPoint,extent:CGRect) -> CGPoint { return CGPoint(x: point.x,y: extent.height - point.y) }翻译为中文:给定一个点的坐标和一个矩形区域,该函数返回该点的笛卡尔坐标系下的坐标。具体而言,该函数将该点的y值从矩形区域的高度中减去,然后返回相应的CGPoint。 - nwales
我无法感谢你上一个函数的帮助!我知道问题出在哪里,但是由于花费了很多时间在我的项目上,我无法想出如何进行转换! - Fengson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接