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 :
We start by importing all necessary libraries:
import os
import PyIPSDK
import PyIPSDK.IPSDKIPLGeometricTransform as gtrans
Then we load 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 :
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 :
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 :
outImg = gtrans.warp2dImg(inImg, transform)
- 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 :
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