Skip to content

Instantly share code, notes, and snippets.

@webstory
Last active May 24, 2018 01:55
Show Gist options
  • Save webstory/87d558bbeae114cec72eebc1a3d41b54 to your computer and use it in GitHub Desktop.
Save webstory/87d558bbeae114cec72eebc1a3d41b54 to your computer and use it in GitHub Desktop.
Affine transform with python opencv
import pyproj
import cv2
import numpy as np
import csv
EPSG5174 = pyproj.Proj("+proj=tmerc +lat_0=38 +lon_0=127.0028902777778 +k=1 +x_0=200000 +y_0=500000 +ellps=bessel +towgs84=-115.80,474.99,674.11,1.16,-2.31,-1.63,6.43 +units=m +no_defs")
EPSG5179 = pyproj.Proj("+proj=tmerc +lat_0=38 +lon_0=127.5 +k=0.9996 +x_0=1000000 +y_0=2000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs") # UTMK
EPSG5186 = pyproj.Proj("+proj=tmerc +lat_0=38 +lon_0=127 +k=1 +x_0=200000 +y_0=600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs")
fromarray = []
toarray = []
scale = 0.005
links = ['uleungdo_links.csv', 'dokdo_links.csv']
with open(links[1], mode='r', newline='') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
idx, x1, y1, x2, y2 = row
x1, y1 = pyproj.transform(EPSG5186, EPSG5179, x1, y1)
x2, y2 = pyproj.transform(EPSG5186, EPSG5179, x2, y2)
fromarray.append([float(x1), float(y1)])
toarray.append([float(x2), float(y2)])
# img = cv2.imread('input.jpg')
# rows, cols = img.shape[:2]
src_points = np.float32(fromarray)
dst_points = np.float32(toarray)
# affine_matrix, mask = cv2.findHomography(src_points, dst_points)
affine_matrix = cv2.getAffineTransform(src_points, dst_points)
print(affine_matrix)
# img_output = cv2.warpPerspective(img, affine_matrix, (cols, rows))
# img_output = cv2.warpAffine(img, affine_matrix, (cols, rows))
# cv2.imwrite('output.jpg', img_output)
numpy==1.14.1
opencv-python==3.4.0.12
pkg-resources==0.0.0
pyproj==1.9.5.1
virtualenv -p /usr/bin/python3 venv
source venv/bin/activate
pip install opencv-python numpy pyproj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment