Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import the necessary packages
- import numpy as np
- import argparse
- import numpy as np
- import argparse
- import cv2
- image = cv2.imread("test.jpg")
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- gray = cv2.bitwise_not(gray)
- thresh = cv2.threshold(gray, 0, 255,
- cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
- coords = np.column_stack(np.where(thresh > 0))
- angle = cv2.minAreaRect(coords)[-1]
- if angle < -45:
- angle = -(90 + angle)
- else:
- angle = -angle
- (h, w) = image.shape[:2]
- center = (w // 2, h // 2)
- M = cv2.getRotationMatrix2D(center, angle, 1.0)
- rotated = cv2.warpAffine(image, M, (w, h),
- flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
- hist = cv2.reduce(rotated,1, cv2.REDUCE_AVG).reshape(-1)
- th = 2
- H,W = image.shape[:2]
- uppers = [y for y in range(H-1) if hist[y]<=th and hist[y+1]>th]
- lowers = [y for y in range(H-1) if hist[y]>th and hist[y+1]<=th]
- rotated = cv2.cvtColor(rotated, cv2.COLOR_GRAY2BGR)
- for y in uppers:
- cv2.line(rotated, (0,y), (W, y), (255,0,0), 1)
- for y in lowers:
- cv2.line(rotated, (0,y), (W, y), (0,255,0), 1)
- cv2.namedWindow('Result Image',cv2.WINDOW_FULLSCREEN)
- cv2.imshow('Result Image',rotated)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment