Esempio n. 1
0
def average_all_img(directory, outdir):
    allfiles = os.listdir(directory)
    
    path = str(directory)
    
    allpaths = []
    
    for files in allfiles:
        p = path + str(files)
        allpaths.append(p)
    
    img, path, filename = pcv.readimage(allpaths[0])
    n = len(allpaths)

    if len(np.shape(img)) == 3:
        ix, iy, iz = np.shape(img)
        arr = np.zeros((ix, iy, iz), np.float)
    else:
        ix, iy = np.shape(img)
        arr = np.zeros((ix, iy), np.float)

    # Build up average pixel intensities, casting each image as an array of floats
    for i, paths in enumerate(allpaths):
        img, path, filename = pcv.readimage(allpaths[i])
        imarr = np.array(img, dtype=np.float)
        arr = arr + imarr / n

    # Round values in array and cast as 8-bit integer
    arr = np.array(np.round(arr), dtype=np.uint8)

    pcv.print_image(arr, (str(outdir)+"average_"+str(allfiles[0])))
Esempio n. 2
0
def average_all_img(directory,outdir):
    allfiles=os.listdir(directory)
    
    path=str(directory)
    
    allpaths=[]
    
    for files in allfiles:
        p=path+str(files)
        allpaths.append(p)
    
    img, path, filename = pcv.readimage(allpaths[0])
    n=len(allpaths)

    
    if len(np.shape(img))==3:
        ix,iy,iz=np.shape(img)
        arr=np.zeros((ix,iy,iz),np.float)
    else:
        ix,iy=np.shape(img)
        arr=np.zeros((ix,iy,iz),np.float)

    # Build up average pixel intensities, casting each image as an array of floats
    for i,paths in enumerate(allpaths):
        img,path,filename=pcv.readimage(allpaths[i])
        imarr=np.array(img,dtype=np.float)
        arr=arr+imarr/n

    #Round values in array and cast as 8-bit integer
    arr=np.array(np.round(arr),dtype=np.uint8)

    pcv.print_image(arr, (str(outdir)+"average_"+str(allfiles[0])))
Esempio n. 3
0
def test_plantcv_print_image():
    img, path, img_name = pcv.readimage(
        filename=os.path.join(TEST_DATA, TEST_INPUT_COLOR))
    filename = os.path.join(TEST_TMPDIR, 'plantcv_print_image.jpg')
    pcv.print_image(img=img, filename=filename)
    # Assert that the file was created
    assert os.path.exists(filename) is True
Esempio n. 4
0
def platCV(imagePath):
    img, path, filename = pcv.readimage(imagePath)
    # Pipeline step
    device = 0
    debug = 'print'

    # Convert RGB to HSV and extract the Saturation channel
    device, s = pcv.rgb2gray_hsv(img, 's', device, debug)
    # Threshold the Saturation image
    device, s_thresh = pcv.binary_threshold(s, 85, 255, 'light', device, debug)
    # Median Filter
    device, s_mblur = pcv.median_blur(s_thresh, 5, device, debug)
    device, s_cnt = pcv.median_blur(s_thresh, 5, device, debug)
    # Convert RGB to LAB and extract the Blue channel
    device, b = pcv.rgb2gray_lab(img, 'b', device, debug)
    # Threshold the blue image
    device, b_thresh = pcv.binary_threshold(b, 130, 255, 'light', device,
                                            debug)
    device, b_cnt = pcv.binary_threshold(b, 130, 255, 'light', device, debug)
    # Fill small objects
    device, b_fill = pcv.fill(b_thresh, b_cnt, 10, device, debug)
    # Join the thresholded saturation and blue-yellow images
    #device, bs = pcv.logical_or(s_mblur, b_cnt, device, debug)
    # Apply Mask (for vis images, mask_color=white)
    device, masked = pcv.apply_mask(img, b_fill, 'white', device, debug)
def convert_json(filename, imagefilename):
    rawjson = load_json(filename)
    outputdict = {}
    rgbdict = {}
    device = 0

    # Ignoring the first key of the JSON which will be the filename + filesize
    key = list(rawjson.keys())[0]
    tempdict = rawjson[key]
    regions = tempdict['regions']

    # Iterating through each key-value pair in the regions dictionary, finding the names of the regions the user has used, finding the coords associated with it
    # and adding it to the dictionary
    for id, values in regions.iteritems():
        region_attributes = values['region_attributes']
        identifier = list(region_attributes.keys())[0]

        name = values['region_attributes'][identifier]

        x_points = values['shape_attributes']['all_points_x']
        y_points = values['shape_attributes']['all_points_y']

        coords = list(zip(x_points, y_points))

        outputdict[name] = coords

    for z in outputdict:
        rgblist = []
        pts = np.array(outputdict[z])

        img, path, filename = pcv.readimage(imagefilename)

        #Creating mask arrays of 0's
        mask = np.zeros((img.shape[0], img.shape[1]))

        #Filling the polygon
        cv2.fillConvexPoly(mask, pts, 1)
        mask = mask.astype(np.bool)

        #Creating the output
        out = np.zeros_like(img)
        out[mask] = img[mask]

        #Turning the black background into a white background
        out[np.where((out == [0, 0, 0]).all(axis=2))] = [255, 255, 255]

        image = Image.fromarray(out.astype('uint8'), 'RGB')
        width, height = image.size

        for x in range(width):
            for y in range(height):
                r, g, b = image.getpixel((x, y))
                if not (r == g == b == 255):
                    temprgb = str(r) + ", " + str(g) + ", " + str(b)
                    rgblist.append(temprgb)

        rgbdict[z] = rgblist

    return output_tsv(rgbdict)
Esempio n. 6
0
def main():
  # Get options
  args = options()
  
  # Read image (converting fmax and track to 8 bit just to create a mask, use 16-bit for all the math)
  mask, path, filename = pcv.readimage(args.fmax)
  #mask = cv2.imread(args.fmax)
  track = cv2.imread(args.track)
  
  mask1, mask2, mask3= cv2.split(mask)
  
  # Pipeline step
  device = 0
  
  # Mask pesky track autofluor
  device, track1= pcv.rgb2gray_hsv(track, 'v', device, args.debug)
  device, track_thresh = pcv.binary_threshold(track1, 0, 255, 'light', device, args.debug)
  device, track_inv=pcv.invert(track_thresh, device, args.debug)
  device, track_masked = pcv.apply_mask(mask1, track_inv, 'black', device, args.debug)
  
  # Threshold the Saturation image
  device, fmax_thresh = pcv.binary_threshold(track_masked, 20, 255, 'light', device, args.debug)
  
  # Median Filter
  device, s_mblur = pcv.median_blur(fmax_thresh, 0, device, args.debug)
  device, s_cnt = pcv.median_blur(fmax_thresh, 0, device, args.debug)
  
  # Fill small objects
  device, s_fill = pcv.fill(s_mblur, s_cnt, 5, device, args.debug)
  device, sfill_cnt = pcv.fill(s_mblur, s_cnt, 5, device, args.debug)
  
  # Identify objects
  device, id_objects,obj_hierarchy = pcv.find_objects(mask, sfill_cnt, device, args.debug)
  
  # Define ROI
  device, roi1, roi_hierarchy= pcv.define_roi(mask,'circle', device, None, 'default', args.debug,True, 0,0,-100,-100)
  
  # Decide which objects to keep
  device,roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(mask,'partial',roi1,roi_hierarchy,id_objects,obj_hierarchy,device, args.debug)
  
  # Object combine kept objects
  device, obj, masked = pcv.object_composition(mask, roi_objects, hierarchy3, device, args.debug)
  
################ Analysis ################  
  
  # Find shape properties, output shape image (optional)
  device, shape_header,shape_data,shape_img = pcv.analyze_object(mask, args.fmax, obj, masked, device,args.debug, args.outdir+'/'+filename)
  
  # Fluorescence Measurement (read in 16-bit images)
  fdark=cv2.imread(args.fdark, -1)
  fmin=cv2.imread(args.fmin, -1)
  fmax=cv2.imread(args.fmax, -1)
  
  device, fvfm_header, fvfm_data=pcv.fluor_fvfm(fdark,fmin,fmax,kept_mask, device, args.outdir+'/'+filename, 1000, args.debug)

  # Output shape and color data
  pcv.print_results(args.fmax, shape_header, shape_data)
  pcv.print_results(args.fmax, fvfm_header, fvfm_data)
Esempio n. 7
0
def main():
  # Get options
  args = options()
  
  # Read image (converting fmax and track to 8 bit just to create a mask, use 16-bit for all the math)
  mask, path, filename = pcv.readimage(args.fmax)
  #mask = cv2.imread(args.fmax)
  track = cv2.imread(args.track)
  
  mask1, mask2, mask3= cv2.split(mask)
  
  # Pipeline step
  device = 0
  
  # Mask pesky track autofluor
  device, track1= pcv.rgb2gray_hsv(track, 'v', device, args.debug)
  device, track_thresh = pcv.binary_threshold(track1, 0, 255, 'light', device, args.debug)
  device, track_inv=pcv.invert(track_thresh, device, args.debug)
  device, track_masked = pcv.apply_mask(mask1, track_inv, 'black', device, args.debug)
  
  # Threshold the Saturation image
  device, fmax_thresh = pcv.binary_threshold(track_masked, 20, 255, 'light', device, args.debug)
  
  # Median Filter
  device, s_mblur = pcv.median_blur(fmax_thresh, 0, device, args.debug)
  device, s_cnt = pcv.median_blur(fmax_thresh, 0, device, args.debug)
  
  # Fill small objects
  device, s_fill = pcv.fill(s_mblur, s_cnt, 5, device, args.debug)
  device, sfill_cnt = pcv.fill(s_mblur, s_cnt, 5, device, args.debug)
  
  # Identify objects
  device, id_objects,obj_hierarchy = pcv.find_objects(mask, sfill_cnt, device, args.debug)
  
  # Define ROI
  device, roi1, roi_hierarchy= pcv.define_roi(mask,'circle', device, None, 'default', args.debug,True, 0,0,-100,-100)
  
  # Decide which objects to keep
  device,roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(mask,'partial',roi1,roi_hierarchy,id_objects,obj_hierarchy,device, args.debug)
  
  # Object combine kept objects
  device, obj, masked = pcv.object_composition(mask, roi_objects, hierarchy3, device, args.debug)
  
################ Analysis ################  
  
  # Find shape properties, output shape image (optional)
  device, shape_header,shape_data,shape_img = pcv.analyze_object(mask, args.fmax, obj, masked, device,args.debug, args.outdir+'/'+filename)
  
  # Fluorescence Measurement (read in 16-bit images)
  fdark=cv2.imread(args.fdark, -1)
  fmin=cv2.imread(args.fmin, -1)
  fmax=cv2.imread(args.fmax, -1)
  
  device, fvfm_header, fvfm_data=pcv.fluor_fvfm(fdark,fmin,fmax,kept_mask, device, args.outdir+'/'+filename, 1000, args.debug)

  # Output shape and color data
  pcv.print_results(args.fmax, shape_header, shape_data)
  pcv.print_results(args.fmax, fvfm_header, fvfm_data)
Esempio n. 8
0
def test_plantcv_readimage():
    img, path, img_name = pcv.readimage(filename=os.path.join(TEST_DATA, TEST_INPUT_COLOR))
    # Assert that the image name returned equals the name of the input image
    # Assert that the path of the image returned equals the path of the input image
    # Assert that the dimensions of the returned image equals the expected dimensions
    if img_name == TEST_INPUT_COLOR and path == TEST_DATA:
        if all([i == j] for i, j in zip(np.shape(img), TEST_COLOR_DIM)):
            assert 1
        else:
            assert 0
    else:
        assert 0
Esempio n. 9
0
def test_plantcv_crop_position_mask():
    nir, path1, filename1 = pcv.readimage(
        os.path.join(TEST_DATA, TEST_INPUT_NIR_MASK))
    mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MASK), -1)
    device, newmask = pcv.crop_position_mask(nir,
                                             mask,
                                             device=0,
                                             x=40,
                                             y=3,
                                             v_pos="top",
                                             h_pos="right",
                                             debug=None)
    assert np.sum(newmask) == 641517
Esempio n. 10
0
def test_plantcv_readimage():
    img, path, img_name = pcv.readimage(
        filename=os.path.join(TEST_DATA, TEST_INPUT_COLOR))
    # Assert that the image name returned equals the name of the input image
    # Assert that the path of the image returned equals the path of the input image
    # Assert that the dimensions of the returned image equals the expected dimensions
    if img_name == TEST_INPUT_COLOR and path == TEST_DATA:
        if all([i == j] for i, j in zip(np.shape(img), TEST_COLOR_DIM)):
            assert 1
        else:
            assert 0
    else:
        assert 0
def remove_background():

    #Uploading all files posted by the user on the input page to the upload directory
    if request.method == 'POST':
        imagefile = request.files['image']
        csvfile = request.files['csv']

        i = os.path.join(app.config['UPLOAD_FOLDER'], imagefile.filename)
        imagefile.save(i)

        c = os.path.join(app.config['UPLOAD_FOLDER'], csvfile.filename)
        csvfile.save(c)

        print("Files uploaded successfully")

    img, path, filename = pcv.readimage(i)

    #Converting the CSV file to a txt file
    convert_csv(csvfile.filename)
    fn = csvfile.filename.split(".")[0]
    plotstextfilename = fn + ".txt"
    cp = os.path.join(app.config['UPLOAD_FOLDER'], plotstextfilename)

    #Reading in the converted CSV file into an array of plotted points
    pts = np.array(get_values(cp))

    #Creating mask arrays of 0's
    mask = np.zeros((img.shape[0], img.shape[1]))

    #Filling the polygon
    cv2.fillConvexPoly(mask, pts, 1)
    mask = mask.astype(np.bool)

    #Creating the output
    out = np.zeros_like(img)
    out[mask] = img[mask]

    #Turning the black background into a white background
    out[np.where((out == [0, 0, 0]).all(axis=2))] = [255, 255, 255]

    #Converting the image from a Numpy array to a Base64 string to allow the website to render it properly
    im = Image.fromarray(out.astype("uint8"))
    b, g, r = im.split()
    im = Image.merge("RGB", (r, g, b))
    rawBytes = io.BytesIO()
    im.save(rawBytes, "PNG")
    rawBytes.seek(0)
    outimage = base64.b64encode(rawBytes.read())

    #Returning the template and image
    return render_template("result.html", image=outimage)
def naive_bayes():

    if request.method == 'POST':
        imagefile = request.files['image']
        pdffile = request.files['pdf']

        i = os.path.join(app.config['UPLOAD_FOLDER'], imagefile.filename)
        imagefile.save(i)

        p = os.path.join(app.config['UPLOAD_FOLDER'], pdffile.filename)
        pdffile.save(p)

        print("Files uploaded successfully")

    img, path, filename = pcv.readimage(i)

    #Creating the mask from the base image and the model
    device, mask = pcv.naive_bayes_classifier(img,
                                              pdf_file=pdffile.filename,
                                              device=0,
                                              debug="print")

    #Applying the mask to the colour image
    device, masked_image = pcv.apply_mask(img,
                                          mask['plant'],
                                          'white',
                                          device,
                                          debug="print")

    #Converting the image from a Numpy array to a Base64 string to allow the website to render it properly
    im = Image.fromarray(masked_image.astype("uint8"))
    b, g, r = im.split()
    im = Image.merge("RGB", (r, g, b))
    rawBytes = io.BytesIO()
    im.save(rawBytes, "PNG")
    rawBytes.seek(0)
    outimage = base64.b64encode(rawBytes.read())

    #Returning the template and image
    return render_template("result.html", image=outimage)
def find_object(image, rand_int):

    head, tail = os.path.split(image)
    print "Finding object in image " + tail + "..."

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

    # Pipeline step
    device = rand_int

    debug = "print"  # or "plot"

    # Convert RGB to HSV and extract the Saturation channel
    # hue, saturation, value
    device, h = pcv.rgb2gray_hsv(img, 'h', device)

    # Threshold the Saturation image
    device, h_thresh = pcv.binary_threshold(h, 30, 255, 'light', device)

    device, h_mblur = pcv.median_blur(h_thresh, 5, device)
    device, h_cnt = pcv.median_blur(h_thresh, 5, device)

    # Fill small objects
    device, ab_fill = pcv.fill(h_mblur, h_mblur, 200, device)

    # Apply Mask (for vis images, mask_color=white)
    device, masked = pcv.apply_mask(img, h_mblur, 'white', device)

    # Identify objects
    device, id_objects,obj_hierarchy = pcv.find_objects(masked, h_mblur, device)

    # Define ROI
    device, roi1, roi_hierarchy = pcv.define_roi(masked, 'rectangle', device, None, 'default', False, 0, 0, 0, 0)

    # Decide which objects to keep
    device, roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(img, 'partial', roi1, roi_hierarchy, id_objects, obj_hierarchy, device, debug)

    return
def binary_mask():

    if request.method == 'POST':
        imagefile = request.files['image']

        i = os.path.join(app.config['UPLOAD_FOLDER'], imagefile.filename)
        imagefile.save(i)

        print("Files uploaded successfully")

    img, path, filename = pcv.readimage(i)

    names = {"h": "hue", "s": "saturation", "v": "value"}

    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(hsv)
    channels = {"h": h, "s": s, "v": v}
    s = channels["s"]

    #Creating the binary threshold image using the channel, the threshold, the max value, and the object type
    device, s_thresh = pcv.binary_threshold(s,
                                            85,
                                            255,
                                            'light',
                                            device=0,
                                            debug=None)

    #Converting the image from Numpy array to Base64 string. The image is not BGR so it does not need to be converted to RGB this time
    im = Image.fromarray(s_thresh.astype("uint8"))
    rawBytes = io.BytesIO()
    im.save(rawBytes, "PNG")
    rawBytes.seek(0)
    outimage = base64.b64encode(rawBytes.read())

    #Returning the template and image
    return render_template("result.html", image=outimage)
Esempio n. 15
0
def main():
    # Get options
    args = options()

    # Change labels
    debug = args.debug
    image = args.image

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

    # 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)
Esempio n. 16
0
def main():
    # Sets variables from input arguments
    args = options()

    device = 0  # Workflow step counter
    debug = args.debug  # Option to display debug images to the notebook
    rgb_img = args.image  # Name of seed Image
    writeimg = args.writeimg
    outfile = str(args.result)
    outdir = str(args.outdir)

    # Reads in RGB image
    img, path, filename = pcv.readimage(rgb_img)
    if writeimg is True:
        pcv.print_image(img, outfile + "_original.jpg")

    # Converts RGB to HSV and extract the Saturation channel and inverts image
    device, img_gray_sat = pcv.rgb2gray_hsv(img, 's', device, debug)
    img_gray_sat = 255 - img_gray_sat

    # Corrects saturation image background brightness
    sat_img2 = 255 - correct_white_background(img_gray_sat)

    # Convert RGB to HSV and extract the Value channel
    device, img_gray_val = pcv.rgb2gray_hsv(img, 'v', device, debug)

    # Corrects value image background brightness
    val_img2 = 255 - correct_white_background(img_gray_val)

    # Convert RGB to HSV and extract the Hue channel
    device, img_hue = pcv.rgb2gray_hsv(img, 'h', device, debug)
    # Corrects Hue Image Based on standard
    mask = np.zeros(img.shape[:2], np.uint8)
    mask[1050: 1150, 3750: 3850] = 255
    huehist = cv2.calcHist([img_hue], [0], mask, [256], [0, 256])
    correction_factor = 155 - np.argmax(huehist)
    hue_channel = np.add(img_hue, correction_factor)
    if correction_factor > 0:
        hue_channel = np.where(hue_channel > 179, hue_channel - 180, hue_channel)
    elif correction_factor < 0:
        hue_channel = np.where(hue_channel < 0, 180 + hue_channel, hue_channel)

    # Thresholds the Saturation image
    device, sat_img_binary = pcv.binary_threshold(sat_img2, 35, 255, 'light', device, debug)

    # Threshold the Value image
    device, val_img_binary = pcv.binary_threshold(val_img2, 35, 255, 'light', device, debug)

    # Combines masks
    img_binary = np.where(sat_img_binary < 255, val_img_binary, sat_img_binary)

    # Fills in speckles smaller than 200 pixels
    mask = np.copy(img_binary)
    device, fill_image = pcv.fill(img_binary, mask, 200, device, debug)
    if writeimg is True:
        pcv.print_image(mask, outfile + "_mask.jpg")
        pcv.print_image(img_binary, outfile + "_binary.jpg")

    # Identifies objects using filled binary image as a mask
    device, id_objects, obj_hierarchy = pcv.find_objects(img, fill_image, device, debug)

    # Defines rectangular ROI
    device, roi, roi_hierarchy = \
        pcv.define_roi(img, 'rectangle', device, None, 'default', debug, True, 300, 1000, -1250, -425)

    # Keeps only objects within or partially within ROI
    device, roi_objects, roi_obj_hierarchy, kept_mask, obj_area = \
        pcv.roi_objects(img, 'partial', roi, roi_hierarchy, id_objects, obj_hierarchy, device, debug)

    # Randomly colors the individual seeds
    img_copy = np.copy(img)
    for i in range(0, len(roi_objects)):
        rand_color = color_palette(1)
        cv2.drawContours(img_copy, roi_objects, i, rand_color[0], -1, lineType=8, hierarchy=roi_obj_hierarchy)
    if writeimg is True:
        pcv.print_image(img_copy, outfile + "_coloredseeds.jpg")

    # Gets the area of each seed, saved in shape_data
    shape_header = []
    table = []
    for i in range(0, len(roi_objects)):
        if roi_obj_hierarchy[0][i][3] == -1:  # Checks if shape is a parent contour

            # Object combine kept objects
            device, obj, mask2 = pcv.object_composition(img, [roi_objects[i]], np.array([[roi_obj_hierarchy[0][i]]]),
                                                        device, debug)
            if obj is not None:
                device, shape_header, shape_data, shape_img = \
                    pcv.analyze_object(img, rgb_img, obj, mask2, device, debug)
                if shape_header is not None:
                    shape_header.append('hue')
                    shape_header.append('saturation')
                if shape_data is not None:
                    darkval = float(np.sum(np.multiply(sat_img2, mask2))) / np.sum(mask2)
                    huehist = cv2.calcHist([hue_channel], [0], mask2, [256], [0, 256])
                    hueval = np.argmax(huehist)
                    shape_data.append(hueval)
                    shape_data.append(darkval)
                    table.append(shape_data)

    # Finds the area of the size marker in pixels and saves to "marker data"
    device, marker_header, marker_data, analysis_images =\
        pcv.report_size_marker_area(img, 'rectangle', device, debug, "detect", 3525, 850, -200, -1700, "black",
                                    "light", "h", 120)
    # shape_header.append("marker_area")

    # Saves seed and marker shape data results to file
    metadata = open(posixpath.join(outdir, outfile), 'r').read()
    os.remove(posixpath.join(outdir, outfile))

    for seed, row in enumerate(table):
        prefix = posixpath.join(outdir, outfile[0:-4])
        results = open(prefix + '_' + str(seed + 1) + '.txt', 'w')
        results.write(metadata)
        results.write('\t'.join(map(str, shape_header)) + '\n')
        # row.append(marker_data[1])
        results.write('\t'.join(map(str, row)) + '\n')
        results.write('\t'.join(map(str, marker_header)) + '\n')
        results.write('\t'.join(map(str, marker_data)) + '\n')
        results.close()
Esempio n. 17
0
                                                 debug=False, adjust=True, x_adj=100, y_adj=50, w_adj=-150,
                                                 h_adj=-50)

    # Decide which objects to keep
    device, roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(img, 'partial', roi1, roi_hierarchy,
                                                                           id_objects, obj_hierarchy, device,
                                                                           debug=False)

    # Object combine kept objects
    device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3, device, debug=False)
    return device, ab_fill, mask, obj



# Read image
img, path, filename = pcv.readimage("yucca3.jpg")
img = cv2.resize(img, (0, 0), fx=0.2, fy=0.2)

device, ab_fill, masked2,obj = back_for_ground_sub(img, False)
device = 1
# obj, win, thresh, sep, img, device, debug=None

device, list_of_acute_points = pcv.acute_vertex(obj, 30, 90, 50, img, device, debug='plot')
# Segment image with watershed function
# device, watershed_header, watershed_data,analysis_images=pcv.watershed_segmentation(device, img,masked2,10,'./examples',debug=False)

# print(watershed_header)
#print(watershed_data)


# Determine color properties: Histograms, Color Slices and Pseudocolored Images, output color analyzed images (optional)
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()
Esempio n. 19
0
def main():
    # Get options 1
    args = options()

    # lee imagen 2
    img, path, filename = pcv.readimage(args.image)
   # cv2.imshow("imagen",img)
    # pasos del pipeline 3
    device = 0
    debug=args.debug 

    # Convert RGB to HSV and extract the Saturation channel 4
    #convertir RGB a HSV y extraer el canal de saturacion
    device, s = pcv.rgb2gray_hsv(img, 's', device, debug)
   # cv2.imshow("rgb a hsv y extraer saturacion 4",s)
     # Threshold the Saturation image 5
     #sacar imagen binaria del canal de saturacion
    device, s_thresh = pcv.binary_threshold(s, 85, 255, 'light', device, debug)
   # cv2.imshow("imagen binaria de hsv",s_thresh)
    # Median Filter 6
    #sacar un filtro median_blur
    device, s_mblur = pcv.median_blur(s_thresh, 5, device, debug)
    device, s_cnt = pcv.median_blur(s_thresh, 5, device, debug)
   # cv2.imshow("s_mblur",s_mblur)
   # cv2.imshow("s_cnt",s_cnt)
    # Convert RGB to LAB and extract the Blue channel 7
    #convertir RGB(imagen original) a LAB Y extraer el canal azul
    device, b = pcv.rgb2gray_lab(img, 'b', device, debug)
   # cv2.imshow("convertir RGB a LAB",b)
    # Threshold the blue image 8
    #sacar imagen binaria de LAB  imagen blue
    device, b_thresh = pcv.binary_threshold(b, 160, 255, 'light', device, debug)
    device, b_cnt = pcv.binary_threshold(b, 160, 255, 'light', device, debug)
   # cv2.imshow("imagen binaria de LAB",b_thresh)
   # cv2.imshow("imagen binaria",b_cnt)
    # Fill small objects
    #device, b_fill = pcv.fill(b_thresh, b_cnt, 10, device, debug)
    
     # Join the thresholded saturation and blue-yellow images 9
    #
    device, bs = pcv.logical_or(s_mblur, b_cnt, device, debug)
   # cv2.imshow("suma logica s_mblur and b_cnt",bs)
     # Apply Mask (for vis images, mask_color=white) 10
    device, masked = pcv.apply_mask(img, bs, 'white', device, debug)
   # cv2.imshow("aplicar mascara masked",masked)
    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels 11
    device, masked_a = pcv.rgb2gray_lab(masked, 'a', device, debug)
    device, masked_b = pcv.rgb2gray_lab(masked, 'b', device, debug)
   # cv2.imshow("canal verde-magenta",masked_a)
   # cv2.imshow("canal azul-amarillo",masked_b)  
    # Threshold the green-magenta and blue images 12
    device, maskeda_thresh = pcv.binary_threshold(masked_a, 115, 255, 'dark', device, debug)
    device, maskeda_thresh1 = pcv.binary_threshold(masked_a, 135, 255, 'light', device, debug)
    device, maskedb_thresh = pcv.binary_threshold(masked_b, 128, 255, 'light', device, debug)
   # cv2.imshow("threshold de canal verde-magenta dark",maskeda_thresh)
   # cv2.imshow("threshold de canal verde-magenta light",maskeda_thresh1)
   # cv2.imshow("threshold de canal azul-amarillo",maskedb_thresh)
    # Join the thresholded saturation and blue-yellow images (OR) 13
    device, ab1 = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, debug)
    device, ab = pcv.logical_or(maskeda_thresh1, ab1, device, debug)
    device, ab_cnt = pcv.logical_or(maskeda_thresh1, ab1, device, debug)
   # cv2.imshow("suma logica or 1",ab1)
   # cv2.imshow("suma logica or 2 ab",ab)
   # cv2.imshow("suma logica or 3 ab_cnt",ab_cnt)
   
    # Fill small objects 14
    device, ab_fill = pcv.fill(ab, ab_cnt, 200, device, debug)
   # cv2.imshow("ab_fill",ab_fill)
    # Apply mask (for vis images, mask_color=white) 15
    device, masked2 = pcv.apply_mask(masked, ab_fill, 'white', device, debug)
   # cv2.imshow("aplicar maskara2 white",masked2)
   
    ####################entendible hasta aqui######################
    # Identify objects 16 solo print Se utiliza para identificar objetos (material vegetal) en una imagen.
    #imprime la imagen si uso print o no si uso plot no almacena la imagen pero en pritn si la aguarda
    #usa b_thresh y observa
    device,id_objects,obj_hierarchy = pcv.find_objects(masked2,ab_fill, device, debug)
  
    # Define ROI 17 solo print encierra el objeto detectato pero aun es manual aun no automatico
    device, roi1, roi_hierarchy= pcv.define_roi(masked2, 'rectangle', device, None, 'default', debug, True, 92, 80, -127, -343)
    
    # Decide which objects to keep 18
    device,roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(img, 'partial', roi1, roi_hierarchy, id_objects, obj_hierarchy, device, debug)
    
    # Object combine kept objects 19
    device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3, device, debug)


    ############### Analysis ################

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

    # Find shape properties, output shape image (optional)
    device, shape_header, shape_data, shape_img = pcv.analyze_object(img,'image', obj, mask, device,args.outdir + '/' + filename)

    # Shape properties relative to user boundary line (optional)
    device, boundary_header, boundary_data, boundary_img1 = pcv.analyze_bound(img, args.image, obj, mask, 1680, device, debug, args.outdir + '/' + filename)

    # 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, args.image, kept_mask, 256, device, debug, 'all', 'v', 'img', 300, args.outdir + '/' + filename)

     #Write shape and color data to results file
    result=open(args.result,"a")
    result.write('\t'.join(map(str,shape_header)))
    result.write("\n")
    result.write('\t'.join(map(str,shape_data)))
    result.write("\n")
    for row in shape_img:  
        result.write('\t'.join(map(str,row)))
        result.write("\n")
    result.write('\t'.join(map(str,color_header)))
    result.write("\n")
    result.write('\t'.join(map(str,color_data)))
    result.write("\n")
    for row in color_img:
        result.write('\t'.join(map(str,row)))
        result.write("\n")
    result.close()
    cv2.waitKey()
    cv2.destroyAllWindows()
Esempio n. 20
0
import plantcv as pcv

#lee imagen

img, path, img_filename = pcv.readimage(
    "/home/fitosmartplatform/plantCV/prueba/plan.jpg")

#contador dl paso de procesamiento de imagen
device = 0

# Create binary image from a gray image based on threshold values. Targeting light objects in the image.
device, threshold_light = pcv.binary_threshold(img,
                                               36,
                                               255,
                                               'dark',
                                               device,
                                               debug="print")
device, h_channel = pcv.rgb2gray_hsv(img, 'h', device, debug="print")
pcv.print_image(h_channel,
                "/home/fitosmartplatform/plantCV/prueba/image-gray.jpg")
pcv.print_image(threshold_light,
                "/home/fitosmartplatform/plantCV/prueba/test-image.jpg")
"""notas"""
#si uso plot :imprime datos de la imagen no aguarda
#si uso print :imprime imagen y la aguarda con un nombre de la funcion

#si uso el light es un entorno diferente
#si uso  dark es otro entorno de la imagen
def main():
    # Get options
    args = options()

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

    # Pipeline step
    device = 0

    # Convert RGB to HSV and extract the Saturation channel
    device, s = pcv.rgb2gray_hsv(img, "s", device, args.debug)

    # Threshold the Saturation image
    device, s_thresh = pcv.binary_threshold(s, 36, 255, "light", device, args.debug)

    # Median Filter
    device, s_mblur = pcv.median_blur(s_thresh, 0, device, args.debug)
    device, s_cnt = pcv.median_blur(s_thresh, 0, device, args.debug)

    # Fill small objects
    # device, s_fill = pcv.fill(s_mblur, s_cnt, 0, device, args.debug)

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

    # Threshold the blue image
    device, b_thresh = pcv.binary_threshold(b, 137, 255, "light", device, args.debug)
    device, b_cnt = pcv.binary_threshold(b, 137, 255, "light", device, args.debug)

    # Fill small objects
    # device, b_fill = pcv.fill(b_thresh, b_cnt, 10, device, args.debug)

    # Join the thresholded saturation and blue-yellow images
    device, bs = pcv.logical_and(s_mblur, b_cnt, device, args.debug)

    # Apply Mask (for vis images, mask_color=white)
    device, masked = pcv.apply_mask(img, bs, "white", device, args.debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, masked_a = pcv.rgb2gray_lab(masked, "a", device, args.debug)
    device, masked_b = pcv.rgb2gray_lab(masked, "b", device, args.debug)

    # Threshold the green-magenta and blue images
    device, maskeda_thresh = pcv.binary_threshold(masked_a, 127, 255, "dark", device, args.debug)
    device, maskedb_thresh = pcv.binary_threshold(masked_b, 128, 255, "light", device, args.debug)

    # Join the thresholded saturation and blue-yellow images (OR)
    device, ab = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, args.debug)
    device, ab_cnt = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, args.debug)

    # Fill small noise
    device, ab_fill1 = pcv.fill(ab, ab_cnt, 2, device, args.debug)

    # Dilate to join small objects with larger ones
    device, ab_cnt1 = pcv.dilate(ab_fill1, 3, 2, device, args.debug)
    device, ab_cnt2 = pcv.dilate(ab_fill1, 3, 2, device, args.debug)

    # Fill dilated image mask
    device, ab_cnt3 = pcv.fill(ab_cnt2, ab_cnt1, 150, device, args.debug)
    device, masked2 = pcv.apply_mask(masked, ab_cnt3, "white", device, args.debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, masked2_a = pcv.rgb2gray_lab(masked2, "a", device, args.debug)
    device, masked2_b = pcv.rgb2gray_lab(masked2, "b", device, args.debug)

    # Threshold the green-magenta and blue images
    device, masked2a_thresh = pcv.binary_threshold(masked2_a, 127, 255, "dark", device, args.debug)
    device, masked2b_thresh = pcv.binary_threshold(masked2_b, 128, 255, "light", device, args.debug)
    device, ab_fill = pcv.logical_or(masked2a_thresh, masked2b_thresh, device, args.debug)

    # Identify objects
    device, id_objects, obj_hierarchy = pcv.find_objects(masked2, ab_fill, device, args.debug)

    # Define ROI
    device, roi1, roi_hierarchy = pcv.define_roi(
        masked2, "rectangle", device, None, "default", args.debug, True, 500, 0, -600, -885
    )

    # Decide which objects to keep
    device, roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(
        img, "partial", roi1, roi_hierarchy, id_objects, obj_hierarchy, device, args.debug
    )

    # Object combine kept objects
    device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3, device, args.debug)

    ############## VIS Analysis ################

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

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

    # Shape properties relative to user boundary line (optional)
    device, boundary_header, boundary_data, boundary_img1 = pcv.analyze_bound(
        img, args.image, obj, mask, 845, device, args.debug, 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, args.image, mask, 256, device, args.debug, None, "v", "img", 300, outfile
    )

    # Output shape and color data

    result = open(args.result, "a")
    result.write("\t".join(map(str, shape_header)))
    result.write("\n")
    result.write("\t".join(map(str, shape_data)))
    result.write("\n")
    for row in shape_img:
        result.write("\t".join(map(str, row)))
        result.write("\n")
    result.write("\t".join(map(str, color_header)))
    result.write("\n")
    result.write("\t".join(map(str, color_data)))
    result.write("\n")
    result.write("\t".join(map(str, boundary_header)))
    result.write("\n")
    result.write("\t".join(map(str, boundary_data)))
    result.write("\n")
    result.write("\t".join(map(str, boundary_img1)))
    result.write("\n")
    for row in color_img:
        result.write("\t".join(map(str, row)))
        result.write("\n")
    result.close()

    ############################# Use VIS image mask for NIR image#########################
    # Find matching NIR image
    device, nirpath = pcv.get_nir(path, filename, device, args.debug)
    nir, path1, filename1 = pcv.readimage(nirpath)
    nir2 = cv2.imread(nirpath, -1)

    # Flip mask
    device, f_mask = pcv.flip(mask, "vertical", device, args.debug)

    # Reize mask
    device, nmask = pcv.resize(f_mask, 0.1304, 0.1304, device, args.debug)

    # position, and crop mask
    device, newmask = pcv.crop_position_mask(nir, nmask, device, 65, 0, "top", "left", args.debug)

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

    # Object combine kept objects
    device, nir_combined, nir_combinedmask = pcv.object_composition(nir, nir_objects, nir_hierarchy, device, args.debug)

    ####################################### Analysis #############################################
    outfile1 = False
    if args.writeimg == True:
        outfile1 = args.outdir + "/" + filename1

    device, nhist_header, nhist_data, nir_imgs = pcv.analyze_NIR_intensity(
        nir2, filename1, nir_combinedmask, 256, device, False, args.debug, outfile1
    )
    device, nshape_header, nshape_data, nir_shape = pcv.analyze_object(
        nir2, filename1, nir_combined, nir_combinedmask, device, args.debug, outfile1
    )

    coresult = open(args.coresult, "a")
    coresult.write("\t".join(map(str, nhist_header)))
    coresult.write("\n")
    coresult.write("\t".join(map(str, nhist_data)))
    coresult.write("\n")
    for row in nir_imgs:
        coresult.write("\t".join(map(str, row)))
        coresult.write("\n")

    coresult.write("\t".join(map(str, nshape_header)))
    coresult.write("\n")
    coresult.write("\t".join(map(str, nshape_data)))
    coresult.write("\n")
    coresult.write("\t".join(map(str, nir_shape)))
    coresult.write("\n")
    coresult.close()
Esempio n. 22
0
def main():
  # Get options
  args = options()
  
  # Read image
  img, path, filename = pcv.readimage(args.image)
  #roi = cv2.imread(args.roi)
  
  # Pipeline step
  device = 0

  # Convert RGB to HSV and extract the Saturation channel
  device, s = pcv.rgb2gray_hsv(img, 's', device, args.debug)
  
  # Threshold the Saturation image
  device, s_thresh = pcv.binary_threshold(s, 36, 255, 'light', device, args.debug)
  
  # Median Filter
  device, s_mblur = pcv.median_blur(s_thresh, 5, device, args.debug)
  device, s_cnt = pcv.median_blur(s_thresh, 5, device, args.debug)
  
  # Fill small objects
  device, s_fill = pcv.fill(s_mblur, s_cnt, 0, device, args.debug)
  
  # Convert RGB to LAB and extract the Blue channel
  device, b = pcv.rgb2gray_lab(img, 'b', device, args.debug)
  
  # Threshold the blue image
  device, b_thresh = pcv.binary_threshold(b, 138, 255, 'light', device, args.debug)
  device, b_cnt = pcv.binary_threshold(b, 138, 255, 'light', device, args.debug)
  
  # Fill small objects
  device, b_fill = pcv.fill(b_thresh, b_cnt, 150, device, args.debug)
  
  # Join the thresholded saturation and blue-yellow images
  device, bs = pcv.logical_and(s_fill, b_fill, device, args.debug)
  
  # Apply Mask (for vis images, mask_color=white)
  device, masked = pcv.apply_mask(img, bs, 'white', device, args.debug)
  
  # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
  device, masked_a = pcv.rgb2gray_lab(masked, 'a', device, args.debug)
  device, masked_b = pcv.rgb2gray_lab(masked, 'b', device, args.debug)
  
  # Threshold the green-magenta and blue images
  device, maskeda_thresh = pcv.binary_threshold(masked_a, 122, 255, 'dark', device, args.debug)
  device, maskedb_thresh = pcv.binary_threshold(masked_b, 133, 255, 'light', device, args.debug)
  
  # Join the thresholded saturation and blue-yellow images (OR)
  device, ab = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, args.debug)
  device, ab_cnt = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, args.debug)
  
  # Fill small objects
  device, ab_fill = pcv.fill(ab, ab_cnt, 200, device, args.debug)
  
  # Apply mask (for vis images, mask_color=white)
  device, masked2 = pcv.apply_mask(masked, ab_fill, 'white', device, args.debug)
  
  # Identify objects
  device, id_objects,obj_hierarchy = pcv.find_objects(masked2, ab_fill, device, args.debug)

  # Define ROI
  device, roi1, roi_hierarchy= pcv.define_roi(img,'rectangle', device, None, 'default', args.debug,True, 0, 0,0,-900)
  
  # Decide which objects to keep
  device,roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(img,'partial',roi1,roi_hierarchy,id_objects,obj_hierarchy,device, args.debug)
  
  # Object combine kept objects
  device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3, device, args.debug)
  
############## Analysis ################  
  
  # Find shape properties, output shape image (optional)
  device, shape_header,shape_data,shape_img = pcv.analyze_object(img, args.image, obj, mask, device,args.debug,args.outdir+'/'+filename)
  
  # Shape properties relative to user boundary line (optional)
  device, boundary_header,boundary_data, boundary_img1= pcv.analyze_bound(img, args.image,obj, mask, 830, device,args.debug,args.outdir+'/'+filename)
  
  # Determine color properties: Histograms, Color Slices and Pseudocolored Images, output color analyzed images (optional)
  device, color_header,color_data,norm_slice= pcv.analyze_color(img, args.image, kept_mask, 256, device, args.debug,'all','rgb','v','img',300,args.outdir+'/'+filename)
  
  # Output shape and color data
  pcv.print_results(args.image, shape_header, shape_data)
  pcv.print_results(args.image, color_header, color_data)
  pcv.print_results(args.image, boundary_header, boundary_data)
def main():
    # Get options
    args = options()

    # Read image
    img, path, filename = pcv.readimage(args.image)
    # roi = cv2.imread(args.roi)

    # Pipeline step
    device = 0

    device, mask = pcv.naive_bayes_classifier(img, "naive_bayes.pdf.txt",
                                              device, args.debug)

    mask1 = np.uint8(mask)

    mask_copy = np.copy(mask1)

    # Fill small objects
    device, soil_fill = pcv.fill(mask1, mask_copy, 200, device, args.debug)

    # Median Filter
    device, soil_mblur = pcv.median_blur(soil_fill, 5, device, args.debug)
    device, soil_cnt = pcv.median_blur(soil_fill, 5, device, args.debug)

    # Apply mask (for vis images, mask_color=white)
    device, masked2 = pcv.apply_mask(img, soil_cnt, 'white', device,
                                     args.debug)

    # Identify objects
    device, id_objects, obj_hierarchy = pcv.find_objects(
        masked2, soil_cnt, device, args.debug)

    # Define ROI
    device, roi1, roi_hierarchy = pcv.define_roi(img, 'rectangle', device,
                                                 None, 'default', args.debug,
                                                 True, 0, 0, 0, -925)

    # Decide which objects to keep
    device, roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(
        img, 'partial', roi1, roi_hierarchy, id_objects, obj_hierarchy, device,
        args.debug)

    # Object combine kept objects
    device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3,
                                               device, args.debug)

    # ############## Analysis ################
    # output mask
    device, maskpath, mask_images = pcv.output_mask(device, img, mask,
                                                    filename, args.outdir,
                                                    True, args.debug)

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

    # Shape properties relative to user boundary line (optional)
    device, boundary_header, boundary_data, boundary_img1 = pcv.analyze_bound(
        img, args.image, obj, mask, 900, device)

    # 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, args.image, mask, 256, device, args.debug, None, 'v', 'img', 300)

    result = open(args.result, "a")
    result.write('\t'.join(map(str, shape_header)))
    result.write("\n")
    result.write('\t'.join(map(str, shape_data)))
    result.write("\n")
    for row in mask_images:
        result.write('\t'.join(map(str, row)))
        result.write("\n")
    result.write('\t'.join(map(str, color_header)))
    result.write("\n")
    result.write('\t'.join(map(str, color_data)))
    result.write("\n")
    result.write('\t'.join(map(str, boundary_header)))
    result.write("\n")
    result.write('\t'.join(map(str, boundary_data)))
    result.write("\n")
    result.close()
Esempio n. 24
0
def main():
  # Get options
  args = options()
  
  # Read image
  img, path, filename = pcv.readimage(args.image)
    
  # Pipeline step
  device = 0

  # Convert RGB to HSV and extract the Saturation channel
  device, s = pcv.rgb2gray_hsv(img, 's', device, args.debug)
  
  # Threshold the Saturation image
  device, s_thresh = pcv.binary_threshold(s, 36, 255, 'light', device, args.debug)
  
  # Median Filter
  device, s_mblur = pcv.median_blur(s_thresh, 0, device, args.debug)
  device, s_cnt = pcv.median_blur(s_thresh, 0, device, args.debug)
  
  # Fill small objects
  #device, s_fill = pcv.fill(s_mblur, s_cnt, 0, device, args.debug)
  
  # Convert RGB to LAB and extract the Blue channel
  device, b = pcv.rgb2gray_lab(img, 'b', device, args.debug)
  
  # Threshold the blue image
  device, b_thresh = pcv.binary_threshold(b, 137, 255, 'light', device, args.debug)
  device, b_cnt = pcv.binary_threshold(b, 137, 255, 'light', device, args.debug)
  
  # Fill small objects
  #device, b_fill = pcv.fill(b_thresh, b_cnt, 10, device, args.debug)
  
  # Join the thresholded saturation and blue-yellow images
  device, bs = pcv.logical_and(s_mblur, b_cnt, device, args.debug)
  
  # Apply Mask (for vis images, mask_color=white)
  device, masked = pcv.apply_mask(img, bs, 'white', device, args.debug)
  
  # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
  device, masked_a = pcv.rgb2gray_lab(masked, 'a', device, args.debug)
  device, masked_b = pcv.rgb2gray_lab(masked, 'b', device, args.debug)
  
  # Threshold the green-magenta and blue images
  device, maskeda_thresh = pcv.binary_threshold(masked_a, 127, 255, 'dark', device, args.debug)
  device, maskedb_thresh = pcv.binary_threshold(masked_b, 128, 255, 'light', device, args.debug)
  
  # Join the thresholded saturation and blue-yellow images (OR)
  device, ab = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, args.debug)
  device, ab_cnt = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, args.debug)
  
  # Fill small noise
  device, ab_fill1 = pcv.fill(ab, ab_cnt, 2, device, args.debug)
  
  # Dilate to join small objects with larger ones
  device, ab_cnt1=pcv.dilate(ab_fill1, 3, 2, device, args.debug)
  device, ab_cnt2=pcv.dilate(ab_fill1, 3, 2, device, args.debug)
  
  # Fill dilated image mask
  device, ab_cnt3=pcv.fill(ab_cnt2,ab_cnt1,150,device,args.debug)
  device, masked2 = pcv.apply_mask(masked, ab_cnt3, 'white', device, args.debug)
  
  # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
  device, masked2_a = pcv.rgb2gray_lab(masked2, 'a', device, args.debug)
  device, masked2_b = pcv.rgb2gray_lab(masked2, 'b', device, args.debug)
  
  # Threshold the green-magenta and blue images
  device, masked2a_thresh = pcv.binary_threshold(masked2_a, 127, 255, 'dark', device, args.debug)
  device, masked2b_thresh = pcv.binary_threshold(masked2_b, 128, 255, 'light', device, args.debug)
  device, ab_fill = pcv.logical_or(masked2a_thresh, masked2b_thresh, device, args.debug)
  
  # Identify objects
  device, id_objects,obj_hierarchy = pcv.find_objects(masked2, ab_fill, device, args.debug)
  
  # Define ROI
  device, roi1, roi_hierarchy= pcv.define_roi(masked2,'rectangle', device, None, 'default', args.debug,True, 525, 0,-490,-150)
  
  # Decide which objects to keep
  device,roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(img,'partial',roi1,roi_hierarchy,id_objects,obj_hierarchy,device, args.debug)
  
  # Object combine kept objects
  device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3, device, args.debug)
  
  ############## VIS Analysis ################
  
  outfile=False
  if args.writeimg==True:
    outfile=args.outdir+"/"+filename
  
  # Find shape properties, output shape image (optional)
  device, shape_header,shape_data,shape_img = pcv.analyze_object(img, args.image, obj, mask, device,args.debug,outfile)
  
  # Shape properties relative to user boundary line (optional)
  device, boundary_header,boundary_data, boundary_img1= pcv.analyze_bound(img, args.image,obj, mask, 325, device,args.debug,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, args.image, mask, 256, device, args.debug,None,'v','img',300,outfile)
  
  # Output shape and color data

  result=open(args.result,"a")
  result.write('\t'.join(map(str,shape_header)))
  result.write("\n")
  result.write('\t'.join(map(str,shape_data)))
  result.write("\n")
  for row in shape_img:
    result.write('\t'.join(map(str,row)))
    result.write("\n")
  result.write('\t'.join(map(str,color_header)))
  result.write("\n")
  result.write('\t'.join(map(str,color_data)))
  result.write("\n")
  result.write('\t'.join(map(str,boundary_header)))
  result.write("\n")
  result.write('\t'.join(map(str,boundary_data)))
  result.write("\n")
  result.write('\t'.join(map(str,boundary_img1)))
  result.write("\n")
  for row in color_img:
    result.write('\t'.join(map(str,row)))
    result.write("\n")
  result.close()
def main():
    # Get options
    args = options()

    # Read image
    img, path, filename = pcv.readimage(args.image)
    brass_mask = cv2.imread(args.roi)

    # Pipeline step
    device = 0

    # Convert RGB to HSV and extract the Saturation channel
    device, s = pcv.rgb2gray_hsv(img, 's', device, args.debug)

    # Threshold the Saturation image
    device, s_thresh = pcv.binary_threshold(s, 49, 255, 'light', device,
                                            args.debug)

    # Median Filter
    device, s_mblur = pcv.median_blur(s_thresh, 5, device, args.debug)
    device, s_cnt = pcv.median_blur(s_thresh, 5, device, args.debug)

    # Fill small objects
    device, s_fill = pcv.fill(s_mblur, s_cnt, 150, device, args.debug)

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

    # Threshold the blue image
    device, b_thresh = pcv.binary_threshold(b, 138, 255, 'light', device,
                                            args.debug)
    device, b_cnt = pcv.binary_threshold(b, 138, 255, 'light', device,
                                         args.debug)

    # Fill small objects
    device, b_fill = pcv.fill(b_thresh, b_cnt, 150, device, args.debug)

    # Join the thresholded saturation and blue-yellow images
    device, bs = pcv.logical_and(s_fill, b_fill, device, args.debug)

    # Apply Mask (for vis images, mask_color=white)
    device, masked = pcv.apply_mask(img, bs, 'white', device, args.debug)

    # Mask pesky brass piece
    device, brass_mask1 = pcv.rgb2gray_hsv(brass_mask, 'v', device, args.debug)
    device, brass_thresh = pcv.binary_threshold(brass_mask1, 0, 255, 'light',
                                                device, args.debug)
    device, brass_inv = pcv.invert(brass_thresh, device, args.debug)
    device, brass_masked = pcv.apply_mask(masked, brass_inv, 'white', device,
                                          args.debug)

    # Further mask soil and car
    device, masked_a = pcv.rgb2gray_lab(brass_masked, 'a', device, args.debug)
    device, soil_car = pcv.binary_threshold(masked_a, 128, 255, 'dark', device,
                                            args.debug)
    device, soil_masked = pcv.apply_mask(brass_masked, soil_car, 'white',
                                         device, args.debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, soil_a = pcv.rgb2gray_lab(soil_masked, 'a', device, args.debug)
    device, soil_b = pcv.rgb2gray_lab(soil_masked, 'b', device, args.debug)

    # Threshold the green-magenta and blue images
    device, soila_thresh = pcv.binary_threshold(soil_a, 118, 255, 'dark',
                                                device, args.debug)
    device, soilb_thresh = pcv.binary_threshold(soil_b, 155, 255, 'light',
                                                device, args.debug)

    # Join the thresholded saturation and blue-yellow images (OR)
    device, soil_ab = pcv.logical_or(soila_thresh, soilb_thresh, device,
                                     args.debug)
    device, soil_ab_cnt = pcv.logical_or(soila_thresh, soilb_thresh, device,
                                         args.debug)

    # Fill small objects
    device, soil_fill = pcv.fill(soil_ab, soil_ab_cnt, 200, device, args.debug)

    # Median Filter
    device, soil_mblur = pcv.median_blur(soil_fill, 5, device, args.debug)
    device, soil_cnt = pcv.median_blur(soil_fill, 5, device, args.debug)

    # Apply mask (for vis images, mask_color=white)
    device, masked2 = pcv.apply_mask(soil_masked, soil_cnt, 'white', device,
                                     args.debug)

    # Identify objects
    device, id_objects, obj_hierarchy = pcv.find_objects(
        masked2, soil_cnt, device, args.debug)

    # Define ROI
    device, roi1, roi_hierarchy = pcv.define_roi(img, 'circle', device, None,
                                                 'default', args.debug, True,
                                                 0, 0, -50, -50)

    # Decide which objects to keep
    device, roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(
        img, 'partial', roi1, roi_hierarchy, id_objects, obj_hierarchy, device,
        args.debug)

    # Object combine kept objects
    device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3,
                                               device, args.debug)

    # ############# Analysis ################

    # output mask
    device, maskpath, mask_images = pcv.output_mask(device, img, mask,
                                                    filename, args.outdir,
                                                    True, args.debug)

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

    # 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, args.image, mask, 256, device, args.debug, None, 'v', 'img', 300)

    result = open(args.result, "a")
    result.write('\t'.join(map(str, shape_header)))
    result.write("\n")
    result.write('\t'.join(map(str, shape_data)))
    result.write("\n")
    for row in mask_images:
        result.write('\t'.join(map(str, row)))
        result.write("\n")
    result.write('\t'.join(map(str, color_header)))
    result.write("\n")
    result.write('\t'.join(map(str, color_data)))
    result.write("\n")
    result.close()
def main():
  # Get options
  args = options()
  
  # Read image
  img, path, filename = pcv.readimage(args.image)
  #roi = cv2.imread(args.roi)
  
  # Pipeline step
  device = 0

  # Convert RGB to HSV and extract the Saturation channel
  device, s = pcv.rgb2gray_hsv(img, 's', device, args.debug)
  
  # Threshold the Saturation image
  device, s_thresh = pcv.binary_threshold(s, 36, 255, 'light', device, args.debug)
  
  # Median Filter
  device, s_mblur = pcv.median_blur(s_thresh, 5, device, args.debug)
  device, s_cnt = pcv.median_blur(s_thresh, 5, device, args.debug)
  
  # Fill small objects
  device, s_fill = pcv.fill(s_mblur, s_cnt, 0, device, args.debug)
  
  # Convert RGB to LAB and extract the Blue channel
  device, b = pcv.rgb2gray_lab(img, 'b', device, args.debug)
  
  # Threshold the blue image
  device, b_thresh = pcv.binary_threshold(b, 138, 255, 'light', device, args.debug)
  device, b_cnt = pcv.binary_threshold(b, 138, 255, 'light', device, args.debug)
  
  # Fill small objects
  device, b_fill = pcv.fill(b_thresh, b_cnt, 150, device, args.debug)
  
  # Join the thresholded saturation and blue-yellow images
  device, bs = pcv.logical_and(s_fill, b_fill, device, args.debug)
  
  # Apply Mask (for vis images, mask_color=white)
  device, masked = pcv.apply_mask(img, bs, 'white', device, args.debug)
  
  # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
  device, masked_a = pcv.rgb2gray_lab(masked, 'a', device, args.debug)
  device, masked_b = pcv.rgb2gray_lab(masked, 'b', device, args.debug)
  
  # Threshold the green-magenta and blue images
  device, maskeda_thresh = pcv.binary_threshold(masked_a, 122, 255, 'dark', device, args.debug)
  device, maskedb_thresh = pcv.binary_threshold(masked_b, 133, 255, 'light', device, args.debug)
  
  # Join the thresholded saturation and blue-yellow images (OR)
  device, ab = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, args.debug)
  device, ab_cnt = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, args.debug)
  
  # Fill small objects
  device, ab_fill = pcv.fill(ab, ab_cnt, 200, device, args.debug)
  
  # Apply mask (for vis images, mask_color=white)
  device, masked2 = pcv.apply_mask(masked, ab_fill, 'white', device, args.debug)
  
  # Select area with black bars and find overlapping plant material
  device, roi1, roi_hierarchy1= pcv.define_roi(masked2,'rectangle', device, None, 'default', args.debug,True, 0, 0,-1900,0)
  device, id_objects1,obj_hierarchy1 = pcv.find_objects(masked2, ab_fill, device, args.debug)
  device,roi_objects1, hierarchy1, kept_mask1, obj_area1 = pcv.roi_objects(masked2,'cutto',roi1,roi_hierarchy1,id_objects1,obj_hierarchy1,device, args.debug)
  device, masked3 = pcv.apply_mask(masked2, kept_mask1, 'white', device, args.debug)
  device, masked_a1 = pcv.rgb2gray_lab(masked3, 'a', device, args.debug)
  device, masked_b1 = pcv.rgb2gray_lab(masked3, 'b', device, args.debug)
  device, maskeda_thresh1 = pcv.binary_threshold(masked_a1, 122, 255, 'dark', device, args.debug)
  device, maskedb_thresh1 = pcv.binary_threshold(masked_b1, 170, 255, 'light', device, args.debug)
  device, ab1 = pcv.logical_or(maskeda_thresh1, maskedb_thresh1, device, args.debug)
  device, ab_cnt1 = pcv.logical_or(maskeda_thresh1, maskedb_thresh1, device, args.debug)
  device, ab_fill1 = pcv.fill(ab1, ab_cnt1, 300, device, args.debug)

  
  device, roi2, roi_hierarchy2= pcv.define_roi(masked2,'rectangle', device, None, 'default', args.debug,True, 1900, 0,0,0)
  device, id_objects2,obj_hierarchy2 = pcv.find_objects(masked2, ab_fill, device, args.debug)
  device,roi_objects2, hierarchy2, kept_mask2, obj_area2 = pcv.roi_objects(masked2,'cutto',roi2,roi_hierarchy2,id_objects2,obj_hierarchy2,device, args.debug)
  device, masked4 = pcv.apply_mask(masked2, kept_mask2, 'white', device, args.debug)
  device, masked_a2 = pcv.rgb2gray_lab(masked4, 'a', device, args.debug)
  device, masked_b2 = pcv.rgb2gray_lab(masked4, 'b', device, args.debug)
  device, maskeda_thresh2 = pcv.binary_threshold(masked_a2, 122, 255, 'dark', device, args.debug)
  device, maskedb_thresh2 = pcv.binary_threshold(masked_b2, 170, 255, 'light', device, args.debug)
  device, ab2 = pcv.logical_or(maskeda_thresh2, maskedb_thresh2, device, args.debug)
  device, ab_cnt2 = pcv.logical_or(maskeda_thresh2, maskedb_thresh2, device, args.debug)
  device, ab_fill2 = pcv.fill(ab2, ab_cnt2, 200, device, args.debug)
  
  device, ab_cnt3 = pcv.logical_or(ab_fill1, ab_fill2, device, args.debug)
  device, masked3 = pcv.apply_mask(masked2, ab_cnt3, 'white', device, args.debug)
  
  # Identify objects
  device, id_objects3,obj_hierarchy3 = pcv.find_objects(masked2, ab_fill, device, args.debug)

  # Define ROI
  device, roi3, roi_hierarchy3= pcv.define_roi(masked2,'rectangle', device, None, 'default', args.debug,True, 500, 0,-450,-530)
 
  # Decide which objects to keep and combine with objects overlapping with black bars
  device,roi_objects3, hierarchy3, kept_mask3, obj_area1 = pcv.roi_objects(img,'cutto',roi3,roi_hierarchy3,id_objects3,obj_hierarchy3,device, args.debug)
  device, kept_mask4_1 = pcv.logical_or(ab_cnt3, kept_mask3, device, args.debug)
  device, kept_cnt = pcv.logical_or(ab_cnt3, kept_mask3, device, args.debug)
  device, kept_mask4 = pcv.fill(kept_mask4_1, kept_cnt, 200, device, args.debug)
  device, masked5 = pcv.apply_mask(masked2, kept_mask4, 'white', device, args.debug)
  device, id_objects4,obj_hierarchy4 = pcv.find_objects(masked5, kept_mask4, device, args.debug)
  device, roi4, roi_hierarchy4= pcv.define_roi(masked2,'rectangle', device, None, 'default', args.debug,False, 0, 0,0,0)
  device,roi_objects4, hierarchy4, kept_mask4, obj_area = pcv.roi_objects(img,'partial',roi4,roi_hierarchy4,id_objects4,obj_hierarchy4,device, args.debug)

 # Object combine kept objects
  device, obj, mask = pcv.object_composition(img, roi_objects4, hierarchy4, device, args.debug)
  
############## Analysis ################  
  
  # Find shape properties, output shape image (optional)
  device, shape_header,shape_data,shape_img = pcv.analyze_object(img, args.image, obj, mask, device,args.debug,args.outdir+'/'+filename)
   
  # Shape properties relative to user boundary line (optional)
  device, boundary_header,boundary_data, boundary_img1= pcv.analyze_bound(img, args.image,obj, mask, 950, device,args.debug,args.outdir+'/'+filename)
  
  # Tiller Tool Test
  device, tillering_header, tillering_data, tillering_img= pcv.tiller_count(img, args.image,obj, mask, 965, device,args.debug,args.outdir+'/'+filename)

  
  # Determine color properties: Histograms, Color Slices and Pseudocolored Images, output color analyzed images (optional)
  device, color_header,color_data,norm_slice= pcv.analyze_color(img, args.image, kept_mask4, 256, device, args.debug,'all','rgb','v',args.outdir+'/'+filename)
  
  # Output shape and color data
  pcv.print_results(args.image, shape_header, shape_data)
  pcv.print_results(args.image, color_header, color_data)
  pcv.print_results(args.image, boundary_header, boundary_data)
  pcv.print_results(args.image, tillering_header,tillering_data)
Esempio n. 27
0
def main():
    # Get options
    args = options()

    # Read image
    img, path, filename = pcv.readimage(args.image)
    #roi = cv2.imread(args.roi)

    # Pipeline step
    device = 0

    # Convert RGB to HSV and extract the Saturation channel
    device, s = pcv.rgb2gray_lab(img, 'l', device, args.debug)

    # Threshold the Saturation image
    device, s_thresh = pcv.binary_threshold(s, 100, 255, 'light', device,
                                            args.debug)

    # Median Filter
    device, s_mblur = pcv.median_blur(s_thresh, 0, device, args.debug)
    device, s_cnt = pcv.median_blur(s_thresh, 0, device, args.debug)

    # Fill small objects
    #device, s_fill = pcv.fill(s_mblur, s_cnt, 0, device, args.debug)

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

    # Threshold the blue image
    device, b_thresh = pcv.binary_threshold(b, 145, 255, 'light', device,
                                            args.debug)
    device, b_cnt = pcv.binary_threshold(b, 145, 255, 'light', device,
                                         args.debug)

    # Fill small objects
    #device, b_fill = pcv.fill(b_thresh, b_cnt, 10, device, args.debug)

    # Join the thresholded saturation and blue-yellow images
    device, bs = pcv.logical_and(s_mblur, b_cnt, device, args.debug)

    # Apply Mask (for vis images, mask_color=white)
    device, masked = pcv.apply_mask(img, bs, 'white', device, args.debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, masked_a = pcv.rgb2gray_lab(masked, 'a', device, args.debug)
    device, masked_b = pcv.rgb2gray_lab(masked, 'b', device, args.debug)

    # Threshold the green-magenta and blue images
    device, maskeda_thresh = pcv.binary_threshold(masked_a, 127, 255, 'dark',
                                                  device, args.debug)
    device, maskedb_thresh = pcv.binary_threshold(masked_b, 128, 255, 'light',
                                                  device, args.debug)

    # Join the thresholded saturation and blue-yellow images (OR)
    device, ab = pcv.logical_or(maskeda_thresh, maskedb_thresh, device,
                                args.debug)
    device, ab_cnt = pcv.logical_or(maskeda_thresh, maskedb_thresh, device,
                                    args.debug)

    # Fill small objects
    device, ab_fill = pcv.fill(ab, ab_cnt, 20, device, args.debug)

    # Apply mask (for vis images, mask_color=white)
    device, masked2 = pcv.apply_mask(masked, ab_fill, 'white', device,
                                     args.debug)

    # Identify objects
    device, id_objects, obj_hierarchy = pcv.find_objects(
        masked2, ab_fill, device, args.debug)

    # Define ROI
    device, roi1, roi_hierarchy = pcv.define_roi(img, 'rectangle', device,
                                                 None, 'default', args.debug,
                                                 True, 30, 25, -10, -15)

    # Decide which objects to keep
    device, roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(
        img, 'partial', roi1, roi_hierarchy, id_objects, obj_hierarchy, device,
        args.debug)

    # Object combine kept objects
    device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3,
                                               device, args.debug)

    ############## Analysis ################

    # Find shape properties, output shape image (optional)
    device, shape_header, shape_data, shape_img = pcv.analyze_object(
        img, args.image, obj, mask, device, args.debug,
        args.outdir + '/' + filename)

    # Shape properties relative to user boundary line (optional)
    device, boundary_header, boundary_data, boundary_img1 = pcv.analyze_bound(
        img, args.image, obj, mask, 25, device, args.debug,
        args.outdir + '/' + filename)

    # Determine color properties: Histograms, Color Slices and Pseudocolored Images, output color analyzed images (optional)
    device, color_header, color_data, norm_slice = pcv.analyze_color(
        img, args.image, kept_mask, 256, device, args.debug, 'all', 'rgb', 'v',
        'img', 300, args.outdir + '/' + filename)

    # Output shape and color data
    pcv.print_results(args.image, shape_header, shape_data)
    pcv.print_results(args.image, color_header, color_data)
    pcv.print_results(args.image, boundary_header, boundary_data)
def process_sv_images(vis_img, nir_img, debug=None):
    """Process side-view images.

    Inputs:
    vis_img = An RGB image.
    nir_img = An NIR grayscale image.
    debug   = None, print, or plot. Print = save to file, Plot = print to screen.

    :param vis_img: str
    :param nir_img: str
    :param debug: str
    :return:
    """
    # Read VIS image
    img, path, filename = pcv.readimage(vis_img)

    # Pipeline step
    device = 0

    # Convert RGB to HSV and extract the Saturation channel
    device, s = pcv.rgb2gray_hsv(img, 's', device, debug)

    # Threshold the Saturation image
    device, s_thresh = pcv.binary_threshold(s, 36, 255, 'light', device, debug)

    # Median Filter
    device, s_mblur = pcv.median_blur(s_thresh, 5, device, debug)
    device, s_cnt = pcv.median_blur(s_thresh, 5, device, debug)

    # Fill small objects
    # device, s_fill = pcv.fill(s_mblur, s_cnt, 0, device, args.debug)

    # Convert RGB to LAB and extract the Blue channel
    device, b = pcv.rgb2gray_lab(img, 'b', device, debug)

    # Threshold the blue image
    device, b_thresh = pcv.binary_threshold(b, 137, 255, 'light', device, debug)
    device, b_cnt = pcv.binary_threshold(b, 137, 255, 'light', device, debug)

    # Fill small objects
    # device, b_fill = pcv.fill(b_thresh, b_cnt, 10, device, args.debug)

    # Join the thresholded saturation and blue-yellow images
    device, bs = pcv.logical_and(s_mblur, b_cnt, device, debug)

    # Apply Mask (for vis images, mask_color=white)
    device, masked = pcv.apply_mask(img, bs, 'white', device, debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, masked_a = pcv.rgb2gray_lab(masked, 'a', device, debug)
    device, masked_b = pcv.rgb2gray_lab(masked, 'b', device, debug)

    # Threshold the green-magenta and blue images
    device, maskeda_thresh = pcv.binary_threshold(masked_a, 127, 255, 'dark', device, debug)
    device, maskedb_thresh = pcv.binary_threshold(masked_b, 128, 255, 'light', device, debug)

    # Join the thresholded saturation and blue-yellow images (OR)
    device, ab = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, debug)
    device, ab_cnt = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, debug)

    # Fill small noise
    device, ab_fill1 = pcv.fill(ab, ab_cnt, 200, device, debug)

    # Dilate to join small objects with larger ones
    device, ab_cnt1 = pcv.dilate(ab_fill1, 3, 2, device, debug)
    device, ab_cnt2 = pcv.dilate(ab_fill1, 3, 2, device, debug)

    # Fill dilated image mask
    device, ab_cnt3 = pcv.fill(ab_cnt2, ab_cnt1, 150, device, debug)
    device, masked2 = pcv.apply_mask(masked, ab_cnt3, 'white', device, debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, masked2_a = pcv.rgb2gray_lab(masked2, 'a', device, debug)
    device, masked2_b = pcv.rgb2gray_lab(masked2, 'b', device, debug)

    # Threshold the green-magenta and blue images
    device, masked2a_thresh = pcv.binary_threshold(masked2_a, 127, 255, 'dark', device, debug)
    device, masked2b_thresh = pcv.binary_threshold(masked2_b, 128, 255, 'light', device, debug)

    device, masked2a_thresh_blur = pcv.median_blur(masked2a_thresh, 5, device, debug)
    device, masked2b_thresh_blur = pcv.median_blur(masked2b_thresh, 13, device, debug)

    device, ab_fill = pcv.logical_or(masked2a_thresh_blur, masked2b_thresh_blur, device, debug)

    # Identify objects
    device, id_objects, obj_hierarchy = pcv.find_objects(masked2, ab_fill, device, debug)

    # Define ROI
    device, roi1, roi_hierarchy = pcv.define_roi(masked2, 'rectangle', device, None, 'default', debug, True, 700,
                                                 0, -600, -300)

    # Decide which objects to keep
    device, roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(img, 'partial', roi1, roi_hierarchy,
                                                                           id_objects, obj_hierarchy, device,
                                                                           debug)

    # Object combine kept objects
    device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3, device, debug)

    ############## VIS Analysis ################
    # Find shape properties, output shape image (optional)
    device, shape_header, shape_data, shape_img = pcv.analyze_object(img, vis_img, obj, mask, device, debug)

    # Shape properties relative to user boundary line (optional)
    device, boundary_header, boundary_data, boundary_img1 = pcv.analyze_bound(img, vis_img, obj, mask, 384, device,
                                                                              debug)

    # 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, vis_img, mask, 256, device, debug,
                                                                    None, 'v', 'img', 300)

    # Output shape and color data
    print('\t'.join(map(str, shape_header)) + '\n')
    print('\t'.join(map(str, shape_data)) + '\n')
    for row in shape_img:
        print('\t'.join(map(str, row)) + '\n')
    print('\t'.join(map(str, color_header)) + '\n')
    print('\t'.join(map(str, color_data)) + '\n')
    print('\t'.join(map(str, boundary_header)) + '\n')
    print('\t'.join(map(str, boundary_data)) + '\n')
    print('\t'.join(map(str, boundary_img1)) + '\n')
    for row in color_img:
        print('\t'.join(map(str, row)) + '\n')

    ############################# Use VIS image mask for NIR image#########################
    # Read NIR image
    nir, path1, filename1 = pcv.readimage(nir_img)
    nir2 = cv2.imread(nir_img, -1)

    # Flip mask
    device, f_mask = pcv.flip(mask, "vertical", device, debug)

    # Reize mask
    device, nmask = pcv.resize(f_mask, 0.1154905775, 0.1154905775, device, debug)

    # position, and crop mask
    device, newmask = pcv.crop_position_mask(nir, nmask, device, 30, 4, "top", "right", debug)

    # Identify objects
    device, nir_objects, nir_hierarchy = pcv.find_objects(nir, newmask, device, debug)

    # Object combine kept objects
    device, nir_combined, nir_combinedmask = pcv.object_composition(nir, nir_objects, nir_hierarchy, device, debug)

    ####################################### Analysis #############################################
    device, nhist_header, nhist_data, nir_imgs = pcv.analyze_NIR_intensity(nir2, filename1, nir_combinedmask, 256,
                                                                           device, False, debug)
    device, nshape_header, nshape_data, nir_shape = pcv.analyze_object(nir2, filename1, nir_combined, nir_combinedmask,
                                                                       device, debug)

    print('\t'.join(map(str, nhist_header)) + '\n')
    print('\t'.join(map(str, nhist_data)) + '\n')
    for row in nir_imgs:
        print('\t'.join(map(str, row)) + '\n')
    print('\t'.join(map(str, nshape_header)) + '\n')
    print('\t'.join(map(str, nshape_data)) + '\n')
    print('\t'.join(map(str, nir_shape)) + '\n')
Esempio n. 29
0
def test_plantcv_print_image():
    img, path, img_name = pcv.readimage(filename=os.path.join(TEST_DATA, TEST_INPUT_COLOR))
    filename = os.path.join(TEST_TMPDIR, 'plantcv_print_image.jpg')
    pcv.print_image(img=img, filename=filename)
    # Assert that the file was created
    assert os.path.exists(filename) is True
Esempio n. 30
0
def main():
    # Get options
    args = options()

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

    # Pipeline step
    device = 0

    # Convert RGB to HSV and extract the Saturation channel
    device, s = pcv.rgb2gray_hsv(img, 's', device, args.debug)

    # Threshold the Saturation image
    device, s_thresh = pcv.binary_threshold(s, 36, 255, 'light', device,
                                            args.debug)

    # Median Filter
    device, s_mblur = pcv.median_blur(s_thresh, 0, device, args.debug)
    device, s_cnt = pcv.median_blur(s_thresh, 0, device, args.debug)

    # Fill small objects
    #device, s_fill = pcv.fill(s_mblur, s_cnt, 0, device, args.debug)

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

    # Threshold the blue image
    device, b_thresh = pcv.binary_threshold(b, 137, 255, 'light', device,
                                            args.debug)
    device, b_cnt = pcv.binary_threshold(b, 137, 255, 'light', device,
                                         args.debug)

    # Fill small objects
    #device, b_fill = pcv.fill(b_thresh, b_cnt, 10, device, args.debug)

    # Join the thresholded saturation and blue-yellow images
    device, bs = pcv.logical_and(s_mblur, b_cnt, device, args.debug)

    # Apply Mask (for vis images, mask_color=white)
    device, masked = pcv.apply_mask(img, bs, 'white', device, args.debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, masked_a = pcv.rgb2gray_lab(masked, 'a', device, args.debug)
    device, masked_b = pcv.rgb2gray_lab(masked, 'b', device, args.debug)

    # Threshold the green-magenta and blue images
    device, maskeda_thresh = pcv.binary_threshold(masked_a, 127, 255, 'dark',
                                                  device, args.debug)
    device, maskedb_thresh = pcv.binary_threshold(masked_b, 128, 255, 'light',
                                                  device, args.debug)

    # Join the thresholded saturation and blue-yellow images (OR)
    device, ab = pcv.logical_or(maskeda_thresh, maskedb_thresh, device,
                                args.debug)
    device, ab_cnt = pcv.logical_or(maskeda_thresh, maskedb_thresh, device,
                                    args.debug)

    # Fill small noise
    device, ab_fill1 = pcv.fill(ab, ab_cnt, 2, device, args.debug)

    # Dilate to join small objects with larger ones
    device, ab_cnt1 = pcv.dilate(ab_fill1, 3, 2, device, args.debug)
    device, ab_cnt2 = pcv.dilate(ab_fill1, 3, 2, device, args.debug)

    # Fill dilated image mask
    device, ab_cnt3 = pcv.fill(ab_cnt2, ab_cnt1, 150, device, args.debug)
    img2 = np.copy(img)
    device, masked2 = pcv.apply_mask(img2, ab_cnt3, 'white', device,
                                     args.debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, masked2_a = pcv.rgb2gray_lab(masked2, 'a', device, args.debug)
    device, masked2_b = pcv.rgb2gray_lab(masked2, 'b', device, args.debug)

    # Threshold the green-magenta and blue images
    device, masked2a_thresh = pcv.binary_threshold(masked2_a, 127, 255, 'dark',
                                                   device, args.debug)
    device, masked2b_thresh = pcv.binary_threshold(masked2_b, 128, 255,
                                                   'light', device, args.debug)
    device, ab_fill = pcv.logical_or(masked2a_thresh, masked2b_thresh, device,
                                     args.debug)

    # Identify objects
    device, id_objects, obj_hierarchy = pcv.find_objects(
        masked2, ab_fill, device, args.debug)

    # Define ROI
    device, roi1, roi_hierarchy = pcv.define_roi(masked2, 'rectangle', device,
                                                 None, 'default', args.debug,
                                                 True, 550, 10, -600, -907)

    # Decide which objects to keep
    device, roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(
        img, 'partial', roi1, roi_hierarchy, id_objects, obj_hierarchy, device,
        args.debug)

    # Object combine kept objects
    device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3,
                                               device, args.debug)

    ############## Landmarks    ################

    device, points = pcv.acute_vertex(obj, 40, 40, 40, img, device, args.debug)
    boundary_line = 900
    # Use acute fxn to estimate tips
    device, points_r, centroid_r, bline_r = pcv.scale_features(
        obj, mask, points, boundary_line, device, args.debug)
    # Get number of points
    tips = len(points_r)
    # Use turgor_proxy fxn to get distances
    device, vert_ave_c, hori_ave_c, euc_ave_c, ang_ave_c, vert_ave_b, hori_ave_b, euc_ave_b, ang_ave_b = pcv.turgor_proxy(
        points_r, centroid_r, bline_r, device, args.debug)
    # Get pseudomarkers along the y-axis
    device, left, right, center_h = pcv.y_axis_pseudolandmarks(
        obj, mask, img, device, args.debug)
    # Re-scale the points
    device, left_r, left_cr, left_br = pcv.scale_features(
        obj, mask, left, boundary_line, device, args.debug)
    device, right_r, right_cr, right_br = pcv.scale_features(
        obj, mask, right, boundary_line, device, args.debug)
    device, center_hr, center_hcr, center_hbr = pcv.scale_features(
        obj, mask, center_h, boundary_line, device, args.debug)

    # Get pseudomarkers along the x-axis
    device, top, bottom, center_v = pcv.x_axis_pseudolandmarks(
        obj, mask, img, device, args.debug)

    # Re-scale the points
    device, top_r, top_cr, top_br = pcv.scale_features(obj, mask, top,
                                                       boundary_line, device,
                                                       args.debug)
    device, bottom_r, bottom_cr, bottom_br = pcv.scale_features(
        obj, mask, bottom, boundary_line, device, args.debug)
    device, center_vr, center_vcr, center_vbr = pcv.scale_features(
        obj, mask, center_v, boundary_line, device, args.debug)

    ## Need to convert the points into a list of tuples format to match the scaled points
    points = points.reshape(len(points), 2)
    points = points.tolist()
    temp_out = []
    for p in points:
        p = tuple(p)
        temp_out.append(p)
    points = temp_out
    left = left.reshape(20, 2)
    left = left.tolist()
    temp_out = []
    for l in left:
        l = tuple(l)
        temp_out.append(l)
    left = temp_out
    right = right.reshape(20, 2)
    right = right.tolist()
    temp_out = []
    for r in right:
        r = tuple(r)
        temp_out.append(r)
    right = temp_out
    center_h = center_h.reshape(20, 2)
    center_h = center_h.tolist()
    temp_out = []
    for ch in center_h:
        ch = tuple(ch)
        temp_out.append(ch)
    center_h = temp_out
    ## Need to convert the points into a list of tuples format to match the scaled points
    top = top.reshape(20, 2)
    top = top.tolist()
    temp_out = []
    for t in top:
        t = tuple(t)
        temp_out.append(t)
    top = temp_out
    bottom = bottom.reshape(20, 2)
    bottom = bottom.tolist()
    temp_out = []
    for b in bottom:
        b = tuple(b)
        temp_out.append(b)
    bottom = temp_out
    center_v = center_v.reshape(20, 2)
    center_v = center_v.tolist()
    temp_out = []
    for cvr in center_v:
        cvr = tuple(cvr)
        temp_out.append(cvr)
    center_v = temp_out

    #Store Landmark Data
    landmark_header = ('HEADER_LANDMARK', 'tip_points', 'tip_points_r',
                       'centroid_r', 'baseline_r', 'tip_number', 'vert_ave_c',
                       'hori_ave_c', 'euc_ave_c', 'ang_ave_c', 'vert_ave_b',
                       'hori_ave_b', 'euc_ave_b', 'ang_ave_b', 'left_lmk',
                       'right_lmk', 'center_h_lmk', 'left_lmk_r',
                       'right_lmk_r', 'center_h_lmk_r', 'top_lmk',
                       'bottom_lmk', 'center_v_lmk', 'top_lmk_r',
                       'bottom_lmk_r', 'center_v_lmk_r')

    landmark_data = ('LANDMARK_DATA', points, points_r, centroid_r, bline_r,
                     tips, vert_ave_c, hori_ave_c, euc_ave_c, ang_ave_c,
                     vert_ave_b, hori_ave_b, euc_ave_b, ang_ave_b, left, right,
                     center_h, left_r, right_r, center_hr, top, bottom,
                     center_v, top_r, bottom_r, center_vr)

    ############## VIS Analysis ################

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

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

    # Shape properties relative to user boundary line (optional)
    device, boundary_header, boundary_data, boundary_img1 = pcv.analyze_bound(
        img, args.image, obj, mask, 935, device, args.debug, 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, args.image, mask, 256, device, args.debug, None, 'v', 'img', 300,
        outfile)

    # Output shape and color data

    result = open(args.result, "a")
    result.write('\t'.join(map(str, shape_header)))
    result.write("\n")
    result.write('\t'.join(map(str, shape_data)))
    result.write("\n")
    for row in shape_img:
        result.write('\t'.join(map(str, row)))
        result.write("\n")
    result.write('\t'.join(map(str, color_header)))
    result.write("\n")
    result.write('\t'.join(map(str, color_data)))
    result.write("\n")
    result.write('\t'.join(map(str, boundary_header)))
    result.write("\n")
    result.write('\t'.join(map(str, boundary_data)))
    result.write("\n")
    result.write('\t'.join(map(str, boundary_img1)))
    result.write("\n")
    for row in color_img:
        result.write('\t'.join(map(str, row)))
        result.write("\n")
    result.write('\t'.join(map(str, landmark_header)))
    result.write("\n")
    result.write('\t'.join(map(str, landmark_data)))
    result.write("\n")
    result.close()
def process_tv_images(vis_img, nir_img, debug=False):
    """Process top-view images.

    Inputs:
    vis_img = An RGB image.
    nir_img = An NIR grayscale image.
    debug   = None, print, or plot. Print = save to file, Plot = print to screen.

    :param vis_img: str
    :param nir_img: str
    :param debug: str
    :return:
    """
    # Read image
    img, path, filename = pcv.readimage(vis_img)
    brass_mask = cv2.imread('mask_brass_tv_z1_L1.png')

    device = 0

    # Convert RGB to HSV and extract the Saturation channel
    device, s = pcv.rgb2gray_hsv(img, 's', device, debug)

    # Threshold the Saturation image
    device, s_thresh = pcv.binary_threshold(s, 75, 255, 'light', device, debug)

    # Median Filter
    device, s_mblur = pcv.median_blur(s_thresh, 5, device, debug)
    device, s_cnt = pcv.median_blur(s_thresh, 5, device, debug)

    # Fill small objects
    device, s_fill = pcv.fill(s_mblur, s_cnt, 150, device, debug)

    # Convert RGB to LAB and extract the Blue channel
    device, b = pcv.rgb2gray_lab(img, 'b', device, debug)

    # Threshold the blue image
    device, b_thresh = pcv.binary_threshold(b, 138, 255, 'light', device, debug)
    device, b_cnt = pcv.binary_threshold(b, 138, 255, 'light', device, debug)

    # Fill small objects
    device, b_fill = pcv.fill(b_thresh, b_cnt, 100, device, debug)

    # Join the thresholded saturation and blue-yellow images
    device, bs = pcv.logical_and(s_fill, b_fill, device, debug)

    # Apply Mask (for vis images, mask_color=white)
    device, masked = pcv.apply_mask(img, bs, 'white', device, debug)

    # Mask pesky brass piece
    device, brass_mask1 = pcv.rgb2gray_hsv(brass_mask, 'v', device, debug)
    device, brass_thresh = pcv.binary_threshold(brass_mask1, 0, 255, 'light', device, debug)
    device, brass_inv = pcv.invert(brass_thresh, device, debug)
    device, brass_masked = pcv.apply_mask(masked, brass_inv, 'white', device, debug)

    # Further mask soil and car
    device, masked_a = pcv.rgb2gray_lab(brass_masked, 'a', device, debug)
    device, soil_car1 = pcv.binary_threshold(masked_a, 128, 255, 'dark', device, debug)
    device, soil_car2 = pcv.binary_threshold(masked_a, 128, 255, 'light', device, debug)
    device, soil_car = pcv.logical_or(soil_car1, soil_car2, device, debug)
    device, soil_masked = pcv.apply_mask(brass_masked, soil_car, 'white', device, debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, soil_a = pcv.rgb2gray_lab(soil_masked, 'a', device, debug)
    device, soil_b = pcv.rgb2gray_lab(soil_masked, 'b', device, debug)

    # Threshold the green-magenta and blue images
    device, soila_thresh = pcv.binary_threshold(soil_a, 124, 255, 'dark', device, debug)
    device, soilb_thresh = pcv.binary_threshold(soil_b, 148, 255, 'light', device, debug)

    # Join the thresholded saturation and blue-yellow images (OR)
    device, soil_ab = pcv.logical_or(soila_thresh, soilb_thresh, device, debug)
    device, soil_ab_cnt = pcv.logical_or(soila_thresh, soilb_thresh, device, debug)

    # Fill small objects
    device, soil_cnt = pcv.fill(soil_ab, soil_ab_cnt, 300, device, debug)

    # Apply mask (for vis images, mask_color=white)
    device, masked2 = pcv.apply_mask(soil_masked, soil_cnt, 'white', device, debug)

    # Identify objects
    device, id_objects, obj_hierarchy = pcv.find_objects(masked2, soil_cnt, device, debug)

    # Define ROI
    device, roi1, roi_hierarchy = pcv.define_roi(img, 'rectangle', device, None, 'default', debug, True, 600, 450, -600,
                                                 -350)

    # Decide which objects to keep
    device, roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(img, 'partial', roi1, roi_hierarchy,
                                                                           id_objects, obj_hierarchy, device, debug)

    # Object combine kept objects
    device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3, device, debug)

    # Find shape properties, output shape image (optional)
    device, shape_header, shape_data, shape_img = pcv.analyze_object(img, vis_img, obj, mask, device, debug)

    # Determine color properties
    device, color_header, color_data, color_img = pcv.analyze_color(img, vis_img, mask, 256, device, debug, None,
                                                                    'v', 'img', 300)

    print('\t'.join(map(str, shape_header)) + '\n')
    print('\t'.join(map(str, shape_data)) + '\n')
    for row in shape_img:
        print('\t'.join(map(str, row)) + '\n')
    print('\t'.join(map(str, color_header)) + '\n')
    print('\t'.join(map(str, color_data)) + '\n')
    for row in color_img:
        print('\t'.join(map(str, row)) + '\n')

    ############################# Use VIS image mask for NIR image#########################
    # Read NIR image
    nir, path1, filename1 = pcv.readimage(nir_img)
    nir2 = cv2.imread(nir_img, -1)

    # Flip mask
    device, f_mask = pcv.flip(mask, "horizontal", device, debug)

    # Reize mask
    device, nmask = pcv.resize(f_mask, 0.116148, 0.116148, device, debug)

    # position, and crop mask
    device, newmask = pcv.crop_position_mask(nir, nmask, device, 15, 5, "top", "right", debug)

    # Identify objects
    device, nir_objects, nir_hierarchy = pcv.find_objects(nir, newmask, device, debug)

    # Object combine kept objects
    device, nir_combined, nir_combinedmask = pcv.object_composition(nir, nir_objects, nir_hierarchy, device, debug)

    ####################################### Analysis #############################################

    device, nhist_header, nhist_data, nir_imgs = pcv.analyze_NIR_intensity(nir2, filename1, nir_combinedmask, 256,
                                                                           device, False, debug)
    device, nshape_header, nshape_data, nir_shape = pcv.analyze_object(nir2, filename1, nir_combined, nir_combinedmask,
                                                                       device, debug)

    print('\t'.join(map(str,nhist_header)) + '\n')
    print('\t'.join(map(str,nhist_data)) + '\n')
    for row in nir_imgs:
      print('\t'.join(map(str,row)) + '\n')
    print('\t'.join(map(str,nshape_header)) + '\n')
    print('\t'.join(map(str,nshape_data)) + '\n')
    print('\t'.join(map(str,nir_shape)) + '\n')
Esempio n. 32
0
def main():
  # Get options
  args = options()
  
  # Read image
  img, path, filename = pcv.readimage(args.image)
  brass_mask = cv2.imread(args.roi)
  
  # Pipeline step
  device = 0

  # Convert RGB to HSV and extract the Saturation channel
  device, s = pcv.rgb2gray_hsv(img, 's', device, args.debug)
  
  # Threshold the Saturation image
  device, s_thresh = pcv.binary_threshold(s, 49, 255, 'light', device, args.debug)
  
   #Median Filter
  device, s_mblur = pcv.median_blur(s_thresh, 5, device, args.debug)
  
   #Apply Mask (for vis images, mask_color=white)
  device, masked = pcv.apply_mask(img, s_mblur, 'white', device, args.debug)
  
#   Convert RGB to LAB and extract the Green-Magenta 
  device, soil_a = pcv.rgb2gray_lab(masked, 'a', device, args.debug)
#  
#   Threshold the green-magenta 
  device, soila_thresh = pcv.binary_threshold(soil_a, 133, 255, 'light', device, args.debug)
  device, soila_cnt = pcv.binary_threshold(soil_a, 133, 255, 'light', device, args.debug)

#
#   Fill small objects
  device, soil_fill = pcv.fill(soila_thresh, soila_cnt, 200, device, args.debug)
#
#   Median Filter
  device, soil_mblur = pcv.median_blur(soil_fill, 13, device, args.debug)
  device, soil_cnt = pcv.median_blur(soil_fill, 13, device, args.debug)
#  
#   Apply mask (for vis images, mask_color=white)
  device, masked2 = pcv.apply_mask(soil_mblur, soil_cnt, 'white', device, args.debug)
#  
#   Identify objects
  device, id_objects,obj_hierarchy = pcv.find_objects(masked2, soil_cnt, device, args.debug)
#
#   Define ROI
  device, roi1, roi_hierarchy= pcv.define_roi(img,'rectangle', device, None, 'default', args.debug,True, 400,400,-400,-400)
#  
#   Decide which objects to keep
  device,roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(img,'partial',roi1,roi_hierarchy,id_objects,obj_hierarchy,device, args.debug)
#  
#   Object combine kept objects
  device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3, device, args.debug)
#  
############## Analysis ################  
  
  # Find shape properties, output shape image (optional)
  device, shape_header,shape_data,shape_img = pcv.analyze_object(img, args.image, obj, mask, device,args.debug,args.outdir+'/'+filename)
   
  # Determine color properties: Histograms, Color Slices and Pseudocolored Images, output color analyzed images (optional)
  device, color_header,color_data,norm_slice= pcv.analyze_color(img, args.image, mask, 256, device, args.debug,'all','rgb','v','img',300,args.outdir+'/'+filename)
  
  # Output shape and color data
  pcv.print_results(args.image, shape_header, shape_data)
  pcv.print_results(args.image, color_header, color_data)
Esempio n. 33
0
def main():
    # Get options
    args = options()

    # Read image
    img, path, filename = pcv.readimage(args.image)
    # roi = cv2.imread(args.roi)

    # Pipeline step
    device = 0

    # Convert RGB to HSV and extract the Saturation channel
    device, s = pcv.rgb2gray_hsv(img, "s", device, args.debug)

    # Threshold the Saturation image
    device, s_thresh = pcv.binary_threshold(s, 36, 255, "light", device, args.debug)

    # Median Filter
    device, s_mblur = pcv.median_blur(s_thresh, 0, device, args.debug)
    device, s_cnt = pcv.median_blur(s_thresh, 0, device, args.debug)

    # Fill small objects
    # device, s_fill = pcv.fill(s_mblur, s_cnt, 0, device, args.debug)

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

    # Threshold the blue image
    device, b_thresh = pcv.binary_threshold(b, 137, 255, "light", device, args.debug)
    device, b_cnt = pcv.binary_threshold(b, 137, 255, "light", device, args.debug)

    # Fill small objects
    # device, b_fill = pcv.fill(b_thresh, b_cnt, 10, device, args.debug)

    # Join the thresholded saturation and blue-yellow images
    device, bs = pcv.logical_and(s_mblur, b_cnt, device, args.debug)

    # Apply Mask (for vis images, mask_color=white)
    device, masked = pcv.apply_mask(img, bs, "white", device, args.debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, masked_a = pcv.rgb2gray_lab(masked, "a", device, args.debug)
    device, masked_b = pcv.rgb2gray_lab(masked, "b", device, args.debug)

    # Threshold the green-magenta and blue images
    device, maskeda_thresh = pcv.binary_threshold(masked_a, 127, 255, "dark", device, args.debug)
    device, maskedb_thresh = pcv.binary_threshold(masked_b, 128, 255, "light", device, args.debug)

    # Join the thresholded saturation and blue-yellow images (OR)
    device, ab = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, args.debug)
    device, ab_cnt = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, args.debug)

    # Fill small noise
    device, ab_fill1 = pcv.fill(ab, ab_cnt, 2, device, args.debug)

    # Dilate to join small objects with larger ones
    device, ab_cnt1 = pcv.dilate(ab_fill1, 3, 2, device, args.debug)
    device, ab_cnt2 = pcv.dilate(ab_fill1, 3, 2, device, args.debug)

    # Fill dilated image mask
    device, ab_cnt3 = pcv.fill(ab_cnt2, ab_cnt1, 150, device, args.debug)
    device, masked2 = pcv.apply_mask(masked, ab_cnt3, "white", device, args.debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, masked2_a = pcv.rgb2gray_lab(masked2, "a", device, args.debug)
    device, masked2_b = pcv.rgb2gray_lab(masked2, "b", device, args.debug)

    # Threshold the green-magenta and blue images
    device, masked2a_thresh = pcv.binary_threshold(masked2_a, 127, 255, "dark", device, args.debug)
    device, masked2b_thresh = pcv.binary_threshold(masked2_b, 128, 255, "light", device, args.debug)
    device, ab_fill = pcv.logical_or(masked2a_thresh, masked2b_thresh, device, args.debug)

    # Identify objects
    device, id_objects, obj_hierarchy = pcv.find_objects(masked2, ab_fill, device, args.debug)

    # Define ROI
    device, roi1, roi_hierarchy = pcv.define_roi(
        masked2, "rectangle", device, None, "default", args.debug, True, 550, 0, -600, -925
    )

    # Decide which objects to keep
    device, roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(
        img, "partial", roi1, roi_hierarchy, id_objects, obj_hierarchy, device, args.debug
    )

    # Object combine kept objects
    device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3, device, args.debug)

    ############### Analysis ################

    # Find shape properties, output shape image (optional)
    device, shape_header, shape_data, shape_img = pcv.analyze_object(
        img, args.image, obj, mask, device, args.debug, args.outdir + "/" + filename
    )

    # Shape properties relative to user boundary line (optional)
    device, boundary_header, boundary_data, boundary_img1 = pcv.analyze_bound(
        img, args.image, obj, mask, 900, device, args.debug, args.outdir + "/" + filename
    )

    # 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, args.image, kept_mask, 256, device, args.debug, None, "v", "img", 300, args.outdir + "/" + filename
    )

    # Output shape and color data
    pcv.print_results(args.image, shape_header, shape_data)
    pcv.print_results(args.image, color_header, color_data)
    pcv.print_results(args.image, boundary_header, boundary_data)
def main():
    # Get options
    args = options()

    # Read image
    img, path, filename = pcv.readimage(args.image)
    brass_mask = cv2.imread(args.roi)

    # Pipeline step
    device = 0

    # Convert RGB to HSV and extract the Saturation channel
    device, s = pcv.rgb2gray_hsv(img, "s", device, args.debug)

    # Threshold the Saturation image
    device, s_thresh = pcv.binary_threshold(s, 49, 255, "light", device, args.debug)

    # Median Filter
    device, s_mblur = pcv.median_blur(s_thresh, 5, device, args.debug)
    device, s_cnt = pcv.median_blur(s_thresh, 5, device, args.debug)

    # Fill small objects
    device, s_fill = pcv.fill(s_mblur, s_cnt, 150, device, args.debug)

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

    # Threshold the blue image
    device, b_thresh = pcv.binary_threshold(b, 138, 255, "light", device, args.debug)
    device, b_cnt = pcv.binary_threshold(b, 138, 255, "light", device, args.debug)

    # Fill small objects
    device, b_fill = pcv.fill(b_thresh, b_cnt, 150, device, args.debug)

    # Join the thresholded saturation and blue-yellow images
    device, bs = pcv.logical_and(s_fill, b_fill, device, args.debug)

    # Apply Mask (for vis images, mask_color=white)
    device, masked = pcv.apply_mask(img, bs, "white", device, args.debug)

    # Mask pesky brass piece
    device, brass_mask1 = pcv.rgb2gray_hsv(brass_mask, "v", device, args.debug)
    device, brass_thresh = pcv.binary_threshold(brass_mask1, 0, 255, "light", device, args.debug)
    device, brass_inv = pcv.invert(brass_thresh, device, args.debug)
    device, brass_masked = pcv.apply_mask(masked, brass_inv, "white", device, args.debug)

    # Further mask soil and car
    device, masked_a = pcv.rgb2gray_lab(brass_masked, "a", device, args.debug)
    device, soil_car = pcv.binary_threshold(masked_a, 128, 255, "dark", device, args.debug)
    device, soil_masked = pcv.apply_mask(brass_masked, soil_car, "white", device, args.debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, soil_a = pcv.rgb2gray_lab(soil_masked, "a", device, args.debug)
    device, soil_b = pcv.rgb2gray_lab(soil_masked, "b", device, args.debug)

    # Threshold the green-magenta and blue images
    device, soila_thresh = pcv.binary_threshold(soil_a, 118, 255, "dark", device, args.debug)
    device, soilb_thresh = pcv.binary_threshold(soil_b, 150, 255, "light", device, args.debug)

    # Join the thresholded saturation and blue-yellow images (OR)
    device, soil_ab = pcv.logical_or(soila_thresh, soilb_thresh, device, args.debug)
    device, soil_ab_cnt = pcv.logical_or(soila_thresh, soilb_thresh, device, args.debug)

    # Fill small objects
    device, soil_cnt = pcv.fill(soil_ab, soil_ab_cnt, 75, device, args.debug)

    # Median Filter
    # device, soil_mblur = pcv.median_blur(soil_fill, 5, device, args.debug)
    # device, soil_cnt = pcv.median_blur(soil_fill, 5, device, args.debug)

    # Apply mask (for vis images, mask_color=white)
    device, masked2 = pcv.apply_mask(soil_masked, soil_cnt, "white", device, args.debug)

    # Identify objects
    device, id_objects, obj_hierarchy = pcv.find_objects(masked2, soil_cnt, device, args.debug)

    # Define ROI
    device, roi1, roi_hierarchy = pcv.define_roi(
        img, "circle", device, None, "default", args.debug, True, 0, 0, -200, -200
    )

    # Decide which objects to keep
    device, roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(
        img, "partial", roi1, roi_hierarchy, id_objects, obj_hierarchy, device, args.debug
    )

    # Object combine kept objects
    device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3, device, args.debug)

    ############## VIS Analysis ################

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

    # Find shape properties, output shape image (optional)
    device, shape_header, shape_data, shape_img = pcv.analyze_object(
        img, args.image, obj, mask, device, args.debug, 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, args.image, mask, 256, device, args.debug, None, "v", "img", 300, outfile
    )

    # Output shape and color data

    result = open(args.result, "a")
    result.write("\t".join(map(str, shape_header)))
    result.write("\n")
    result.write("\t".join(map(str, shape_data)))
    result.write("\n")
    for row in shape_img:
        result.write("\t".join(map(str, row)))
        result.write("\n")
    result.write("\t".join(map(str, color_header)))
    result.write("\n")
    result.write("\t".join(map(str, color_data)))
    result.write("\n")
    for row in color_img:
        result.write("\t".join(map(str, row)))
        result.write("\n")
    result.close()

    ############################# Use VIS image mask for NIR image#########################
    # Find matching NIR image
    device, nirpath = pcv.get_nir(path, filename, device, args.debug)
    nir, path1, filename1 = pcv.readimage(nirpath)
    nir2 = cv2.imread(nirpath, -1)

    # Flip mask
    device, f_mask = pcv.flip(mask, "horizontal", device, args.debug)

    # Reize mask
    device, nmask = pcv.resize(f_mask, 0.1304, 0.1304, device, args.debug)

    # position, and crop mask
    device, newmask = pcv.crop_position_mask(nir, nmask, device, 9, 12, "top", "left", args.debug)

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

    # Object combine kept objects
    device, nir_combined, nir_combinedmask = pcv.object_composition(nir, nir_objects, nir_hierarchy, device, args.debug)

    ####################################### Analysis #############################################
    outfile1 = False
    if args.writeimg == True:
        outfile1 = args.outdir + "/" + filename1

    device, nhist_header, nhist_data, nir_imgs = pcv.analyze_NIR_intensity(
        nir2, filename1, nir_combinedmask, 256, device, False, args.debug, outfile1
    )
    device, nshape_header, nshape_data, nir_shape = pcv.analyze_object(
        nir2, filename1, nir_combined, nir_combinedmask, device, args.debug, outfile1
    )

    coresult = open(args.coresult, "a")
    coresult.write("\t".join(map(str, nhist_header)))
    coresult.write("\n")
    coresult.write("\t".join(map(str, nhist_data)))
    coresult.write("\n")
    for row in nir_imgs:
        coresult.write("\t".join(map(str, row)))
        coresult.write("\n")

    coresult.write("\t".join(map(str, nshape_header)))
    coresult.write("\n")
    coresult.write("\t".join(map(str, nshape_data)))
    coresult.write("\n")
    coresult.write("\t".join(map(str, nir_shape)))
    coresult.write("\n")
    coresult.close()
Esempio n. 35
0
def main():
    # Get options
    args = options()

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

    # Pipeline step
    device = 0

    debug = args.debug

    # print('Original image')
    # pcv.plot_image(img)

    # Convert RGB to HSV and extract the Saturation channel
    device, s = pcv.rgb2gray_hsv(img, 's', device, debug)
    # print('Convert RGB to HSV and extract the Saturation channel')
    # plt.imshow(s)
    # plt.show()

    # Threshold the Saturation image
    device, s_thresh = pcv.binary_threshold(s, 100, 255, 'light', device,
                                            debug)
    # print('Threshold the Saturation image')
    # plt.imshow(s_thresh)
    # plt.show()
    #
    # Median Filter
    device, s_mblur = pcv.median_blur(s_thresh, 5, device, debug)
    device, s_cnt = pcv.median_blur(s_thresh, 5, device, debug)
    # print('Median Filter')
    # plt.imshow(s_mblur)
    # plt.show()
    #
    # Convert RGB to LAB and extract the Blue channel
    device, b = pcv.rgb2gray_lab(img, 'b', device, debug)
    # print('Convert RGB to LAB and extract the Blue channel')
    # plt.imshow(b)
    # plt.show()

    # Threshold the blue image
    device, b_thresh = pcv.binary_threshold(b, 160, 255, 'light', device,
                                            debug)
    device, b_cnt = pcv.binary_threshold(b, 160, 255, 'light', device, debug)
    # print('Threshold the blue image')
    # plt.imshow(b_cnt)
    # plt.show()
    # Fill small objects
    #device, b_fill = pcv.fill(b_thresh, b_cnt, 10, device, debug)
    #

    # Join the thresholded saturation and blue-yellow images
    device, bs = pcv.logical_or(s_mblur, b_cnt, device, debug)

    # print('Join the thresholded saturation and blue-yellow images')
    # plt.imshow(bs)
    # plt.show()
    #
    # Apply Mask (for vis images, mask_color=white)
    device, masked = pcv.apply_mask(img, bs, 'white', device, debug)
    # print('Apply Mask 1 (for vis images, mask_color=white)')
    # plt.imshow(masked)
    # plt.show()
    #
    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, masked_a = pcv.rgb2gray_lab(masked, 'a', device, debug)
    device, masked_b = pcv.rgb2gray_lab(masked, 'b', device, debug)

    # Threshold the green-magenta and blue images
    device, maskeda_thresh = pcv.binary_threshold(masked_a, 115, 255, 'dark',
                                                  device, debug)
    device, maskeda_thresh1 = pcv.binary_threshold(masked_a, 135, 255, 'light',
                                                   device, debug)
    device, maskedb_thresh = pcv.binary_threshold(masked_b, 128, 255, 'light',
                                                  device, debug)

    # Join the thresholded saturation and blue-yellow images (OR)
    device, ab1 = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, debug)
    device, ab = pcv.logical_or(maskeda_thresh1, ab1, device, debug)
    device, ab_cnt = pcv.logical_or(maskeda_thresh1, ab1, device, debug)

    # Fill small objects
    device, ab_fill = pcv.fill(ab, ab_cnt, 200, device, debug)

    # Apply mask (for vis images, mask_color=white)
    device, masked2 = pcv.apply_mask(masked, ab_fill, 'white', device, debug)
    # print('Apply Mask 2 (for vis images, mask_color=white)')
    # plt.imshow(masked2)
    # plt.show()
    #
    #Identify objects
    device, id_objects, obj_hierarchy = pcv.find_objects(
        masked2, ab_fill, device, debug)

    #
    # Define ROI
    # device, roi1, roi_hierarchy= pcv.define_roi(masked2, 'rectangle', device, None, 'default', debug, True, 67, 377, -125, -368)
    device, roi1, roi_hierarchy = pcv.define_roi(masked2, 'rectangle', device,
                                                 None, 'default', debug, True,
                                                 1, 1, -1, -1)
    #
    # Decide which objects to keep
    device, roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(
        img, 'partial', roi1, roi_hierarchy, id_objects, obj_hierarchy, device,
        debug)
    #
    # Object combine kept objects
    device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3,
                                               device, debug)

    ############### Analysis ################

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

    # Find shape properties, output shape image (optional)
    device, shape_header, shape_data, shape_img = pcv.analyze_object(
        img, args.image, obj, mask, device, debug,
        args.outdir + '/' + filename)

    # Shape properties relative to user boundary line (optional)
    device, boundary_header, boundary_data, boundary_img1 = pcv.analyze_bound(
        img, args.image, obj, mask, 1680, device, debug,
        args.outdir + '/' + filename)

    # 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, args.image, kept_mask, 256, device, debug, 'all', 'v', 'img', 300,
        args.outdir + '/' + filename)

    # Write shape and color data to results file
    result = open(args.result, "a")
    result.write('\t'.join(map(str, shape_header)))
    result.write("\n")
    result.write('\t'.join(map(str, shape_data)))
    result.write("\n")
    for row in shape_img:
        result.write('\t'.join(map(str, row)))
        result.write("\n")
    result.write('\t'.join(map(str, color_header)))
    result.write("\n")
    result.write('\t'.join(map(str, color_data)))
    result.write("\n")
    for row in color_img:
        result.write('\t'.join(map(str, row)))
        result.write("\n")
    result.close()
Esempio n. 36
0
def main():
  # Get options
  args = options()
  path_mask = '/home/mfeldman/tester/mask/mask_brass_tv_z1_L0.png'
  
  # Read image
  img, path, filename = pcv.readimage(args.image)
  brass_mask = cv2.imread(path_mask)
  
  # Pipeline step
  device = 0

  # Mask pesky brass piece
  device, brass_mask1 = pcv.rgb2gray_hsv(brass_mask, 'v', device, args.debug)
  device, brass_thresh = pcv.binary_threshold(brass_mask1, 0, 255, 'light', device, args.debug)
  device, brass_inv=pcv.invert(brass_thresh, device, args.debug)
  device, masked_image = pcv.apply_mask(img, brass_inv, 'white', device, args.debug)
  
  # We can do a pretty good job of identifying the plant from the s channel
  device, s = pcv.rgb2gray_hsv(masked_image, 's', device, args.debug)
  s_thresh = cv2.inRange(s, 100, 190)

  # Lets blur the result a bit to get rid of unwanted noise
  s_blur = cv2.medianBlur(s_thresh,5)
  
  # The a channel is good too
  device, a = pcv.rgb2gray_lab(masked_image, 'a', device, args.debug)
  a_thresh = cv2.inRange(a, 90, 120)
  a_blur = cv2.medianBlur(a_thresh,5)
  
  # Now lets set of a series of filters to remove unwanted background
  plant_shape = cv2.bitwise_and(a_blur, s_blur)
  
  # Lets remove all the crap on the sides of the image
  plant_shape[:,:330] = 0
  plant_shape[:,2100:] = 0
  plant_shape[:200,:] = 0
  
  # Now remove all remaining small points using erosion with a 3 x 3 kernel
  kernel = np.ones((3,3),np.uint8)
  erosion = cv2.erode(plant_shape ,kernel,iterations = 1)
  
  # Now dilate to fill in small holes
  kernel = np.ones((3,3),np.uint8)
  dilation = cv2.dilate(erosion ,kernel,iterations = 1)
  
  # Apply mask to the background image
  device, masked = pcv.apply_mask(masked_image, plant_shape, 'white', device, args.debug)
  
  # Identify objects
  device, id_objects, obj_hierarchy = pcv.find_objects(masked, dilation, device, args.debug)
  
  # Get ROI contours
  device, roi, roi_hierarchy = pcv.define_roi(masked_image, 'circle', device, None, 'default', args.debug, True, x_adj=0, y_adj=0, w_adj=0, h_adj=-1200)
  
  # ROI
  device,roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(masked_image,'partial',roi, roi_hierarchy, id_objects,obj_hierarchy,device, args.debug)
  
  # Get object contour and masked object
  device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3, device, args.debug)
  
  ############## Landmarks    ################
  
  device, points = pcv.acute_vertex(obj, 40, 40, 40, img, device, args.debug)
  boundary_line = 'NA'
  # Use acute fxn to estimate tips
  device, points_r, centroid_r, bline_r = pcv.scale_features(obj, mask, points, boundary_line, device, args.debug)
    # Get number of points
  tips = len(points_r)
  # Use turgor_proxy fxn to get distances 
  device, vert_ave_c, hori_ave_c, euc_ave_c, ang_ave_c, vert_ave_b, hori_ave_b, euc_ave_b, ang_ave_b = pcv.turgor_proxy(points_r, centroid_r, bline_r, device, args.debug)
  # Get pseudomarkers along the y-axis
  device, left, right, center_h = pcv.y_axis_pseudolandmarks(obj, mask, img, device, args.debug)
  # Re-scale the points
  device, left_r, left_cr, left_br = pcv.scale_features(obj, mask, left, boundary_line, device, args.debug)
  device, right_r, right_cr, right_br = pcv.scale_features(obj, mask, right, boundary_line, device, args.debug)
  device, center_hr, center_hcr, center_hbr = pcv.scale_features(obj, mask, center_h, boundary_line, device, args.debug)
  
  # Get pseudomarkers along the x-axis
  device, top, bottom, center_v = pcv.x_axis_pseudolandmarks(obj, mask, img, device, args.debug)
  
  # Re-scale the points
  device, top_r, top_cr, top_br = pcv.scale_features(obj, mask, top, boundary_line, device, args.debug)
  device, bottom_r, bottom_cr, bottom_br = pcv.scale_features(obj, mask, bottom, boundary_line, device, args.debug)
  device, center_vr, center_vcr, center_vbr = pcv.scale_features(obj, mask, center_v, boundary_line, device, args.debug)
  
  ## Need to convert the points into a list of tuples format to match the scaled points
  points = points.reshape(len(points),2)
  points = points.tolist()
  temp_out = []
  for p in points:
    p = tuple(p)
    temp_out.append(p)
  points = temp_out
  left = left.reshape(20,2)
  left = left.tolist()
  temp_out = []
  for l in left:
    l = tuple(l)
    temp_out.append(l)
  left = temp_out
  right = right.reshape(20,2)
  right = right.tolist()
  temp_out = []
  for r in right:
    r = tuple(r)
    temp_out.append(r)
  right = temp_out
  center_h = center_h.reshape(20,2)
  center_h = center_h.tolist()
  temp_out = []
  for ch in center_h:
    ch = tuple(ch)
    temp_out.append(ch)
  center_h = temp_out
  ## Need to convert the points into a list of tuples format to match the scaled points
  top = top.reshape(20,2)
  top = top.tolist()
  temp_out = []
  for t in top:
    t = tuple(t)
    temp_out.append(t)
  top = temp_out
  bottom = bottom.reshape(20,2)
  bottom = bottom.tolist()
  temp_out = []
  for b in bottom:
    b = tuple(b)
    temp_out.append(b)
  bottom = temp_out
  center_v = center_v.reshape(20,2)
  center_v = center_v.tolist()
  temp_out = []
  for cvr in center_v:
    cvr = tuple(cvr)
    temp_out.append(cvr)
  center_v = temp_out
  
  #Store Landmark Data
  landmark_header=(
    'HEADER_LANDMARK',
    'tip_points',
    'tip_points_r',
    'centroid_r',
    'baseline_r',
    'tip_number',
    'vert_ave_c',
    'hori_ave_c',
    'euc_ave_c',
    'ang_ave_c',
    'vert_ave_b',
    'hori_ave_b',
    'euc_ave_b',
    'ang_ave_b',
    'left_lmk',
    'right_lmk',
    'center_h_lmk',
    'left_lmk_r',
    'right_lmk_r',
    'center_h_lmk_r',
    'top_lmk',
    'bottom_lmk',
    'center_v_lmk',
    'top_lmk_r',
    'bottom_lmk_r',
    'center_v_lmk_r'
    )

  landmark_data = (
    'LANDMARK_DATA',
    points,
    points_r,
    centroid_r,
    bline_r,
    tips,
    vert_ave_c,
    hori_ave_c,
    euc_ave_c,
    ang_ave_c,
    vert_ave_b,
    hori_ave_b,
    euc_ave_b,
    ang_ave_b,
    left,
    right,
    center_h,
    left_r,
    right_r,
    center_hr,
    top,
    bottom,
    center_v,
    top_r,
    bottom_r,
    center_vr
    )
    
  
  
 ############## VIS Analysis ################
  
  outfile=False
  #if args.writeimg==True:
  #  outfile=args.outdir+"/"+filename
  
  # Find shape properties, output shape image (optional)
  device, shape_header,shape_data,shape_img = pcv.analyze_object(img, args.image, obj, mask, device,args.debug,outfile)
  
  # Shape properties relative to user boundary line (optional)
  device, boundary_header,boundary_data, boundary_img1= pcv.analyze_bound(img, args.image,obj, mask, 330, device,args.debug,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, args.image, mask, 256, device, args.debug,None,'v','img',300,outfile)
  
  # Output shape and color data

  result=open(args.result,"a")
  result.write('\t'.join(map(str,shape_header)))
  result.write("\n")
  result.write('\t'.join(map(str,shape_data)))
  result.write("\n")
  for row in shape_img:
    result.write('\t'.join(map(str,row)))
    result.write("\n")
  result.write('\t'.join(map(str,color_header)))
  result.write("\n")
  result.write('\t'.join(map(str,color_data)))
  result.write("\n")
  result.write('\t'.join(map(str,boundary_header)))
  result.write("\n")
  result.write('\t'.join(map(str,boundary_data)))
  result.write("\n")
  result.write('\t'.join(map(str,boundary_img1)))
  result.write("\n")
  for row in color_img:
    result.write('\t'.join(map(str,row)))
    result.write("\n")
  result.write('\t'.join(map(str,landmark_header)))
  result.write("\n")
  result.write('\t'.join(map(str,landmark_data)))
  result.write("\n")
  result.close()
Esempio n. 37
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 Green-Magenta channel
    device, green_channel = pcv.rgb2gray_lab(img=img,
                                             channel="a",
                                             device=device,
                                             debug=args.debug)

    # Invert the Green-Magenta image because the plant is dark green
    device, green_inv = pcv.invert(img=green_channel,
                                   device=device,
                                   debug=args.debug)

    # Threshold the inverted Green-Magenta image to mostly isolate green pixels
    device, green_thresh = pcv.binary_threshold(img=green_inv,
                                                threshold=134,
                                                maxValue=255,
                                                object_type="light",
                                                device=device,
                                                debug=args.debug)

    # Extract core plant region from the image to preserve delicate plant features during filtering
    device += 1
    plant_region = green_thresh[100:2000, 250:2250]
    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=green_thresh,
                                              ksize=(7, 7),
                                              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[100:2000, 250:2250] = plant_region
    if args.debug is not None:
        pcv.print_image(filename=str(device) + "_replace_plant_region.png",
                        img=blur_thresholded)

    # Use a median blur to breakup the horizontal and vertical lines caused by shadows from the track edges
    device, med_blur = pcv.median_blur(img=blur_thresholded,
                                       ksize=5,
                                       device=device,
                                       debug=args.debug)

    # Fill in small contours
    device, green_fill_50 = pcv.fill(img=np.copy(med_blur),
                                     mask=np.copy(med_blur),
                                     size=50,
                                     device=device,
                                     debug=args.debug)

    # Define an ROI for the brass stopper
    device, stopper_roi, stopper_hierarchy = pcv.define_roi(
        img=img,
        shape="rectangle",
        device=device,
        roi=None,
        roi_input="default",
        debug=args.debug,
        adjust=True,
        x_adj=1420,
        y_adj=890,
        w_adj=-920,
        h_adj=-1040)

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

    # Remove contours completely contained within the stopper region of interest
    device, remove_stopper_mask = remove_countors_roi(mask=green_fill_50,
                                                      contours=contours,
                                                      hierarchy=hierarchy,
                                                      roi=stopper_roi,
                                                      device=device,
                                                      debug=args.debug)

    # Define an ROI for a screw hole
    device, screw_roi, screw_hierarchy = pcv.define_roi(img=img,
                                                        shape="rectangle",
                                                        device=device,
                                                        roi=None,
                                                        roi_input="default",
                                                        debug=args.debug,
                                                        adjust=True,
                                                        x_adj=1870,
                                                        y_adj=1010,
                                                        w_adj=-485,
                                                        h_adj=-960)

    # Remove contours completely contained within the screw region of interest
    device, remove_screw_mask = remove_countors_roi(mask=remove_stopper_mask,
                                                    contours=contours,
                                                    hierarchy=hierarchy,
                                                    roi=screw_roi,
                                                    device=device,
                                                    debug=args.debug)

    # Identify objects
    device, contours, contour_hierarchy = pcv.find_objects(
        img=img, mask=remove_screw_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=200,
                                                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)

        # 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")
        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=0,
                                                 y=1,
                                                 v_pos="bottom",
                                                 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()
Esempio n. 38
0
def main():
    # Get options
    args = options()

    # Read image
    img, path, filename = pcv.readimage(args.image)
    roi, path1, filename1 = pcv.readimage(args.roi)
    #roi = cv2.imread(args.roi)

    # Pipeline step
    device = 0

    ## Convert RGB to HSV and extract the Saturation channel
    #device, s = pcv.rgb2gray_hsv(img, 's', device, args.debug)
    #
    ## Threshold the Saturation image
    #device, s_thresh = pcv.binary_threshold(s, 30, 255, 'light', device, args.debug)
    #
    ## Median Filter
    #device, s_mblur = pcv.median_blur(s_thresh, 0, device, args.debug)
    #device, s_cnt = pcv.median_blur(s_thresh, 0, device, args.debug)
    #
    ## Fill small objects
    ##device, s_fill = pcv.fill(s_mblur, s_cnt, 0, device, args.debug)
    #
    ## Convert RGB to LAB and extract the Blue channel
    #device, b = pcv.rgb2gray_lab(img, 'b', device, args.debug)
    #
    ## Threshold the blue image
    #device, b_thresh = pcv.binary_threshold(b, 135, 255, 'light', device, args.debug)
    #device, b_cnt = pcv.binary_threshold(b, 135, 255, 'light', device, args.debug)
    #
    ## Fill small objects
    ##device, b_fill = pcv.fill(b_thresh, b_cnt, 10, device, args.debug)
    #
    ## Join the thresholded saturation and blue-yellow images
    #device, bs = pcv.logical_or(s_mblur, b_cnt, device, args.debug)
    #
    ## Apply Mask (for vis images, mask_color=white)
    #device, masked = pcv.apply_mask(img, bs, 'white', device, args.debug)
    #
    ## Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    #device, masked_a = pcv.rgb2gray_lab(masked, 'a', device, args.debug)
    #device, masked_b = pcv.rgb2gray_lab(masked, 'b', device, args.debug)
    #
    ## Threshold the green-magenta and blue images
    #device, maskeda_thresh = pcv.binary_threshold(masked_a, 115, 255, 'dark', device, args.debug)
    #device, maskeda_thresh1 = pcv.binary_threshold(masked_a, 135, 255, 'light', device, args.debug)
    #device, maskedb_thresh = pcv.binary_threshold(masked_b, 128, 255, 'light', device, args.debug)
    #
    ## Join the thresholded saturation and blue-yellow images (OR)
    #device, ab1 = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, args.debug)
    #device, ab = pcv.logical_or(maskeda_thresh1, ab1, device, args.debug)
    #device, ab_cnt = pcv.logical_or(maskeda_thresh1, ab1, device, args.debug)
    #
    ## Fill small objects
    #device, ab_fill = pcv.fill(ab, ab_cnt, 50, device, args.debug)
    #
    ## Apply mask (for vis images, mask_color=white)
    #device, masked2 = pcv.apply_mask(masked, ab_fill, 'white', device, args.debug)
    #
    ## Identify objects
    #device, id_objects,obj_hierarchy = pcv.find_objects(masked2, ab_fill, device, args.debug)

    # Define ROI
    device = pcvd.define_multi_roi(img,
                                   device,
                                   args.debug,
                                   roi_file=roi,
                                   roi_input='rgb')
Esempio n. 39
0
def main():
  # Get options
  args = options()
  
  # Read image
  img, path, filename = pcv.readimage(args.image)
  roi, path1, filename1 = pcv.readimage(args.roi)
  #roi = cv2.imread(args.roi)
  
  # Pipeline step
  device = 0

  ## Convert RGB to HSV and extract the Saturation channel
  #device, s = pcv.rgb2gray_hsv(img, 's', device, args.debug)
  #
  ## Threshold the Saturation image
  #device, s_thresh = pcv.binary_threshold(s, 30, 255, 'light', device, args.debug)
  #
  ## Median Filter
  #device, s_mblur = pcv.median_blur(s_thresh, 0, device, args.debug)
  #device, s_cnt = pcv.median_blur(s_thresh, 0, device, args.debug)
  #
  ## Fill small objects
  ##device, s_fill = pcv.fill(s_mblur, s_cnt, 0, device, args.debug)
  #
  ## Convert RGB to LAB and extract the Blue channel
  #device, b = pcv.rgb2gray_lab(img, 'b', device, args.debug)
  #
  ## Threshold the blue image
  #device, b_thresh = pcv.binary_threshold(b, 135, 255, 'light', device, args.debug)
  #device, b_cnt = pcv.binary_threshold(b, 135, 255, 'light', device, args.debug)
  #
  ## Fill small objects
  ##device, b_fill = pcv.fill(b_thresh, b_cnt, 10, device, args.debug)
  #
  ## Join the thresholded saturation and blue-yellow images
  #device, bs = pcv.logical_or(s_mblur, b_cnt, device, args.debug)
  #
  ## Apply Mask (for vis images, mask_color=white)
  #device, masked = pcv.apply_mask(img, bs, 'white', device, args.debug)
  #
  ## Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
  #device, masked_a = pcv.rgb2gray_lab(masked, 'a', device, args.debug)
  #device, masked_b = pcv.rgb2gray_lab(masked, 'b', device, args.debug)
  #
  ## Threshold the green-magenta and blue images
  #device, maskeda_thresh = pcv.binary_threshold(masked_a, 115, 255, 'dark', device, args.debug)
  #device, maskeda_thresh1 = pcv.binary_threshold(masked_a, 135, 255, 'light', device, args.debug)
  #device, maskedb_thresh = pcv.binary_threshold(masked_b, 128, 255, 'light', device, args.debug)
  #
  ## Join the thresholded saturation and blue-yellow images (OR)
  #device, ab1 = pcv.logical_or(maskeda_thresh, maskedb_thresh, device, args.debug)
  #device, ab = pcv.logical_or(maskeda_thresh1, ab1, device, args.debug)
  #device, ab_cnt = pcv.logical_or(maskeda_thresh1, ab1, device, args.debug)
  #
  ## Fill small objects
  #device, ab_fill = pcv.fill(ab, ab_cnt, 50, device, args.debug)
  #
  ## Apply mask (for vis images, mask_color=white)
  #device, masked2 = pcv.apply_mask(masked, ab_fill, 'white', device, args.debug)
  #
  ## Identify objects
  #device, id_objects,obj_hierarchy = pcv.find_objects(masked2, ab_fill, device, args.debug)

  # Define ROI
  device= pcvd.define_multi_roi(img, device, args.debug, roi_file=roi, roi_input='rgb')
Esempio n. 40
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()
Esempio n. 41
0
def main():
  # Get options
  args = options()
  
  # Read image
  img, path, filename = pcv.readimage(args.image)
  brass_mask = cv2.imread(args.roi)
  
  # Pipeline step
  device = 0

  # Convert RGB to HSV and extract the Saturation channel
  device, s = pcv.rgb2gray_hsv(img, 's', device, args.debug)
  
  # Threshold the Saturation image
  device, s_thresh = pcv.binary_threshold(s, 49, 255, 'light', device, args.debug)
  
   #Median Filter
  device, s_mblur = pcv.median_blur(s_thresh, 5, device, args.debug)
  
   #Apply Mask (for vis images, mask_color=white)
  device, masked = pcv.apply_mask(img, s_mblur, 'white', device, args.debug)
  
#   Convert RGB to LAB and extract the Green-Magenta 
  device, soil_a = pcv.rgb2gray_lab(masked, 'a', device, args.debug)
#  
#   Threshold the green-magenta 
  device, soila_thresh = pcv.binary_threshold(soil_a, 133, 255, 'light', device, args.debug)
  device, soila_cnt = pcv.binary_threshold(soil_a, 133, 255, 'light', device, args.debug)

#
#   Fill small objects
  device, soil_fill = pcv.fill(soila_thresh, soila_cnt, 200, device, args.debug)
#
#   Median Filter
  device, soil_mblur = pcv.median_blur(soil_fill, 13, device, args.debug)
  device, soil_cnt = pcv.median_blur(soil_fill, 13, device, args.debug)
#  
#   Apply mask (for vis images, mask_color=white)
  device, masked2 = pcv.apply_mask(soil_mblur, soil_cnt, 'white', device, args.debug)
#  
#   Identify objects
  device, id_objects,obj_hierarchy = pcv.find_objects(masked2, soil_cnt, device, args.debug)
#
#   Define ROI
  device, roi1, roi_hierarchy= pcv.define_roi(img,'rectangle', device, None, 'default', args.debug,True, 400,400,-400,-400)
#  
#   Decide which objects to keep
  device,roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(img,'partial',roi1,roi_hierarchy,id_objects,obj_hierarchy,device, args.debug)
#  
#   Object combine kept objects
  device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3, device, args.debug)
#  
############## Analysis ################  
  
  # Find shape properties, output shape image (optional)
  device, shape_header,shape_data,shape_img = pcv.analyze_object(img, args.image, obj, mask, device,args.debug,args.outdir+'/'+filename)
   
  # Determine color properties: Histograms, Color Slices and Pseudocolored Images, output color analyzed images (optional)
  device, color_header,color_data,norm_slice= pcv.analyze_color(img, args.image, mask, 256, device, args.debug,'all','rgb','v','img',300,args.outdir+'/'+filename)
  
  # Output shape and color data
  pcv.print_results(args.image, shape_header, shape_data)
  pcv.print_results(args.image, color_header, color_data)
Esempio n. 42
0
def main():
    # Get options
    args = options()

    # Read image
    img, path, filename = pcv.readimage(args.image)
    brass_mask = cv2.imread(args.roi)

    # Pipeline step
    device = 0

    # Convert RGB to HSV and extract the Saturation channel
    device, s = pcv.rgb2gray_hsv(img, "s", device, args.debug)

    # Threshold the Saturation image
    device, s_thresh = pcv.binary_threshold(s, 49, 255, "light", device, args.debug)

    # Median Filter
    device, s_mblur = pcv.median_blur(s_thresh, 5, device, args.debug)
    device, s_cnt = pcv.median_blur(s_thresh, 5, device, args.debug)

    # Fill small objects
    device, s_fill = pcv.fill(s_mblur, s_cnt, 150, device, args.debug)

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

    # Threshold the blue image
    device, b_thresh = pcv.binary_threshold(b, 138, 255, "light", device, args.debug)
    device, b_cnt = pcv.binary_threshold(b, 138, 255, "light", device, args.debug)

    # Fill small objects
    device, b_fill = pcv.fill(b_thresh, b_cnt, 150, device, args.debug)

    # Join the thresholded saturation and blue-yellow images
    device, bs = pcv.logical_and(s_fill, b_fill, device, args.debug)

    # Apply Mask (for vis images, mask_color=white)
    device, masked = pcv.apply_mask(img, bs, "white", device, args.debug)

    # Mask pesky brass piece
    device, brass_mask1 = pcv.rgb2gray_hsv(brass_mask, "v", device, args.debug)
    device, brass_thresh = pcv.binary_threshold(brass_mask1, 0, 255, "light", device, args.debug)
    device, brass_inv = pcv.invert(brass_thresh, device, args.debug)
    device, brass_masked = pcv.apply_mask(masked, brass_inv, "white", device, args.debug)

    # Further mask soil and car
    device, masked_a = pcv.rgb2gray_lab(brass_masked, "a", device, args.debug)
    device, soil_car1 = pcv.binary_threshold(masked_a, 128, 255, "dark", device, args.debug)
    device, soil_car2 = pcv.binary_threshold(masked_a, 128, 255, "light", device, args.debug)
    device, soil_car = pcv.logical_or(soil_car1, soil_car2, device, args.debug)
    device, soil_masked = pcv.apply_mask(brass_masked, soil_car, "white", device, args.debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, soil_a = pcv.rgb2gray_lab(soil_masked, "a", device, args.debug)
    device, soil_b = pcv.rgb2gray_lab(soil_masked, "b", device, args.debug)

    # Threshold the green-magenta and blue images
    device, soila_thresh = pcv.binary_threshold(soil_a, 124, 255, "dark", device, args.debug)
    device, soilb_thresh = pcv.binary_threshold(soil_b, 148, 255, "light", device, args.debug)

    # Join the thresholded saturation and blue-yellow images (OR)
    device, soil_ab = pcv.logical_or(soila_thresh, soilb_thresh, device, args.debug)
    device, soil_ab_cnt = pcv.logical_or(soila_thresh, soilb_thresh, device, args.debug)

    # Fill small objects
    device, soil_cnt = pcv.fill(soil_ab, soil_ab_cnt, 250, device, args.debug)

    # Median Filter
    # device, soil_mblur = pcv.median_blur(soil_fill, 5, device, args.debug)
    # device, soil_cnt = pcv.median_blur(soil_fill, 5, device, args.debug)

    # Apply mask (for vis images, mask_color=white)
    device, masked2 = pcv.apply_mask(soil_masked, soil_cnt, "white", device, args.debug)

    # Identify objects
    device, id_objects, obj_hierarchy = pcv.find_objects(masked2, soil_cnt, device, args.debug)

    # Define ROI
    device, roi1, roi_hierarchy = pcv.define_roi(
        img, "rectangle", device, None, "default", args.debug, True, 600, 450, -600, -350
    )

    # Decide which objects to keep
    device, roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(
        img, "partial", roi1, roi_hierarchy, id_objects, obj_hierarchy, device, args.debug
    )

    # Object combine kept objects
    device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3, device, args.debug)
    ############## VIS Analysis ################

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

    # Find shape properties, output shape image (optional)
    device, shape_header, shape_data, shape_img = pcv.analyze_object(
        img, args.image, obj, mask, device, args.debug, 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, args.image, mask, 256, device, args.debug, None, "v", "img", 300, outfile
    )

    # Output shape and color data

    result = open(args.result, "a")
    result.write("\t".join(map(str, shape_header)))
    result.write("\n")
    result.write("\t".join(map(str, shape_data)))
    result.write("\n")
    for row in shape_img:
        result.write("\t".join(map(str, row)))
        result.write("\n")
    result.write("\t".join(map(str, color_header)))
    result.write("\n")
    result.write("\t".join(map(str, color_data)))
    result.write("\n")
    for row in color_img:
        result.write("\t".join(map(str, row)))
        result.write("\n")
Esempio n. 43
0
def main():
    # Get options
    args = options()

    # Read image
    img, path, filename = pcv.readimage(args.image)
    brass_mask = cv2.imread(args.roi)

    # Pipeline step
    device = 0

    # Convert RGB to HSV and extract the Saturation channel
    device, s = pcv.rgb2gray_hsv(img, 's', device, args.debug)

    # Threshold the Saturation image
    device, s_thresh = pcv.binary_threshold(s, 49, 255, 'light', device,
                                            args.debug)

    # Median Filter
    device, s_mblur = pcv.median_blur(s_thresh, 5, device, args.debug)
    device, s_cnt = pcv.median_blur(s_thresh, 5, device, args.debug)

    # Fill small objects
    device, s_fill = pcv.fill(s_mblur, s_cnt, 150, device, args.debug)

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

    # Threshold the blue image
    device, b_thresh = pcv.binary_threshold(b, 138, 255, 'light', device,
                                            args.debug)
    device, b_cnt = pcv.binary_threshold(b, 138, 255, 'light', device,
                                         args.debug)

    # Fill small objects
    device, b_fill = pcv.fill(b_thresh, b_cnt, 100, device, args.debug)

    # Join the thresholded saturation and blue-yellow images
    device, bs = pcv.logical_and(s_fill, b_fill, device, args.debug)

    # Apply Mask (for vis images, mask_color=white)
    device, masked = pcv.apply_mask(img, bs, 'white', device, args.debug)

    # Mask pesky brass piece
    device, brass_mask1 = pcv.rgb2gray_hsv(brass_mask, 'v', device, args.debug)
    device, brass_thresh = pcv.binary_threshold(brass_mask1, 0, 255, 'light',
                                                device, args.debug)
    device, brass_inv = pcv.invert(brass_thresh, device, args.debug)
    device, brass_masked = pcv.apply_mask(masked, brass_inv, 'white', device,
                                          args.debug)

    # Further mask soil and car
    device, masked_a = pcv.rgb2gray_lab(brass_masked, 'a', device, args.debug)
    device, soil_car1 = pcv.binary_threshold(masked_a, 128, 255, 'dark',
                                             device, args.debug)
    device, soil_car2 = pcv.binary_threshold(masked_a, 128, 255, 'light',
                                             device, args.debug)
    device, soil_car = pcv.logical_or(soil_car1, soil_car2, device, args.debug)
    device, soil_masked = pcv.apply_mask(brass_masked, soil_car, 'white',
                                         device, args.debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, soil_a = pcv.rgb2gray_lab(soil_masked, 'a', device, args.debug)
    device, soil_b = pcv.rgb2gray_lab(soil_masked, 'b', device, args.debug)

    # Threshold the green-magenta and blue images
    device, soila_thresh = pcv.binary_threshold(soil_a, 124, 255, 'dark',
                                                device, args.debug)
    device, soilb_thresh = pcv.binary_threshold(soil_b, 148, 255, 'light',
                                                device, args.debug)

    # Join the thresholded saturation and blue-yellow images (OR)
    device, soil_ab = pcv.logical_or(soila_thresh, soilb_thresh, device,
                                     args.debug)
    device, soil_ab_cnt = pcv.logical_or(soila_thresh, soilb_thresh, device,
                                         args.debug)

    # Fill small objects
    device, soil_cnt = pcv.fill(soil_ab, soil_ab_cnt, 150, device, args.debug)

    # Median Filter
    #device, soil_mblur = pcv.median_blur(soil_fill, 5, device, args.debug)
    #device, soil_cnt = pcv.median_blur(soil_fill, 5, device, args.debug)

    # Apply mask (for vis images, mask_color=white)
    device, masked2 = pcv.apply_mask(soil_masked, soil_cnt, 'white', device,
                                     args.debug)

    # Identify objects
    device, id_objects, obj_hierarchy = pcv.find_objects(
        masked2, soil_cnt, device, args.debug)

    # Define ROI
    device, roi1, roi_hierarchy = pcv.define_roi(img, 'rectangle', device,
                                                 None, 'default', args.debug,
                                                 True, 600, 450, -600, -350)

    # Decide which objects to keep
    device, roi_objects, hierarchy3, kept_mask, obj_area = pcv.roi_objects(
        img, 'partial', roi1, roi_hierarchy, id_objects, obj_hierarchy, device,
        args.debug)

    # Object combine kept objects
    device, obj, mask = pcv.object_composition(img, roi_objects, hierarchy3,
                                               device, args.debug)

    ############## VIS Analysis ################

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

    # Find shape properties, output shape image (optional)
    device, shape_header, shape_data, shape_img = pcv.analyze_object(
        img, args.image, obj, mask, device, args.debug, 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, args.image, mask, 256, device, args.debug, None, 'v', 'img', 300,
        outfile)

    # Output shape and color data

    result = open(args.result, "a")
    result.write('\t'.join(map(str, shape_header)))
    result.write("\n")
    result.write('\t'.join(map(str, shape_data)))
    result.write("\n")
    for row in shape_img:
        result.write('\t'.join(map(str, row)))
        result.write("\n")
    result.write('\t'.join(map(str, color_header)))
    result.write("\n")
    result.write('\t'.join(map(str, color_data)))
    result.write("\n")
    for row in color_img:
        result.write('\t'.join(map(str, row)))
        result.write("\n")
    result.close()

    ############################# Use VIS image mask for NIR image#########################
    # Find matching NIR image
    device, nirpath = pcv.get_nir(path, filename, device, args.debug)
    nir, path1, filename1 = pcv.readimage(nirpath)
    nir2 = cv2.imread(nirpath, -1)

    # Flip mask
    device, f_mask = pcv.flip(mask, "horizontal", device, args.debug)

    # Reize mask
    device, nmask = pcv.resize(f_mask, 0.116148, 0.116148, device, args.debug)

    # position, and crop mask
    device, newmask = pcv.crop_position_mask(nir, nmask, device, 15, 5, "top",
                                             "right", args.debug)

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

    # Object combine kept objects
    device, nir_combined, nir_combinedmask = pcv.object_composition(
        nir, nir_objects, nir_hierarchy, device, args.debug)

    ####################################### Analysis #############################################
    outfile1 = False
    if args.writeimg == True:
        outfile1 = args.outdir + "/" + filename1

    device, nhist_header, nhist_data, nir_imgs = pcv.analyze_NIR_intensity(
        nir2, filename1, nir_combinedmask, 256, device, False, args.debug,
        outfile1)
    device, nshape_header, nshape_data, nir_shape = pcv.analyze_object(
        nir2, filename1, nir_combined, nir_combinedmask, device, args.debug,
        outfile1)

    coresult = open(args.coresult, "a")
    coresult.write('\t'.join(map(str, nhist_header)))
    coresult.write("\n")
    coresult.write('\t'.join(map(str, nhist_data)))
    coresult.write("\n")
    for row in nir_imgs:
        coresult.write('\t'.join(map(str, row)))
        coresult.write("\n")

    coresult.write('\t'.join(map(str, nshape_header)))
    coresult.write("\n")
    coresult.write('\t'.join(map(str, nshape_data)))
    coresult.write("\n")
    coresult.write('\t'.join(map(str, nir_shape)))
    coresult.write("\n")
    coresult.close()
Esempio n. 44
0
def main():
    # Get options
    args = options()

    # Read image
    img, path, filename = pcv.readimage(args.image)
    #roi = cv2.imread(args.roi)

    # Pipeline step
    device = 0

    # Convert RGB to HSV and extract the Saturation channel
    device, s = pcv.rgb2gray_hsv(img, 's', device, args.debug)

    # Threshold the Saturation image
    device, s_thresh = pcv.binary_threshold(s, 34, 255, 'light', device,
                                            args.debug)

    # Median Filter
    #device, s_mblur = pcv.median_blur(s_thresh, 0, device, args.debug)
    #device, s_cnt = pcv.median_blur(s_thresh, 0, device, args.debug)

    # Fill small objects
    #device, s_fill = pcv.fill(s_mblur, s_cnt, 0, device, args.debug)

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

    # Threshold the blue image
    device, b_thresh = pcv.binary_threshold(b, 138, 255, 'light', device,
                                            args.debug)
    device, b_cnt = pcv.binary_threshold(b, 138, 255, 'light', device,
                                         args.debug)

    # Fill small objects
    device, b_fill = pcv.fill(b_thresh, b_cnt, 150, device, args.debug)

    # Join the thresholded saturation and blue-yellow images
    device, bs = pcv.logical_and(s_thresh, b_fill, device, args.debug)

    # Apply Mask (for vis images, mask_color=white)
    device, masked = pcv.apply_mask(img, bs, 'white', device, args.debug)

    # Convert RGB to LAB and extract the Green-Magenta and Blue-Yellow channels
    device, masked_a = pcv.rgb2gray_lab(masked, 'a', device, args.debug)
    device, masked_b = pcv.rgb2gray_lab(masked, 'b', device, args.debug)

    # Threshold the green-magenta and blue images
    device, maskeda_thresh = pcv.binary_threshold(masked_a, 122, 255, 'dark',
                                                  device, args.debug)
    device, maskedb_thresh = pcv.binary_threshold(masked_b, 133, 255, 'light',
                                                  device, args.debug)

    # Join the thresholded saturation and blue-yellow images (OR)
    device, ab = pcv.logical_or(maskeda_thresh, maskedb_thresh, device,
                                args.debug)
    device, ab_cnt = pcv.logical_or(maskeda_thresh, maskedb_thresh, device,
                                    args.debug)

    # Fill small objects
    device, ab_fill = pcv.fill(ab, ab_cnt, 200, device, args.debug)

    # Apply mask (for vis images, mask_color=white)
    device, masked2 = pcv.apply_mask(masked, ab_fill, 'white', device,
                                     args.debug)

    # Select area with black bars and find overlapping plant material
    device, roi1, roi_hierarchy1 = pcv.define_roi(masked2, 'rectangle', device,
                                                  None, 'default', args.debug,
                                                  True, 0, 0, -1700, 0)
    device, id_objects1, obj_hierarchy1 = pcv.find_objects(
        masked2, ab_fill, device, args.debug)
    device, roi_objects1, hierarchy1, kept_mask1, obj_area1 = pcv.roi_objects(
        masked2, 'cutto', roi1, roi_hierarchy1, id_objects1, obj_hierarchy1,
        device, args.debug)
    device, masked3 = pcv.apply_mask(masked2, kept_mask1, 'white', device,
                                     args.debug)
    device, masked_a1 = pcv.rgb2gray_lab(masked3, 'a', device, args.debug)
    device, masked_b1 = pcv.rgb2gray_lab(masked3, 'b', device, args.debug)
    device, maskeda_thresh1 = pcv.binary_threshold(masked_a1, 110, 255, 'dark',
                                                   device, args.debug)
    device, maskedb_thresh1 = pcv.binary_threshold(masked_b1, 170, 255,
                                                   'light', device, args.debug)
    device, ab1 = pcv.logical_or(maskeda_thresh1, maskedb_thresh1, device,
                                 args.debug)
    device, ab_cnt1 = pcv.logical_or(maskeda_thresh1, maskedb_thresh1, device,
                                     args.debug)
    device, ab_fill1 = pcv.fill(ab1, ab_cnt1, 300, device, args.debug)

    device, roi2, roi_hierarchy2 = pcv.define_roi(masked2, 'rectangle', device,
                                                  None, 'default', args.debug,
                                                  True, 1700, 0, 0, 0)
    device, id_objects2, obj_hierarchy2 = pcv.find_objects(
        masked2, ab_fill, device, args.debug)
    device, roi_objects2, hierarchy2, kept_mask2, obj_area2 = pcv.roi_objects(
        masked2, 'cutto', roi2, roi_hierarchy2, id_objects2, obj_hierarchy2,
        device, args.debug)
    device, masked4 = pcv.apply_mask(masked2, kept_mask2, 'white', device,
                                     args.debug)
    device, masked_a2 = pcv.rgb2gray_lab(masked4, 'a', device, args.debug)
    device, masked_b2 = pcv.rgb2gray_lab(masked4, 'b', device, args.debug)
    device, maskeda_thresh2 = pcv.binary_threshold(masked_a2, 110, 255, 'dark',
                                                   device, args.debug)
    device, maskedb_thresh2 = pcv.binary_threshold(masked_b2, 170, 255,
                                                   'light', device, args.debug)
    device, ab2 = pcv.logical_or(maskeda_thresh2, maskedb_thresh2, device,
                                 args.debug)
    device, ab_cnt2 = pcv.logical_or(maskeda_thresh2, maskedb_thresh2, device,
                                     args.debug)
    device, ab_fill2 = pcv.fill(ab2, ab_cnt2, 200, device, args.debug)

    device, ab_cnt3 = pcv.logical_or(ab_fill1, ab_fill2, device, args.debug)
    device, masked3 = pcv.apply_mask(masked2, ab_cnt3, 'white', device,
                                     args.debug)

    # Identify objects
    device, id_objects3, obj_hierarchy3 = pcv.find_objects(
        masked2, ab_fill, device, args.debug)

    # Define ROI
    device, roi3, roi_hierarchy3 = pcv.define_roi(masked2, 'rectangle', device,
                                                  None, 'default', args.debug,
                                                  True, 650, 0, -450, -300)

    # Decide which objects to keep and combine with objects overlapping with black bars
    device, roi_objects3, hierarchy3, kept_mask3, obj_area1 = pcv.roi_objects(
        img, 'cutto', roi3, roi_hierarchy3, id_objects3, obj_hierarchy3,
        device, args.debug)
    device, kept_mask4_1 = pcv.logical_or(ab_cnt3, kept_mask3, device,
                                          args.debug)
    device, kept_cnt = pcv.logical_or(ab_cnt3, kept_mask3, device, args.debug)
    device, kept_mask4 = pcv.fill(kept_mask4_1, kept_cnt, 200, device,
                                  args.debug)
    device, masked5 = pcv.apply_mask(masked2, kept_mask4, 'white', device,
                                     args.debug)
    device, id_objects4, obj_hierarchy4 = pcv.find_objects(
        masked5, kept_mask4, device, args.debug)
    device, roi4, roi_hierarchy4 = pcv.define_roi(masked2, 'rectangle', device,
                                                  None, 'default', args.debug,
                                                  False, 0, 0, 0, 0)
    device, roi_objects4, hierarchy4, kept_mask4, obj_area = pcv.roi_objects(
        img, 'partial', roi4, roi_hierarchy4, id_objects4, obj_hierarchy4,
        device, args.debug)

    # Object combine kept objects
    device, obj, mask = pcv.object_composition(img, roi_objects4, hierarchy4,
                                               device, args.debug)

    ############## Analysis ################

    # Find shape properties, output shape image (optional)
    device, shape_header, shape_data, shape_img = pcv.analyze_object(
        img, args.image, obj, mask, device, args.debug,
        args.outdir + '/' + filename)

    # Shape properties relative to user boundary line (optional)
    device, boundary_header, boundary_data, boundary_img1 = pcv.analyze_bound(
        img, args.image, obj, mask, 375, device, args.debug,
        args.outdir + '/' + filename)

    # Determine color properties: Histograms, Color Slices and Pseudocolored Images, output color analyzed images (optional)
    device, color_header, color_data, norm_slice = pcv.analyze_color(
        img, args.image, kept_mask4, 256, device, args.debug, 'all', 'rgb',
        'v', 'img', 300, args.outdir + '/' + filename)

    # Output shape and color data
    pcv.print_results(args.image, shape_header, shape_data)
    pcv.print_results(args.image, color_header, color_data)
    pcv.print_results(args.image, boundary_header, boundary_data)
Esempio n. 45
0
def test_plantcv_crop_position_mask():
    nir, path1, filename1 = pcv.readimage(os.path.join(TEST_DATA, TEST_INPUT_NIR_MASK))
    mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_MASK), -1)
    device, newmask = pcv.crop_position_mask(nir, mask, device=0, x=40, y=3, v_pos="top", h_pos="right", debug=None)
    assert np.sum(newmask) == 641517