示例#1
0
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
示例#2
0
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