def locate(self, geo_image, image, marked_image): '''Find QR codes in image and decode them. Return list of FieldItems representing valid QR codes.''' # Threshold grayscaled image to make white QR codes stands out. #gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #_, mask = cv2.threshold(gray_image, 100, 255, 0) hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) lower_white = np.array([0, 0, 160], np.uint8) upper_white = np.array([179, 65, 255], np.uint8) mask = cv2.inRange(hsv_image, lower_white, upper_white) # Find outer contours (edges) and 'approximate' them to reduce the number of points along nearly straight segments. contours, hierarchy = cv2.findContours(mask.copy(), cv2.cv.CV_RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #contours = [cv2.approxPolyDP(contour, .1, True) for contour in contours] # Create bounding box for each contour. bounding_rectangles = [cv2.minAreaRect(contour) for contour in contours] # Remove any rectangles that couldn't be a QR item based off specified side length. filtered_rectangles = filter_by_size(bounding_rectangles, geo_image.resolution, self.qr_min_size, self.qr_max_size) if ImageWriter.level <= ImageWriter.DEBUG: # Debug save intermediate images thresh_filename = postfix_filename(geo_image.file_name, 'thresh') ImageWriter.save_debug(thresh_filename, mask) # Scan each rectangle with QR reader to remove false positives and also extract data from code. qr_items = [] for rectangle in filtered_rectangles: qr_data = self.scan_image_different_trims_and_threshs(image, rectangle, trims=[0, 3, 8, 12]) scan_successful = len(qr_data) != 0 and len(qr_data[0]) != 0 if scan_successful: qr_code = create_qr_code(qr_data[0]) if qr_code is None: print 'WARNING: Invalid QR data found ' + qr_data[0] else: qr_code.bounding_rect = rectangle qr_items.append(qr_code) elif self.missed_code_finder is not None: # Scan wasn't successful so tag this rectangle for later analysis. self.missed_code_finder.add_possibly_missed_code(rectangle, geo_image) if marked_image is not None: # Show success/failure using colored bounding box success_color = (0, 255, 0) # green if scan_successful and qr_code is not None and qr_code.type == 'RowCode': success_color = (0, 255, 255) # yellow for row codes failure_color = (0, 0, 255) # red item_color = success_color if scan_successful else failure_color draw_rect(marked_image, rectangle, item_color, thickness=2) return qr_items
def locate(self, geo_image, image, marked_image): '''Find possible plant leaves in image and return list of rotated rectangle instances.''' # Convert Blue-Green-Red color space to HSV hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # Threshold the HSV image to get only green colors that correspond to healthy plants. lower_green = np.array([35, 80, 20], np.uint8) upper_green = np.array([90, 255, 255], np.uint8) plant_mask = cv2.inRange(hsv_image, lower_green, upper_green) # Now do the same thing for greenish dead plants. #lower_dead_green = np.array([10, 35, 60], np.uint8) #upper_dead_green = np.array([90, 255, 255], np.uint8) #dead_green_plant_mask = cv2.inRange(hsv_image, lower_dead_green, upper_dead_green) # Now do the same thing for yellowish dead plants. #lower_yellow = np.array([10, 50, 125], np.uint8) #upper_yellow = np.array([40, 255, 255], np.uint8) #dead_yellow_plant_mask = cv2.inRange(hsv_image, lower_yellow, upper_yellow) filtered_rectangles = [] for i, mask in enumerate([plant_mask]): # Open mask (to remove noise) and then dilate it to connect contours. kernel = np.ones((3,3), np.uint8) mask_open = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) mask = cv2.dilate(mask_open, kernel, iterations = 1) # Find outer contours (edges) and 'approximate' them to reduce the number of points along nearly straight segments. contours, hierarchy = cv2.findContours(mask.copy(), cv2.cv.CV_RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Create bounding box for each contour. bounding_rectangles = [cv2.minAreaRect(contour) for contour in contours] if marked_image is not None and ImageWriter.level <= ImageWriter.DEBUG: for rectangle in bounding_rectangles: # Show rectangles using bounding box. draw_rect(marked_image, rectangle, (0,0,0), thickness=2) # Remove any rectangles that couldn't be a plant based off specified size. filtered_rectangles.extend(filter_by_size(bounding_rectangles, geo_image.resolution, self.min_leaf_size, self.max_leaf_size, enforce_min_on_w_and_h=False)) if ImageWriter.level <= ImageWriter.DEBUG: # Debug save intermediate images mask_filename = postfix_filename(geo_image.file_name, 'mask_{}'.format(i)) ImageWriter.save_debug(mask_filename, mask) if marked_image is not None: for rectangle in filtered_rectangles: # Show rectangles using colored bounding box. purple = (255, 0, 255) draw_rect(marked_image, rectangle, purple, thickness=2) return filtered_rectangles
def locate(self, geo_image, image, marked_image): '''Find possible blue sticks in image and return list of rotated bounding box instances.''' # Convert Blue-Green-Red color space to HSV hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # Create binary mask image where potential sticks are white. lower_blue = np.array([90, 31, 16], np.uint8) upper_blue = np.array([130, 255, 255], np.uint8) mask = cv2.inRange(hsv_image, lower_blue, upper_blue) # Open mask (to remove noise) and then dilate it to connect contours. kernel = np.ones((3,3), np.uint8) mask_open = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) mask = cv2.dilate(mask_open, kernel, iterations = 1) # Find outer contours (edges) contours, hierarchy = cv2.findContours(mask.copy(), cv2.cv.CV_RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Create bounding box for each contour. bounding_rectangles = [cv2.minAreaRect(contour) for contour in contours] if marked_image is not None and ImageWriter.level <= ImageWriter.DEBUG: for rectangle in bounding_rectangles: # Show rectangles using bounding box. draw_rect(marked_image, rectangle, (255,255,255), thickness=1) # Remove any rectangles that couldn't be a stick part based off specified size. filtered_rectangles = filter_by_size(bounding_rectangles, geo_image.resolution, self.min_stick_part_size, self.max_stick_part_size, enforce_min_on_w_and_h=True) if ImageWriter.level <= ImageWriter.DEBUG: # Debug save intermediate images mask_filename = postfix_filename(geo_image.file_name, 'blue_thresh') ImageWriter.save_debug(mask_filename, mask) if marked_image is not None: for rectangle in filtered_rectangles: # Show rectangles using colored bounding box. purple = (50, 255, 255) draw_rect(marked_image, rectangle, purple, thickness=2) return filtered_rectangles