Guest User

Untitled

a guest
Feb 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. # import the necessary packages
  2.  
  3. import numpy as np
  4.  
  5. import argparse
  6. import numpy as np
  7. import argparse
  8. import cv2
  9.  
  10. image = cv2.imread("test.jpg")
  11.  
  12. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  13. gray = cv2.bitwise_not(gray)
  14. thresh = cv2.threshold(gray, 0, 255,
  15. cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
  16. coords = np.column_stack(np.where(thresh > 0))
  17. angle = cv2.minAreaRect(coords)[-1]
  18. if angle < -45:
  19. angle = -(90 + angle)
  20. else:
  21. angle = -angle
  22. (h, w) = image.shape[:2]
  23. center = (w // 2, h // 2)
  24. M = cv2.getRotationMatrix2D(center, angle, 1.0)
  25. rotated = cv2.warpAffine(image, M, (w, h),
  26. flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
  27. hist = cv2.reduce(rotated,1, cv2.REDUCE_AVG).reshape(-1)
  28.  
  29. th = 2
  30. H,W = image.shape[:2]
  31. uppers = [y for y in range(H-1) if hist[y]<=th and hist[y+1]>th]
  32. lowers = [y for y in range(H-1) if hist[y]>th and hist[y+1]<=th]
  33. rotated = cv2.cvtColor(rotated, cv2.COLOR_GRAY2BGR)
  34. for y in uppers:
  35. cv2.line(rotated, (0,y), (W, y), (255,0,0), 1)
  36.  
  37. for y in lowers:
  38. cv2.line(rotated, (0,y), (W, y), (0,255,0), 1)
  39.  
  40. cv2.namedWindow('Result Image',cv2.WINDOW_FULLSCREEN)
  41. cv2.imshow('Result Image',rotated)
  42. cv2.waitKey(0)
  43. cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment