1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
| import time from robomaster import robot,battery from robomaster import camera import cv2 import numpy as np import sys sys.path.append(r'P:\Python\cv2') from string_recognise_self import cv_show,FillHole,cv_drawContours,cv_center,cv_erodedAndDilated
"""碎片填充""" def FillHole(img,SavePath='img_fillhole.jpg'): img_in = img img_floodfill = img_in.copy() h, w = img_in.shape[:2] mask = np.zeros((h+2, w+2), np.uint8) isbreak = False for i in range(img_floodfill.shape[0]): for j in range(img_floodfill.shape[1]): if(img_floodfill[i][j]==0): seedPoint=(i,j) isbreak = True break if(isbreak): break cv2.floodFill(img_floodfill, mask, seedPoint, 255); img_floodfill_inv = cv2.bitwise_not(img_floodfill) img_out = img_in | img_floodfill_inv cv2.imwrite(SavePath, img_out) return img_out """腐蚀与膨胀""" def cv_erodedAndDilated(img): size = 20 kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(size,size)) eroded = cv2.erode(img, kernel) img_dilated = cv2.dilate(eroded, kernel) return img_dilated
"""小球位置识别""" def cv_center(img): kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) height,width,layer = img.shape center_x = height/2 center_y = width/2 sensity = 10 green_lower = np.array([50-sensity,43,46]) green_upper = np.array([60+sensity,255,255]) hsv_frame = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv_frame, green_lower,green_upper) mask2 = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) mask3 = cv2.morphologyEx(mask2, cv2.MORPH_CLOSE, kernel) mask4 = FillHole(mask3) mask5 = cv_erodedAndDilated(mask4) cnt,heridency = cv2.findContours(mask5, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if cnt == []: continue maxArea = 0 maxIndex = 0 for i, j in enumerate(cnt): area = cv2.contourArea(j) if area > maxArea: maxArea = area maxIndex = i cv2.drawContours(img,cnt,maxIndex,(255,0,255),10) x, y, w, h = cv2.boundingRect(np.array(cnt[maxIndex])) cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) size = 6000 area = w*h robot_x = x+w/2 robot_y = y+h/2 left_sign = 0 right_sign = 0 forward_sign = 0 up_sign = 0 down_sign = 0 if area < size and area > size-2000: forward_sign = 1 elif area > size-4000 and area <= size-2000: forward_sign = 2 elif area <= size-4000 and size > 0: forward_sign = 3 if area >= size and area < size+100000: forward_sign = -1 elif area >= size+100000 and area < size+300000: forward_sign = -2 elif area > size+300000: forward_sign = -3 if robot_x < center_x and robot_x > center_x-120: left_sign = 1 elif robot_x <= center_x-120 and robot_x >= center_x-240: left_sign = 2 elif robot_x < center_x-240: left_sign =3 if robot_x > center_x and robot_x < center_x+120: right_sign = 1 elif robot_x >= center_x+120 and robot_x <= center_x+240: right_sign = 2 elif robot_x > center_x+240: right_sign =3 return forward_sign,left_sign,right_sign
if __name__ == '__main__': tl_drone = robot.Drone() tl_drone.initialize() tl_flight = tl_drone.flight tl_camera = tl_drone.camera tl_battery = tl_drone.battery print('battery=\t'+str(tl_battery.get_battery())+'%') cnt = [] print(tl_drone.get_temp()) tl_flight.takeoff().wait_for_completed() tl_camera.set_fps(fps='high') tl_camera.set_bitrate(bitrate=6) tl_camera.set_resolution(resolution='high') tl_camera.start_video_stream(display=False) try: while True: img = tl_camera.read_cv2_image(strategy='newest') forward,left,right = cv_center(img) """ 控制飞机遥控器的四个杆量 参数: a – float:[-100, 100] 横滚 b – float:[-100, 100] 俯仰 c – float:[-100, 100] 油门 d – float:[-100, 100] 偏航 """ a_level = [25,35,45] b_level_forward = [15,25,40] b_level_backward = [-30,-45,-60] if forward == 1: tl_flight.rc(b=b_level_forward[0]) elif forward == 2: tl_flight.rc(b=b_level_forward[1]) elif forward == 3: tl_flight.rc(b=b_level_forward[2]) elif forward == 0: tl_flight.rc(b=0) elif forward == -1: tl_flight.rc(b=b_level_backward[0]) elif forward == -2: tl_flight.rc(b=b_level_backward[1]) elif forward == -3: tl_flight.rc(b=b_level_backward[2]) if left == 1: tl_flight.rc(a=-a_level[0]) elif left == 2: tl_flight.rc(a=-a_level[1]) elif left == 3: tl_flight.rc(a=-a_level[2]) elif right == 1: tl_flight.rc(a=a_level[0]) elif right == 2: tl_flight.rc(a=a_level[1]) elif right == 3: tl_flight.rc(a=a_level[2]) elif right == 0 and left == 0: tl_flight.rc(a=0) time.sleep(0.08) cv2.namedWindow('Drone', 0) cv2.imshow("Drone", img) cv2.waitKey(10) tl_flight.rc(a=0,b=0,c=0,d=0) except KeyboardInterrupt: pass cv2.destroyAllWindows() tl_camera.stop_video_stream() tl_flight.land().wait_for_completed() tl_drone.close()
|