Advertisement
Guest User

kodzaizvestaj

a guest
Jun 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. import numpy as np
  2. from matplotlib import pyplot as plt
  3. import cv2
  4.  
  5. def find_marker(image):
  6.     # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  7.     gray = cv2.GaussianBlur(image, (5, 5), 0)
  8.     cv2.imwrite("faza1.jpg", image)
  9.     edged = cv2.Canny(gray, 35, 125)
  10.     cv2.imwrite("faza2.jpg", image)
  11.  
  12.     (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
  13.     c = max(cnts, key=cv2.contourArea)
  14.     return cv2.minAreaRect(c)
  15.  
  16.  
  17. def distance_to_camera(knownWidth, focalLength, perWidth):
  18.     return (knownWidth * focalLength) / perWidth
  19.  
  20. known_d = 97 # cm
  21. known_w = 29.5 # cm
  22. image_path = 'totka.jpg' # postavete pateka do vasita slika, ako kalibrirate od 2 sliki izvrete dvapati
  23.  
  24. image = cv2.imread(image_path)
  25. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # rgb to gray
  26.  
  27. marker = find_marker(gray)
  28. print marker
  29. focal = (marker[1][0] * known_d) / known_w
  30. print 'focalno rastojanie:', focal
  31.  
  32. box = np.int0(cv2.cv.BoxPoints(marker))
  33. cv2.drawContours(image, [box], -1, (0, 255, 0), 2)
  34.  
  35. cv2.imwrite("faza4.jpg", image)
  36. #cv2.imshow("image", image)
  37.  
  38. cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement