images = io_functions.specimen_ids_from_images(os.listdir(input_folder))

with open(os.path.join(output_folder,'colors.csv'), 'wb') as csvfile:
    writer = csv.writer(csvfile)

    num_colors = [10, 5]

    headers = ['Specimen']
    for n in num_colors:
        headers += ['Segment', 'Mean Hue', 'Mean Saturation']
        headers += [channel + str(n) for n in range(n) for channel in ['R', 'G', 'B', 'P']]

    writer.writerow(headers)

    for filename in images:
        color = io_functions.read_image(os.path.join(input_folder, 'color_' + filename))
        abdomen = io_functions.read_image(os.path.join(input_folder, 'abdomen_' + filename))[:, :, 0]
        wings = io_functions.read_image(os.path.join(input_folder, 'wings_' + filename))[:, :, 0]
        if color is not None and abdomen is not None and wings is not None:
            HSV = rgb2hsv(color)

            segments = [Segment('wings', wings, 10),
                        Segment('abdomen', abdomen, 5)]

            current_row = [os.path.splitext(filename)[0]]

            for segment in segments:
                current_row += [segment.name,
                                np.mean(HSV[:, :, 0][np.where(segment.mask > 128)]),
                                np.mean(HSV[:, :, 1][np.where(segment.mask > 128)])]
    writer = csv.writer(csvfile)

    num_colors = [10, 5]

    headers = ['Specimen']
    for n in num_colors:
        headers += ['Segment', 'Mean Hue', 'Mean Saturation']
        headers += [
            channel + str(n) for n in range(n)
            for channel in ['R', 'G', 'B', 'P']
        ]

    writer.writerow(headers)

    for filename in images:
        color = io_functions.read_image(
            os.path.join(input_folder, 'color_' + filename))
        abdomen = io_functions.read_image(
            os.path.join(input_folder, 'abdomen_' + filename))[:, :, 0]
        wings = io_functions.read_image(
            os.path.join(input_folder, 'wings_' + filename))[:, :, 0]
        if color is not None and abdomen is not None and wings is not None:
            HSV = rgb2hsv(color)

            segments = [
                Segment('wings', wings, 10),
                Segment('abdomen', abdomen, 5)
            ]

            current_row = [os.path.splitext(filename)[0]]

            for segment in segments:
    writer = csv.writer(csvfile)
    writer.writerow(['Specimen', 'Right', 'Left'])

with open('computed_wing_lengths.csv', 'rU') as csvfile:
    reader = csv.reader(csvfile)
    specimen_ids = [row[0] for row in reader]
    specimens = [
        specimen for specimen in specimens if specimen[0] not in specimen_ids
    ]

for specimen_id, filename in specimens:
    with open('computed_wing_lengths.csv', 'a') as csvfile:
        writer = csv.writer(csvfile)

        start = timeit.default_timer()
        image = io_functions.read_image(filename)

        height, width = image.shape[:2]

        segmented_image, segmented_mask = segmentation.segment_butterfly(
            image, saliency_threshold=96, approximate=False)

        wing_mask, wing_paths, centre_of_mass = segmentation.segment_wing(
            segmented_mask)

        left_wing_length = wing_length(wing_mask[:, centre_of_mass[1]::-1],
                                       wing_paths[0][:, centre_of_mass[1]::-1])
        right_wing_length = wing_length(wing_mask[:, centre_of_mass[1]:],
                                        wing_paths[1][:, centre_of_mass[1]:])
        stop = timeit.default_timer()
        print('{}: Left wing is {:.2f}px, right wing is {:.2f}px [{:.2f}s]'.
示例#4
0
with open('computed_wing_lengths.csv', 'wb') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['Specimen', 'Right', 'Left'])

with open('computed_wing_lengths.csv', 'rU') as csvfile:
    reader = csv.reader(csvfile)
    specimen_ids = [row[0] for row in reader]
    specimens = [specimen for specimen in specimens if specimen[0] not in specimen_ids]

for specimen_id, filename in specimens:
    with open('computed_wing_lengths.csv', 'a') as csvfile:
        writer = csv.writer(csvfile)

        start = timeit.default_timer()
        image = io_functions.read_image(filename)

        height, width = image.shape[:2]

        segmented_image, segmented_mask = segmentation.segment_butterfly(image,
                                                                         saliency_threshold=96,
                                                                         approximate=False)

        wing_mask, wing_paths, centre_of_mass = segmentation.segment_wing(segmented_mask)

        left_wing_length = wing_length(wing_mask[:, centre_of_mass[1]::-1], wing_paths[0][:, centre_of_mass[1]::-1])
        right_wing_length = wing_length(wing_mask[:, centre_of_mass[1]:], wing_paths[1][:, centre_of_mass[1]:])
        stop = timeit.default_timer()
        print('{}: Left wing is {:.2f}px, right wing is {:.2f}px [{:.2f}s]'.format(specimen_id, left_wing_length, right_wing_length, stop - start))
        writer.writerow([specimen_id, right_wing_length, left_wing_length])