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 extract_items(field_items, geo_image, image, marked_image, filter_edge_items=True): '''Extract items into separate images. Return updated list of field items.''' if filter_edge_items: # Filter out any items that touch the image border since it likely doesn't represent entire item. items_without_border_elements = [] for item in field_items: if touches_image_border(item, geo_image): # Mark as special color to show user why it wasn't included. if marked_image is not None: dark_orange = (0, 140, 255) # dark orange draw_rect(marked_image, item.bounding_rect, dark_orange, thickness=2) else: items_without_border_elements.append(item) field_items = items_without_border_elements # Extract field items into separate image for item in field_items: extracted_image = extract_square_image(image, item.bounding_rect, 20) extracted_image_fname = postfix_filename(geo_image.file_name, "_{}_{}".format(item.type, item.name)) extracted_image_path = ImageWriter.save_normal(extracted_image_fname, extracted_image) item.image_path = extracted_image_path item.parent_image_filename = geo_image.file_name item.position = calculate_item_position(item, geo_image) item.zone = geo_image.zone return field_items
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
def extract_global_plants_from_images(plants, geo_images, out_directory): for plant in plants: global_bounding_rect = plant.bounding_rect if global_bounding_rect is None: continue found_in_image = False for k, geo_image in enumerate(geo_images): from src.util.clustering import rect_to_image image_rect = rect_to_image(global_bounding_rect, geo_image) x, y = image_rect[0] if x > 0 and x < geo_image.width and y > 0 and y < geo_image.height: found_in_image = True if not plant.parent_image_filename.strip(): plant.bounding_rect = image_rect plant.parent_image_filename = geo_image.file_name else: plant_copy = plant.__class__('plant', position=plant.position, zone=plant.zone) plant_copy.bounding_rect = image_rect plant_copy.parent_image_filename = geo_image.file_name plant.add_other_item(plant_copy) if out_directory is None: continue image_out_directory = os.path.join(out_directory, os.path.splitext(geo_image.file_name)[0]) ImageWriter.output_directory = image_out_directory img = cv2.imread(geo_image.file_path, cv2.CV_LOAD_IMAGE_COLOR) if img is not None: plant_img = extract_square_image(img, image_rect, 200) plant_image_fname = postfix_filename(geo_image.file_name, "_{}".format(plant.type)) plant.image_path = ImageWriter.save_normal(plant_image_fname, plant_img) if not found_in_image: # Can't convert global rect to a rotated image rect so remove it to be consistent. plant.bounding_rect = None