def test_plantcv_output_mask(): img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) device, imgpath, maskpath, analysis_images = pcv.output_mask( 0, img, mask, 'test.png', TEST_DATA, mask_only=False, debug=None) path = str(TEST_DATA) + '/ori-images' path1 = str(TEST_DATA) + '/mask-images' assert all([os.path.exists(path) is True, os.path.exists(path1) is True]) shutil.rmtree(path) shutil.rmtree(path1)
def test_plantcv_output_mask(): img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_GRAY), -1) mask = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_BINARY), -1) device, imgpath, maskpath, analysis_images = pcv.output_mask(0, img, mask, 'test.png', TEST_DATA, mask_only=False, debug=None) path = str(TEST_DATA) + '/ori-images' path1 = str(TEST_DATA) + '/mask-images' assert all([os.path.exists(path) is True, os.path.exists(path1) is True]) shutil.rmtree(path) shutil.rmtree(path1)
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()
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, 200, 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, -50) # 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 ################ # 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, 280, 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()