Пример #1
0
def test_plantcv_triangle_threshold():
    img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1)
    device, thresholded = pcv.triangle_auto_threshold(0,
                                                      img1,
                                                      255,
                                                      "light",
                                                      10,
                                                      debug=None)
    thresholdedavg = np.average(thresholded)
    imgavg = np.average(img1)
    assert thresholdedavg > imgavg
Пример #2
0
def main():
    # Initialize device
    device = 0

    # Parse command-line options
    args = options()

    # Read image
    img, path, filename = pcv.readimage(filename=args.image, debug=args.debug)

    # Convert RGB to LAB and extract the Blue-Yellow channel
    device, blue_channel = pcv.rgb2gray_lab(img=img,
                                            channel="b",
                                            device=device,
                                            debug=args.debug)

    # Threshold the blue image using the triangle autothreshold method
    device, blue_tri = pcv.triangle_auto_threshold(device=device,
                                                   img=blue_channel,
                                                   maxvalue=255,
                                                   object_type="light",
                                                   xstep=1,
                                                   debug=args.debug)

    # Extract core plant region from the image to preserve delicate plant features during filtering
    device += 1
    plant_region = blue_tri[0:1750, 600:2080]
    if args.debug is not None:
        pcv.print_image(filename=str(device) + "_extract_plant_region.png",
                        img=plant_region)

    # Use a Gaussian blur to disrupt the strong edge features in the cabinet
    device, blur_gaussian = pcv.gaussian_blur(device=device,
                                              img=blue_tri,
                                              ksize=(3, 3),
                                              sigmax=0,
                                              sigmay=None,
                                              debug=args.debug)

    # Threshold the blurred image to remove features that were blurred
    device, blur_thresholded = pcv.binary_threshold(img=blur_gaussian,
                                                    threshold=250,
                                                    maxValue=255,
                                                    object_type="light",
                                                    device=device,
                                                    debug=args.debug)

    # Add the plant region back in to the filtered image
    device += 1
    blur_thresholded[0:1750, 600:2080] = plant_region
    if args.debug is not None:
        pcv.print_image(filename=str(device) + "_replace_plant_region.png",
                        img=blur_thresholded)

    # Fill small noise
    device, blue_fill_50 = pcv.fill(img=np.copy(blur_thresholded),
                                    mask=np.copy(blur_thresholded),
                                    size=50,
                                    device=device,
                                    debug=args.debug)

    # Apply a small median blur to break up pot edges
    device, med_blur = pcv.median_blur(img=np.copy(blue_fill_50),
                                       ksize=3,
                                       device=device,
                                       debug=args.debug)

    # Define an ROI for the barcode label
    device, label_roi, label_hierarchy = pcv.define_roi(img=img,
                                                        shape="rectangle",
                                                        device=device,
                                                        roi=None,
                                                        roi_input="default",
                                                        debug=args.debug,
                                                        adjust=True,
                                                        x_adj=1100,
                                                        y_adj=1350,
                                                        w_adj=-1070,
                                                        h_adj=-590)

    # Identify all remaining contours in the binary image
    device, contours, hierarchy = pcv.find_objects(img=img,
                                                   mask=np.copy(med_blur),
                                                   device=device,
                                                   debug=args.debug)

    # Remove contours completely contained within the label region of interest
    device, remove_label_mask = remove_countors_roi(mask=med_blur,
                                                    contours=contours,
                                                    hierarchy=hierarchy,
                                                    roi=label_roi,
                                                    device=device,
                                                    debug=args.debug)

    # Identify objects
    device, contours, contour_hierarchy = pcv.find_objects(
        img=img, mask=remove_label_mask, device=device, debug=args.debug)

    # Define ROI
    device, roi, roi_hierarchy = pcv.define_roi(img=img,
                                                shape="rectangle",
                                                device=device,
                                                roi=None,
                                                roi_input="default",
                                                debug=args.debug,
                                                adjust=True,
                                                x_adj=565,
                                                y_adj=0,
                                                w_adj=-490,
                                                h_adj=-600)

    # Decide which objects to keep
    device, roi_contours, roi_contour_hierarchy, _, _ = pcv.roi_objects(
        img=img,
        roi_type="partial",
        roi_contour=roi,
        roi_hierarchy=roi_hierarchy,
        object_contour=contours,
        obj_hierarchy=contour_hierarchy,
        device=device,
        debug=args.debug)

    # If there are no contours left we cannot measure anything
    if len(roi_contours) > 0:
        # Object combine kept objects
        device, plant_contour, plant_mask = pcv.object_composition(
            img=img,
            contours=roi_contours,
            hierarchy=roi_contour_hierarchy,
            device=device,
            debug=args.debug)

        outfile = False
        if args.writeimg:
            outfile = args.outdir + "/" + filename

        # Find shape properties, output shape image (optional)
        device, shape_header, shape_data, shape_img = pcv.analyze_object(
            img=img,
            imgname=args.image,
            obj=plant_contour,
            mask=plant_mask,
            device=device,
            debug=args.debug,
            filename=outfile)

        # Shape properties relative to user boundary line (optional)
        device, boundary_header, boundary_data, boundary_img = pcv.analyze_bound(
            img=img,
            imgname=args.image,
            obj=plant_contour,
            mask=plant_mask,
            line_position=690,
            device=device,
            debug=args.debug,
            filename=outfile)

        # Determine color properties: Histograms, Color Slices and Pseudocolored Images,
        # output color analyzed images (optional)
        device, color_header, color_data, color_img = pcv.analyze_color(
            img=img,
            imgname=args.image,
            mask=plant_mask,
            bins=256,
            device=device,
            debug=args.debug,
            hist_plot_type=None,
            pseudo_channel="v",
            pseudo_bkg="img",
            resolution=300,
            filename=outfile)

        # Output shape and color data
        result = open(args.result, "a")
        result.write('\t'.join(map(str, shape_header)) + "\n")
        result.write('\t'.join(map(str, shape_data)) + "\n")
        for row in shape_img:
            result.write('\t'.join(map(str, row)) + "\n")
        result.write('\t'.join(map(str, color_header)) + "\n")
        result.write('\t'.join(map(str, color_data)) + "\n")
        result.write('\t'.join(map(str, boundary_header)) + "\n")
        result.write('\t'.join(map(str, boundary_data)) + "\n")
        result.write('\t'.join(map(str, boundary_img)) + "\n")
        for row in color_img:
            result.write('\t'.join(map(str, row)) + "\n")
        result.close()
Пример #3
0
def main():
    # Initialize device
    device = 0

    # Parse command-line options
    args = options()

    # Read image
    img, path, filename = pcv.readimage(filename=args.image, debug=args.debug)

    # Convert RGB to LAB and extract the Blue-Yellow channel
    device, blue_channel = pcv.rgb2gray_lab(img=img,
                                            channel="b",
                                            device=device,
                                            debug=args.debug)

    # Threshold the blue image using the triangle autothreshold method
    device, blue_tri = pcv.triangle_auto_threshold(device=device,
                                                   img=blue_channel,
                                                   maxvalue=255,
                                                   object_type="light",
                                                   xstep=1,
                                                   debug=args.debug)

    # Extract core plant region from the image to preserve delicate plant features during filtering
    device += 1
    plant_region = blue_tri[0:1750, 600:2080]
    if args.debug is not None:
        pcv.print_image(filename=str(device) + "_extract_plant_region.png",
                        img=plant_region)

    # Use a Gaussian blur to disrupt the strong edge features in the cabinet
    device, blur_gaussian = pcv.gaussian_blur(device=device,
                                              img=blue_tri,
                                              ksize=(3, 3),
                                              sigmax=0,
                                              sigmay=None,
                                              debug=args.debug)

    # Threshold the blurred image to remove features that were blurred
    device, blur_thresholded = pcv.binary_threshold(img=blur_gaussian,
                                                    threshold=250,
                                                    maxValue=255,
                                                    object_type="light",
                                                    device=device,
                                                    debug=args.debug)

    # Add the plant region back in to the filtered image
    device += 1
    blur_thresholded[0:1750, 600:2080] = plant_region
    if args.debug is not None:
        pcv.print_image(filename=str(device) + "_replace_plant_region.png",
                        img=blur_thresholded)

    # Fill small noise
    device, blue_fill_50 = pcv.fill(img=np.copy(blur_thresholded),
                                    mask=np.copy(blur_thresholded),
                                    size=50,
                                    device=device,
                                    debug=args.debug)

    # Identify objects
    device, contours, contour_hierarchy = pcv.find_objects(img=img,
                                                           mask=blue_fill_50,
                                                           device=device,
                                                           debug=args.debug)

    # Define ROI
    device, roi, roi_hierarchy = pcv.define_roi(img=img,
                                                shape="rectangle",
                                                device=device,
                                                roi=None,
                                                roi_input="default",
                                                debug=args.debug,
                                                adjust=True,
                                                x_adj=565,
                                                y_adj=0,
                                                w_adj=-490,
                                                h_adj=-250)

    # Decide which objects to keep
    device, roi_contours, roi_contour_hierarchy, _, _ = pcv.roi_objects(
        img=img,
        roi_type="partial",
        roi_contour=roi,
        roi_hierarchy=roi_hierarchy,
        object_contour=contours,
        obj_hierarchy=contour_hierarchy,
        device=device,
        debug=args.debug)

    # If there are no contours left we cannot measure anything
    if len(roi_contours) > 0:
        # Object combine kept objects
        device, plant_contour, plant_mask = pcv.object_composition(
            img=img,
            contours=roi_contours,
            hierarchy=roi_contour_hierarchy,
            device=device,
            debug=args.debug)

        outfile = False
        if args.writeimg:
            outfile = args.outdir + "/" + filename

        # Find shape properties, output shape image (optional)
        device, shape_header, shape_data, shape_img = pcv.analyze_object(
            img=img,
            imgname=args.image,
            obj=plant_contour,
            mask=plant_mask,
            device=device,
            debug=args.debug,
            filename=outfile)

        # Shape properties relative to user boundary line (optional)
        device, boundary_header, boundary_data, boundary_img = pcv.analyze_bound(
            img=img,
            imgname=args.image,
            obj=plant_contour,
            mask=plant_mask,
            line_position=440,
            device=device,
            debug=args.debug,
            filename=outfile)

        # Determine color properties: Histograms, Color Slices and Pseudocolored Images,
        # output color analyzed images (optional)
        device, color_header, color_data, color_img = pcv.analyze_color(
            img=img,
            imgname=args.image,
            mask=plant_mask,
            bins=256,
            device=device,
            debug=args.debug,
            hist_plot_type=None,
            pseudo_channel="v",
            pseudo_bkg="img",
            resolution=300,
            filename=outfile)

        # Output shape and color data
        result = open(args.result, "a")
        result.write('\t'.join(map(str, shape_header)) + "\n")
        result.write('\t'.join(map(str, shape_data)) + "\n")
        for row in shape_img:
            result.write('\t'.join(map(str, row)) + "\n")
        result.write('\t'.join(map(str, color_header)) + "\n")
        result.write('\t'.join(map(str, color_data)) + "\n")
        result.write('\t'.join(map(str, boundary_header)) + "\n")
        result.write('\t'.join(map(str, boundary_data)) + "\n")
        result.write('\t'.join(map(str, boundary_img)) + "\n")
        for row in color_img:
            result.write('\t'.join(map(str, row)) + "\n")
        result.close()

        # Find matching NIR image
        device, nirpath = pcv.get_nir(path=path,
                                      filename=filename,
                                      device=device,
                                      debug=args.debug)
        nir_rgb, nir_path, nir_filename = pcv.readimage(nirpath)
        nir_img = cv2.imread(nirpath, 0)

        # Make mask glovelike in proportions via dilation
        device, d_mask = pcv.dilate(plant_mask,
                                    kernel=1,
                                    i=0,
                                    device=device,
                                    debug=args.debug)

        # Resize mask
        prop2, prop1 = conv_ratio()
        device, nmask = pcv.resize(img=d_mask,
                                   resize_x=prop1,
                                   resize_y=prop2,
                                   device=device,
                                   debug=args.debug)

        # Convert the resized mask to a binary mask
        device, bmask = pcv.binary_threshold(img=nmask,
                                             threshold=0,
                                             maxValue=255,
                                             object_type="light",
                                             device=device,
                                             debug=args.debug)

        device, crop_img = crop_sides_equally(mask=bmask,
                                              nir=nir_img,
                                              device=device,
                                              debug=args.debug)

        # position, and crop mask
        device, newmask = pcv.crop_position_mask(img=nir_img,
                                                 mask=crop_img,
                                                 device=device,
                                                 x=34,
                                                 y=9,
                                                 v_pos="top",
                                                 h_pos="right",
                                                 debug=args.debug)

        # Identify objects
        device, nir_objects, nir_hierarchy = pcv.find_objects(img=nir_rgb,
                                                              mask=newmask,
                                                              device=device,
                                                              debug=args.debug)

        # Object combine kept objects
        device, nir_combined, nir_combinedmask = pcv.object_composition(
            img=nir_rgb,
            contours=nir_objects,
            hierarchy=nir_hierarchy,
            device=device,
            debug=args.debug)

        if args.writeimg:
            outfile = args.outdir + "/" + nir_filename

        # Analyze NIR signal data
        device, nhist_header, nhist_data, nir_imgs = pcv.analyze_NIR_intensity(
            img=nir_img,
            rgbimg=nir_rgb,
            mask=nir_combinedmask,
            bins=256,
            device=device,
            histplot=False,
            debug=args.debug,
            filename=outfile)

        # Analyze the shape of the plant contour from the NIR image
        device, nshape_header, nshape_data, nir_shape = pcv.analyze_object(
            img=nir_img,
            imgname=nir_filename,
            obj=nir_combined,
            mask=nir_combinedmask,
            device=device,
            debug=args.debug,
            filename=outfile)

        # Write NIR data to co-results file
        coresult = open(args.coresult, "a")
        coresult.write('\t'.join(map(str, nhist_header)) + "\n")
        coresult.write('\t'.join(map(str, nhist_data)) + "\n")
        for row in nir_imgs:
            coresult.write('\t'.join(map(str, row)) + "\n")
        coresult.write('\t'.join(map(str, nshape_header)) + "\n")
        coresult.write('\t'.join(map(str, nshape_data)) + "\n")
        coresult.write('\t'.join(map(str, nir_shape)) + "\n")
        coresult.close()
Пример #4
0
    # Pipeline step
    device = 0

    # White balance
    device, corr_img = pcv.white_balance(device, img, debug, [1450, 400, 5, 5])
	# Modify the 4 numbers in square brackets to draw a square that rests on a white color card, or white color marker.

    # Convert RBG to Grey using Blue-Yellow Channel 
	device, b = pcv.rgb2gray_lab(corr_img, 'b', device, debug)
	# Channel 'b' should make the plant brighter over background. Otherwise, use a different channel. 

    # Gaussian blur
	device, g_blur = pcv.gaussian_blur(device, b, (5,5), 0, None, debug)

    # Threshold the blue image
    device, img_thresh = pcv.triangle_auto_threshold(device, g_blur, 255, 'light', 20, args.debug)
    device, img_cnt = pcv.triangle_auto_threshold(device, g_blur, 255, 'light', 20, args.debug)
	# Modify the number 20 in both lines to an appropriate threshold that best separates plant from background

    # Fill small objects
    device, img_fill = pcv.fill(img_thresh, img_cnt, 50, device, args.debug)
	# Modify the number 50 to what best fills holes in the plant (white) and background (black)

    # Mask white-balanced image (here, based on blue channel)
	device, masked = pcv.apply_mask(corr_img, b_fill, 'white', device, debug)

    # Identify objects
    device, id_objects, obj_hierarchy = pcv.find_objects(masked, img_fill, device, args.debug)

    # Define region of interest
	device, roi1, roi_hierarchy = pcv.define_roi(masked, 'rectangle', device, None, 'default', debug, True, 
Пример #5
0
def test_plantcv_triangle_threshold():
    img1 = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1)
    device, thresholded = pcv.triangle_auto_threshold(0, img1, 255, "light", 10, debug=None)
    thresholdedavg = np.average(thresholded)
    imgavg = np.average(img1)
    assert thresholdedavg > imgavg