class TargetProcessor: def __init__(self): self.rectHeight = 0.02 self.rectWidth = 0.05 self.focalLen = 480 self.horizCent = 240 self.vertiCent = 320 def loadTarget(self, approx): '''Does all the higher level calculations. Takes an input array of points and sends it to the Target object. Then takes the width and height from the target object and calculates Distance, Azimuth, and Altitude. ''' self.dist = 0 self.azimuth = 0 self.altitude = 0 self.targetType = -1 self.target = Target(approx) imgWidth = self.target.getWidth() imgHeight = self.target.getHeight() imgCenter = self.target.getCenter() rectCentX = imgCenter[0] rectCentY = imgCenter[1] offsetX = float(rectCentX - self.horizCent) offsetY = float(rectCentY - self.vertiCent) self.targetType = self.target.getType() self.dist = self.rectWidth * self.focalLen / imgWidth self.azimuth = np.arctan(offsetX / self.focalLen) * 180 / math.pi self.altitude = np.arctan(offsetY / self.focalLen) * 180 / math.pi def getType(self): '''Returns the target type''' return self.targetType def getAzimuth(self): '''Returns the azimuth''' return self.azimuth
proc = Processor() # makes a new TargetProcessor object if not ret: # checks if boolean value ret is false continue threshold = det.threshold(minThreshold, maxThreshold, frame) # getting thresholded frame det.Contours(threshold) # finding contours based on thresholded frame det.filterContours() # filtering the contours by size and number contours, index, corners, isCross, isRect = det.getContours( ) # getting the contours, specific index, and array of corners if (corners is not None): # checking if the corners array returned is not null target = Target(corners) # making a new Target object Imagewidth = target.getWidth() Xmid, Ymid = target.getCenter() cv2.line(frame, (Xmid, Ymid), (Xmid, Ymid), lightblue, 5) cv2.drawContours(frame, contours, index, lightblue, 8) if (isRect): proc.calculate(focalLength, rectActualWidth, Imagewidth, Xmid - imgXcenter, imgYcenter - Ymid) network.setRectAzi(proc.getAzimuth()) if (isCross): proc.calculate(focalLength, crossActualWidth, Imagewidth, Xmid - imgXcenter, imgYcenter - Ymid) network.setCrossAzi(proc.getAzimuth()) contoured = cv2.resize(frame, None, fx=0.5, fy=0.5) threshed = cv2.resize(threshold, None, fx=0.5, fy=0.5) displayValues() # method displays values in terminal cv2.imshow("contoured", contoured)
detector = TargetDetector() target = Target() processor = TargetProcessor() plusCount = 0 rectCount = 0 while (True): ret, frame = cam.read() if not ret: continue detector.threshold(frame) detector.contour() targetApprox = detector.getTargetApprox() width = target.getWidth(targetApprox) height = target.getHeight(targetApprox) centerX, centerY = target.getCenter() shape = target.getShape(targetApprox) processor.calculate(width, height, centerX, centerY, detector.getHSV(), shape) azi = processor.getAzi() alti = processor.getAlti() dist = processor.getDist() cv2.imshow("Threshed Image", detector.getThreshed()) cv2.imshow("Contoured Image", detector.getContour()) print(shape) print("Azimuth: " + str(azi)) print("Altitude: " + str(alti)) print("Distance: " + str(dist)) if (cv2.waitKey(10) == 27): break