editing - Laser Rangefinder Webcam DIY Python -


after researching on web diy laser range finder webcam; found cool project finds distance of laser in cm. wondering if can me change lines of code can measure distance in yards decimals( act fractions). here link website found webcam laser rangefinder

i don't know trigonometry , know nothing beyond high school algebra , geometry remember. in other words, don't understand math link talking about; why, asking show me example of code if measured in yards instead of centimeters. thank in advanced.

here code

     ## program written shane ormonde 7th sept 2013      ## updated on 25th january 2014      ## calculates distance of red dot in field of view of webcam.      import cv2     numpy import *     import math      #variables     loop = 1      dot_dist = 0      cv2.namedwindow("preview")     vc = cv2.videocapture(1)        if vc.isopened(): # try first frame     rval, frame = vc.read()      else:             rval = false      #print "failed open webcam"       if rval == 1 :             while loop == 1:         cv2.imshow("preview", frame)         rval, frame = vc.read()         key = cv2.waitkey(20)         if key == 27: # exit on esc             loop = 0         num = (frame[...,...,2] > 236)         xy_val =  num.nonzero()          y_val = median(xy_val[0])         x_val = median(xy_val[1])          dist = ((x_val - 320)**2 + (y_val - 240)**2 )**0.5 # distance of dot center pixel         dist = abs(x_val - 320) # distance of dot center x_axis          print " dist center pixel " + str(dist)           # work out distance using d = h/tan(theta)          theta =0.0011450*dist + 0.0154                  tan_theta = math.tan(theta)            if tan_theta > 0: # bit of error checking             obj_dist =  int(5.33 / tan_theta)           print "\033[12;0h" + "the dot " + str(obj_dist) + "cm  away"         elif rval == 0:           print " webcam error " 

assuming yards = centimeters * 0.010936

change block:

if tan_theta > 0: # bit of error checking     obj_dist =  int(5.33 / tan_theta) 

to this:

if tan_theta > 0: # bit of error checking     obj_dist =  int(5.33 / tan_theta)     #convert centimeters yards     obj_dist = obj_dist * 0.010936 

if @ part tell how far dot is, see line print "\033[12;0h" + "the dot " + str(obj_dist) + "cm away". tells distance stored in variable obj_dist. looked through code find out how obj_dist got value. other time obj_dist referenced on line: obj_dist = int(5.33 / tan_theta). way view line obj_dist = centimters_away. yards, have add line converts obj_dist centimeters yards, did.


Comments