IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit

Modules demonstrating how apply a 2d warping transformation on an image. More...

Modules demonstrating how apply a 2d warping transformation on an image.

This sample illustrate how to correct perspective effects on a plan.

We will define (at least) four input points in input image which will be used to correct perspective effects :

warp2d_input.png

We start by importing all necessary libraries:

import os
import PyIPSDK
import PyIPSDK.IPSDKIPLGeometricTransform as gtrans

Then we load input image :

# opening of input image
imagesSamplePath = PyIPSDK.getIPSDKDirectory(PyIPSDK.eInternalDirectory.eID_Images)
inputImgPath = os.path.join(imagesSamplePath, "IPSDK_flyer1.tif")
inImg = PyIPSDK.loadTiffImageFile(inputImgPath)

We then select input image points for which we define target output image coordinates :

# we define a 2d mapping between 4 points of input image and target points
# for output image
# on output we want to get a rectangular view
targetWidth = 400
targetHeight = 550
inPtColl = [[327, 83],
[697, 216],
[459, 763],
[29, 552]]
outPtColl = [[100, 100],
[100+targetWidth, 100],
[100+targetWidth, 100+targetHeight],
[100, 100+targetHeight]]

These data allow us to compute associated homography 2d transform :

# we compute associated homography
transform, res = PyIPSDK.homographyTransform2dSimpleEstimation(inPtColl, outPtColl)

For more informations about parametric estimation features, please see Parametric estimation.

Computed transform can then be used to warp input image to output image :

# application of warping transform
outImg = gtrans.warp2dImg(inImg, transform)
warp2d_output.png
See also
Warping 2d algorithm for more informations
Note
user could define and pass output image to warping function to define a target output image size.

At last we save output image :

# save generated image
tmpPath = PyIPSDK.getIPSDKDefaultDirectory(PyIPSDK.eDefaultExternalDirectory.eDED_Tmp)
outputImgPath = os.path.join(tmpPath, "warp2dImg.tif")
print("Writing result in file : " + outputImgPath)
PyIPSDK.saveTiffImageFile(outputImgPath, outImg)

See the full source listing