Advertisement
artemgf

bred

Jun 10th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import math
  4.  
  5. cap = cv2.VideoCapture(0)
  6. vertices = None
  7. area = None
  8.  
  9.  
  10. def calc_box_vector(box):
  11.     v_side = math.sqrt((box[0][0] - box[3][0]) ** 2 + (box[0][1] - box[3][1]) ** 2)
  12.     h_side = math.sqrt((box[0][0] - box[1][0]) ** 2 + (box[0][1] - box[1][1]) ** 2)
  13.     idx = [0, 1, 2, 3]
  14.     if v_side < h_side:
  15.         idx = [0, 3, 1, 2]
  16.     return ((box[idx[0]][0] + box[idx[1]][0]) / 2, (box[idx[0]][1] + box[idx[1]][1]) / 2), (
  17.         (box[idx[2]][0] + box[idx[3]][0]) / 2, (box[idx[2]][1] + box[idx[3]][1]) / 2)
  18.  
  19.  
  20. def get_vert_angle(p1, p2, w, h):
  21.     px1 = p1[0] - w / 2
  22.     px2 = p2[0] - w / 2
  23.  
  24.     py1 = h - p1[1]
  25.     py2 = h - p2[1]
  26.  
  27.     angle = 90
  28.     if px1 != px2:
  29.         a = float(py2 - py1) / (px2 - px1) if px2 != px1 else 0
  30.         b = py1 - a * px1
  31.         angle = 0
  32.         if a != 0:
  33.             x0 = -b / a
  34.             y1 = 1.0
  35.             x1 = (y1 - b) / a
  36.             dx = x1 - x0
  37.             tg = y1 * y1 / dx / dx
  38.             angle = 180 * np.arctan(tg) / np.pi
  39.             if a < 0:
  40.                 angle = 180 - angle
  41.     return angle
  42.  
  43.  
  44. T = 140
  45.  
  46. def order_box(box):
  47.     srt = np.argsort(box[:, 1])
  48.     btm1 = box[srt[0]]
  49.     btm2 = box[srt[1]]
  50.  
  51.     top1 = box[srt[2]]
  52.     top2 = box[srt[3]]
  53.  
  54.     bc = btm1[0] < btm2[0]
  55.     btm_l = btm1 if bc else btm2
  56.     btm_r = btm2 if bc else btm1
  57.  
  58.     tc = top1[0] < top2[0]
  59.     top_l = top1 if tc else top2
  60.     top_r = top2 if tc else top1
  61.  
  62.     return np.array([top_l, top_r, btm_r, btm_l])
  63.  
  64.  
  65. def find_main_countour(image):
  66.     im2, cnts, hierarchy = cv2.findContours(image, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
  67.  
  68.     C = None
  69.     if cnts is not None and len(cnts) > 0:
  70.         C = max(cnts, key=cv2.contourArea)
  71.  
  72.     if C is None:
  73.         return None, None
  74.  
  75.     rect = cv2.minAreaRect(C)
  76.     box = cv2.boxPoints(rect)
  77.     box = np.int0(box)
  78.     box = order_box(box)
  79.     return C, box
  80.  
  81.  
  82. while 1:
  83.     s, image = cap.read()
  84.     h, w = image.shape[:2]
  85.  
  86.     hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
  87.     blurred = cv2.GaussianBlur(hsv, (9, 9), 0)
  88.  
  89.     cropped = cv2.inRange(blurred,(0, 0, 0),(188, 255, 32))
  90.     if cropped is None:
  91.         continue
  92.     cont, box = find_main_countour(cropped)
  93.     if cont is None:
  94.         continue
  95.     p1, p2 = calc_box_vector(box)
  96.  
  97.     angle = get_vert_angle(p1, p2, w, h)
  98.     shift = 100 * (p1[0] - w / 2) / (w / 2)
  99.  
  100.     cv2.drawContours(image, [cont], -1, (0, 0, 255), 3)
  101.     cv2.drawContours(image, [box], 0, (255, 0, 0), 2)
  102.     cv2.line(image, (int(p1[0]), int(p1[1])), (int(p2[0]), int(p2[1])), (0, 255, 0), 3)
  103.     msg_a = "Angle {0}".format(int(angle))
  104.     msg_s = "Shift {0}".format(int(shift))
  105.  
  106.     cv2.putText(image, msg_a, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
  107.     cv2.putText(image, msg_s, (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
  108.  
  109.     cv2.imshow("Image", image)
  110.  
  111.     ch = cv2.waitKey(5)  # ожидание кнопки для закрытия P.S. не помню какая кнопка, но ESC работает
  112.     if ch == 27:
  113.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement