Diana
Diana

Reputation: 713

iOS Swift How to warp part of image from detected source point to destination point?

I’ve detected specific part of image (blue ball) and I need to warp this part from upper-point to lower-point. I’ve found WarpGeometryFilter but the problem is that it warp full image to point of detected part. How to set source and destination properly?I need to go from this:

enter image description here

To this: enter image description here

//Detected points part:
ball.normalizedPoints.forEach { point in
let ballPosition = vector_float2(x: Float(point.x), y: Float(point.y))
ballVectorArray.append(ballPosition) //I've got 6 values 

//Warp part
let filter: WarpGeometryFilter = .init(device: MTLCreateSystemDefaultDevice()!)

        let source: [vector_float2] = ? (I've thought it would be my ballVectorArray but it works wrong)
        
        let destination: [vector_float2] = ?
        
        let warpGeometry = SKWarpGeometryGrid(columns: ?, rows: ?, sourcePositions: source, destinationPositions: destination)

        filter.setValue(sourceImage, forKey: kCIInputImageKey)
        filter.setValue(warpGeometry, forKey: kCIInputWarpGeometryKey)

        let result = filter.outputImage!

If I'll set source and destination like this

let source: [vector_float2] = ballVectorArray //[SIMD2<Float>(0.23153101, 0.7204376), SIMD2<Float>(0.2829985, 0.74947053), SIMD2<Float>(0.36217925, 0.74208033), SIMD2<Float>(0.4081041, 0.7040736), SIMD2<Float>(0.35399726, 0.6966834), SIMD2<Float>(0.28009522, 0.69483584)]
let destination: [vector_float2] = ballVectorNewArray// [SIMD2<Float>(0.13153102, 0.62043756), SIMD2<Float>(0.18299851, 0.6494705), SIMD2<Float>(0.26217926, 0.6420803), SIMD2<Float>(0.3081041, 0.6040736), SIMD2<Float>(0.25399727, 0.5966834), SIMD2<Float>(0.18009523, 0.5948358)]

I get this result: enter image description here

Upvotes: 0

Views: 132

Answers (0)

Related Questions