Skip to content

Instantly share code, notes, and snippets.

@djnugent
Created August 1, 2017 22:31
Show Gist options
  • Save djnugent/31605c57cab00d93d48cf3b5c4863ef1 to your computer and use it in GitHub Desktop.
Save djnugent/31605c57cab00d93d48cf3b5c4863ef1 to your computer and use it in GitHub Desktop.
import cv2
import imageio
import numpy as np
def nothing():
pass
cv2.namedWindow('image')
# create trackbars for color change
cv2.createTrackbar('shift','image',50,100,nothing)
cv2.createTrackbar('rotation','image',0,25,nothing)
def perspective_transform(img):
# Choose an offset from image corners to plot detected corners
lane_width = 170 #cv2.getTrackbarPos('lane_width','image')
apex_width = 210 #cv2.getTrackbarPos('apex_width','image')
horizon0 = 460 #cv2.getTrackbarPos('h0','image')
horizon1 = 500 #cv2.getTrackbarPos('h1','image')
# Grab the image shape
h,w,c = img.shape
# Specify the transform
src = np.float32([[0,h],
[w,h],
[int(w/2 - apex_width/2),horizon0],
[int(w/2 + apex_width/2),horizon0]])
dst = np.float32([[int(w/2 - lane_width/2), h],
[int(w/2 + lane_width/2), h],
[int(w/2 - lane_width/2), horizon1],
[int(w/2 + lane_width/2), horizon1]])
# Given src and dst points, calculate the perspective transform matrix
M = cv2.getPerspectiveTransform(src, dst)
inv_M = cv2.getPerspectiveTransform(dst, src)
# Warp the image using OpenCV warpPerspective()
warped = cv2.warpPerspective(img, M, (w,h))
return warped, M, inv_M
def warpPoints(pnts,M):
new_pnts = []
for pnt in pnts:
new_pnts.append(np.matmul(M,np.array([pnt[0],pnt[1],1])))
return new_pnts
def shiftPoints(pnts,x,y):
M = np.float32([[1,0,x],[0,1,y],[0,0,1]])
return cv2.perspectiveTransform(np.array([pnts]),M)[0]
def rotatePoints(pnts,img_shape,angle):
w,h,c = img_shape
center = int(w/2),h
M = cv2.getRotationMatrix2D(center,angle,1.0)
M = np.concatenate((M, np.array([[0,0,1]])), axis=0)
return cv2.perspectiveTransform(np.array([pnts]),M)[0]
def augment(img, rotation, shift, M, inv_M):
h,w,c = img.shape
# Warp points birds eye view
src = np.array([[200,500],[555,500],[80,550],[577,550]], dtype='float32')
warped = cv2.perspectiveTransform(np.array([src]),M)[0]
# shift points
x = shift
y = 0
trans_M = np.float32([[1,0,x],[0,1,y],[0,0,1]])
warped_shifted = cv2.perspectiveTransform(np.array([warped]),trans_M)[0]
print(warped_shifted)
# Rotate points
center = int(w/2),h
rot_M = cv2.getRotationMatrix2D(center,rotation,1.0)
rot_M = np.concatenate((rot_M, np.array([[0,0,1]])), axis=0)
warped_rot = cv2.perspectiveTransform(np.array([warped_shifted]),rot_M)[0]
print(warped_rot)
# Warp back to regular perspective
warped_final = cv2.perspectiveTransform(np.array([warped_rot]),inv_M)[0]
# Apply transfom to image
final_M = cv2.getPerspectiveTransform(src, warped_final)
# Warp the image using OpenCV warpPerspective()
result = cv2.warpPerspective(img, final_M, (w,h))
return result
while True:
'''
workflow:
- perspective map points
- shift points
- rotate points
- inverse perspective map
- warp image to points
'''
im = cv2.imread("ats.jpg")
warped, M,inv_M = perspective_transform(im)
shift = cv2.getTrackbarPos('shift','image')
rot = cv2.getTrackbarPos('rotation','image')
result = augment(im,rot,shift,M,inv_M)
#cv2.imshow("orig",im)
cv2.imshow("result",result)
cv2.waitKey(1)
'''
pnts = np.array([[200,500],[555,500],[80,550],[577,550]], dtype='float32')
warped, m, inv_m = perspective_transform(im)
warped_pnts = cv2.perspectiveTransform(np.array([pnts]),m)[0]
warped_pnts = shiftPoints(warped_pnts,100,0)
warped_pnts = rotatePoints(warped_pnts,im.shape,45)
print(warped_pnts)
for pnt in pnts:
cv2.circle(im,(pnt[0],pnt[1]),2,(0,0,255))
cv2.imshow("im",im)
cv2.waitKey(0)
for pnt in warped_pnts:
print(pnt)
cv2.circle(warped,(int(pnt[0]),int(pnt[1])),2,(0,0,255))
cv2.imshow("warped",warped)
cv2.waitKey(0)
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment