This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Image Processing
Ryan Peach edited this page Oct 27, 2015
·
2 revisions
#Fixing Perspective #Perspective Transform
def fixPerspective(img, border,ref,ratio=8.5/11.0):
"""Returns img skewed to the border."""
out = img.copy()
#Rotate the array until the reference is first
while tuple(border[0])!=tuple(ref):
border = rotateList(border,1)
#Copied from http://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/
(tl, tr, br, bl) = border
# compute the width of the new image, which will be the
# maximum distance between bottom-right and bottom-left
# x-coordiates or the top-right and top-left x-coordinates
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
# compute the height of the new image, which will be the
# maximum distance between the top-right and bottom-right
# y-coordinates or the top-left and bottom-left y-coordinates
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))
# now that we have the dimensions of the new image, construct
# the set of destination points to obtain a "birds eye view",
# (i.e. top-down view) of the image, again specifying points
# in the top-left, top-right, bottom-right, and bottom-left
# order
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype = "float32")
#Return Perspective Transform
M = cv2.getPerspectiveTransform(np.array(border, dtype = "float32"), dst)
out = cv2.warpPerspective(img.copy(), M, (maxWidth, maxHeight))
return out
##Cropping
def cropImage(img,r):
sizeX,sizeY,p = img.shape
return img[r:sizeX-r,r:sizeY-r]
#Output Filter
def filterOut(img):
out = cv2.cvtColor(img.copy(),cv2.COLOR_RGB2GRAY)
out = cv2.adaptiveThreshold(out,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
return out