Пример #1
0
def test_with_ax(fake_points_of_interest):
    fig, ax = plt.subplots(figsize=(20, 5))
    points_interest, true_results_pix, true_results_mm = fake_points_of_interest
    axes = [ax] + [None] * 6
    dist_pix, dist_mm = measurement.main(points_interest, 2)
    assert dist_pix == true_results_pix
    assert dist_mm == true_results_mm
Пример #2
0
def test_t_space_unchanged():
    # tests with simple t_space = 1 that everything remains the same.
    pixel_points = [(0, 0), (5, 0), (0, 0), (0, 4)]
    mm_points = measurement.main(pixel_points, 1)
    assert mm_points == ((5, 4), (5, 4))
Пример #3
0
def test_with_ax():
    fig, ax = plt.subplots(figsize=(20, 5))
    pixel_points = [(0, 0), (5, 0), (0, 0), (0, 4)]
    mm_points = measurement.main(pixel_points, 2, ax)
    assert mm_points == ((5, 4), (2.5, 2))
Пример #4
0
def test_different_t_space():
    pixel_points = [(0, 0), (5, 0), (0, 0), (0, 4)]
    mm_points = measurement.main(pixel_points, 2)
    assert mm_points == ((5, 4), (2.5, 2))
Пример #5
0
def main():
    # Assign description to the help doc
    parser = argparse.ArgumentParser(
        description='Script to automate butterfly wings measurment')
    # Add arguments
    # Plotting
    parser.add_argument('-p',
                        '--plot',
                        action='store_true',
                        help='If entered images are plotted to the output\
                        folder')

    parser.add_argument('-pp',
                        '--detailed_plot',
                        action='store_true',
                        help='If entered detailed images are plotted to the\
                        output folder')

    # Input path
    parser.add_argument('-i',
                        '--input',
                        type=str,
                        help='Input path for folder or single image',
                        required=False,
                        default='raw_images')

    # Output path
    parser.add_argument('-o',
                        '--output_folder',
                        type=str,
                        help='Output path for raw image',
                        required=False,
                        default='outputs')
    # Stage
    parser.add_argument('-s',
                        '--stage',
                        type=str,
                        help="Stage name: 'ruler_detection', 'binarization',\
                        'tracing', 'measurements",
                        required=False,
                        default='measurements')
    # Dots per inch
    parser.add_argument('-dpi',
                        type=int,
                        help='Dots per inch of the saved figures',
                        default=300)

    # CSV output path
    parser.add_argument('-csv',
                        '--path_csv',
                        type=str,
                        help='Path of the resulting csv file',
                        default='results.csv')

    # Grabcut
    parser.add_argument('-g',
                        '--grabcut',
                        action='store_true',
                        help='Use grabcut in binarization step')

    args = parser.parse_args()

    # Initialization
    if os.path.exists(args.output_folder):
        oldList = os.listdir(args.output_folder)
        for oldFile in oldList:
            os.remove(args.output_folder + "/" + oldFile)
    else:
        os.mkdir(args.output_folder)

    stages = ['ruler_detection', 'binarization', 'measurements']

    if args.stage not in stages:
        print("ERROR : Stage can only be 'ruler_detection', 'binarization',\
               or 'measurements'")
        return 0

    plot_level = 0
    if args.plot:
        plot_level = 1
    if args.detailed_plot:
        plot_level = 2

    # Initializing the csv file
    if args.stage == 'measurements':
        if os.path.exists(args.path_csv):
            os.remove(args.path_csv)
        with open(args.path_csv, 'w') as csv_file:
            writer = csv.writer(csv_file)
            writer.writerow([
                'image_id', 'left_wing (mm)', 'right_wing (mm)',
                'left_wing_center (mm)', 'right_wing_center (mm)',
                'wing_span (mm)'
            ])

    stage_idx = stages.index(args.stage)
    pipeline_process = stages[:stage_idx + 1]

    raw_image_path = args.input
    if (os.path.isdir(raw_image_path)):
        image_names = os.listdir(raw_image_path)
        image_paths = []
        for image_name in image_names:
            if not image_name.lower().endswith(('.png', '.jpg', '.jpeg')):
                continue
            image_path = os.path.join(raw_image_path, image_name)
            image_paths.append(image_path)
    else:
        image_paths = [raw_image_path]
    n = len(image_paths)

    for i, image_path in enumerate(image_paths):
        image_name = os.path.basename(image_path)
        print(f'Image {i+1}/{n} : {image_name}')

        image_rgb = imread(image_path, plugin='matplotlib')
        axes = create_layout(len(pipeline_process), plot_level)

        for step in pipeline_process:
            if step == 'ruler_detection':
                T_space, top_ruler = ruler_detection.main(image_rgb, axes)

            elif step == 'binarization':
                binary = binarization.main(image_rgb, top_ruler, args.grabcut,
                                           axes)

            elif step == 'measurements':
                points_interest = tracing.main(binary, axes)
                dist_pix, dist_mm = measurement.main(points_interest, T_space,
                                                     axes)

                with open(args.path_csv, 'a') as csv_file:
                    writer = csv.writer(csv_file)
                    writer.writerow([
                        image_name, dist_mm["dist_l"], dist_mm["dist_r"],
                        dist_mm["dist_l_center"], dist_mm["dist_r_center"],
                        dist_mm["dist_span"]
                    ])

        if plot_level > 0:
            output_path = os.path.normpath(
                os.path.join(args.output_folder, image_name))
            dpi = args.dpi
            if plot_level == 2:
                dpi = int(1.5 * args.dpi)
            plt.savefig(output_path, dpi=dpi)
            plt.close()
Пример #6
0
def test_different_t_space(fake_points_of_interest):
    points_interest, true_results_pix, true_results_mm = fake_points_of_interest
    dist_pix, dist_mm = measurement.main(points_interest, 2)
    assert dist_pix == true_results_pix
    assert dist_mm == true_results_mm
Пример #7
0
def test_t_space_unchanged(fake_points_of_interest):
    # tests with simple t_space = 1 that everything remains the same.
    points_interest, true_results_pix, true_results_mm = fake_points_of_interest
    dist_pix, dist_mm = measurement.main(points_interest, 1)
    assert dist_pix == true_results_pix
    assert dist_mm == true_results_pix
Пример #8
0
def test_t_space_unchanged():
    # tests with simple t_space = 1 that everything remains the same.
    pixel_points = [(0, 0), (5, 0), (0, 0), (0, 4)]
    dst_pix, dst_mm = measurement.main(pixel_points, 1, AXES)
    assert (dst_pix, dst_mm) == ((5, 4), (5, 4))
Пример #9
0
def test_with_ax():
    fig, ax = plt.subplots(figsize=(20, 5))
    pixel_points = [(0, 0), (5, 0), (0, 0), (0, 4)]
    axes = [ax] + [None] * 6
    dst_pix, dst_mm = measurement.main(pixel_points, 2, axes)
    assert (dst_pix, dst_mm) == ((5, 4), (2.5, 2))
Пример #10
0
def test_different_t_space():
    pixel_points = [(0, 0), (5, 0), (0, 0), (0, 4)]
    dst_pix, dst_mm = measurement.main(pixel_points, 2, AXES)
    assert (dst_pix, dst_mm) == ((5, 4), (2.5, 2))
Пример #11
0
def main():
    # Assign description to the help doc
    parser = argparse.ArgumentParser(
        description='Script to automate butterfly wings measurment')
    # Add arguments
    # Plotting
    parser.add_argument('-p',
                        '--plot',
                        action='store_true',
                        help='If entered images are plotted to the output\
                        folder')

    # Input path
    parser.add_argument('-i',
                        '--input',
                        type=str,
                        help='Input path for folder or single image',
                        required=False,
                        default='raw_images')

    # Output path
    parser.add_argument('-o',
                        '--output_folder',
                        type=str,
                        help='Output path for raw image',
                        required=False,
                        default='outputs')
    # Stage
    parser.add_argument('-s',
                        '--stage',
                        type=str,
                        help="Stage name: 'ruler_detection', 'binarization',\
                        'tracing', 'measurements",
                        required=True)
    # Dots per inch
    parser.add_argument('-dpi',
                        type=int,
                        help='Dots per inch of the saved figures',
                        default=300)
    parser.add_argument('-csv',
                        '--path_csv',
                        type=str,
                        help='Path of the resulting csv file',
                        default='results.csv')

    args = parser.parse_args()

    # Initialization
    if os.path.exists(args.output_folder):
        oldList = os.listdir(args.output_folder)
        for oldFile in oldList:
            os.remove(args.output_folder + "/" + oldFile)
    else:
        os.mkdir(args.output_folder)

    stages = ['ruler_detection', 'binarization', 'tracing', 'measurements']

    if args.stage not in stages:
        print("ERROR : Stage can only be 'ruler_detection', 'binarization',\
              'tracing' or 'measurements'")
        return 0

    # Initializing the csv file
    if args.stage == 'measurements':
        if os.path.exists(args.path_csv):
            os.remove(args.path_csv)
        with open(args.path_csv, 'w') as csv_file:
            writer = csv.writer(csv_file)
            writer.writerow(['image_id', 'left_wing (mm)', 'right_wing (mm)'])

    raw_image_path = args.input

    stage_idx = stages.index(args.stage)
    pipeline_process = stages[:stage_idx + 1]

    raw_image_path = args.input
    if (os.path.isdir(raw_image_path)):
        image_names = os.listdir(raw_image_path)
    else:
        image_names = [""]

    # For testing purpose, the pipeline is only applied to the first 10 images
    for image_name in image_names[:10]:
        print(raw_image_path + '/' + image_name)
        image_path = os.path.normpath(raw_image_path + '/' + image_name)
        image_rgb = imread(image_path)
        ax = [None, None, None]
        if args.plot:
            ncols = min(len(pipeline_process), 3)
            fig, ax = plt.subplots(ncols=ncols, figsize=(20, 5))

        for step in pipeline_process:
            if step == 'ruler_detection':
                ax0 = ax
                if len(pipeline_process) > 1:
                    ax0 = ax[0]
                T_space, top_ruler = ruler_detection.main(image_rgb, ax0)
            elif step == 'binarization':
                binary = binarization.main(image_rgb, top_ruler, ax[1])
            elif step == 'tracing':
                points_interest = tracing.main(binary, ax[2])
            else:
                dst_pix, dst_mm = measurement.main(points_interest, T_space,
                                                   ax[0])
                with open(args.path_csv, 'a') as csv_file:
                    writer = csv.writer(csv_file)
                    writer.writerow([image_name, dst_mm[0], dst_mm[1]])

        if args.plot:
            filename = args.output_folder + '/' + image_name
            output_path = os.path.normpath(filename)
            plt.savefig(output_path, dpi=args.dpi)
            plt.close()