def identify_easy_sample(img): #mask_img = cv2.resize(mask_img, (img_start.shape[1], img.shape[0])) #img = np.concatenate((img_start, mask_img), axis=1) # filter out purple object filtered = Color_Filter.filter_colors(frame=img, show_images=False, verbose=False) # detect edges of purple object edges = Color_Filter.contour_color(frame=filtered["Median Blur"][filtered["Colors"][0]], show_images=False) # blur creates consistency blurred = cv2.GaussianBlur(edges, (5,5), 0) # get image contours (hopefully only one, the purple sample) (cnts, _) = cv2.findContours(blurred.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cv2.imshow('image', blurred) if cnts: # take the biggest contour and calculate its centroid cont = max(cnts, key = cv2.contourArea) moments = cv2.moments(cont) centx = int(moments['m10']/moments['m00']) centy = int(moments['m01']/moments['m00']) # absolute coordinates where (0,0) is bottom left return (centx,(centy)) else: return None
def check_easy_sample(left, right): xyz = None # filter image so that only purple objects show up in each image filter_left = Color_Filter.filter_colors(frame=left, show_images=False, verbose=False) filter_right = Color_Filter.filter_colors(frame=right, show_images=False, verbose=False) # detect edges of the purple objects in each image edges_left = Color_Filter.contour_color(frame=filter_left["Median Blur"][filter_left["Colors"][0]], show_images=False) edges_right = Color_Filter.contour_color(frame=filter_right["Median Blur"][filter_right["Colors"][0]], show_images=False) #cv2.imshow('LEFT', edges_left) #cv2.imshow('RIGHT', edges_right) # blur to create consistency #blurred_left = cv2.GaussianBlur(edges_left, (5,5), 0) #blurred_right = cv2.GaussianBlur(edges_right, (5,5), 0) # find image contours (cnts_left, _) = cv2.findContours(edges_left.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) (cnts_right, _) = cv2.findContours(edges_right.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) if cnts_left and cnts_right: # take the biggest contour from each image and calculate their centroids left_cont = max(cnts_left, key = cv2.contourArea) right_cont = max(cnts_right, key = cv2.contourArea) left_moments = cv2.moments(left_cont) right_moments = cv2.moments(right_cont) if left_moments['m00'] != 0 and right_moments['m00'] != 0: left_centx = int(left_moments['m10']/left_moments['m00']) right_centx = int(right_moments['m10']/right_moments['m00']) left_centy = int(left_moments['m01']/left_moments['m00']) right_centy = int(right_moments['m01']/right_moments['m00']) # calulcate distance from camera (in mm): xyz = calculate_distance(left_centx, right_centx, left_centy, right_centy) rospy.loginfo("Easy_Sample:(x,y,z): {0}".format(xyz)) else: rospy.loginfo("DIVIDE BY ZERO") else: rospy.loginfo("NO PURPLE") return None # print for testing only #cv2.imshow('LEFT', blurred_left) #cv2.imshow('RIGHT', blurred_right) # returns a tuple (x,y,z), or None if no sample is detected print "see thing at "+str(xyz) return xyz