Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Evgenij-Gr/9659266 to your computer and use it in GitHub Desktop.
Save Evgenij-Gr/9659266 to your computer and use it in GitHub Desktop.
Suppose that we have an image on the plane, which borders are parallel to coordinate axis. This procedure does the mapping from plane coordinates to pixels of image.
#! /usr/bin/python
# We lay sides of image parallel to axi of planes
# Bigger side of image maps to bigger side of rect
# On images we always have X ~ Width, Y ~ Height (it is true for OpenCV)
def PlaneToImage(Point, LTCorner, RBCorner, resW, resH):
assert(not resW==resH)
if (resW > resH):
if (abs(LTCorner[0]-RBCorner[0]) > abs(LTCorner[1]-RBCorner[1])):
OnImgX, OnImgY = 0, 1
else:
OnImgX, OnImgY = 1, 0
else:
if (abs(LTCorner[0]-RBCorner[0]) > abs(LTCorner[1]-RBCorner[1])):
OnImgX, OnImgY = 1, 0
else:
OnImgX, OnImgY = 0, 1
OWidth, OHeight = float(RBCorner[OnImgX]-LTCorner[OnImgX]), float(RBCorner[OnImgY]- LTCorner[OnImgY])
NImgX, NImgY = float(Point[OnImgX]-LTCorner[OnImgX])/OWidth, float(Point[OnImgY]-LTCorner[OnImgY])/OHeight
return [int(resW*NImgX), int(resH*NImgY)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment