Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_plantcv_roi_from_binary_image():
# Test cache directory
cache_dir = os.path.join(TEST_TMPDIR, "test_plantcv_roi_from_binary_image")
os.mkdir(cache_dir)
# Read in test RGB image
rgb_img = cv2.imread(os.path.join(TEST_DATA, TEST_INPUT_COLOR))
# Create a binary image
bin_img = np.zeros(np.shape(rgb_img)[0:2], dtype=np.uint8)
cv2.rectangle(bin_img, (100, 100), (1000, 1000), 255, -1)
# Test with debug = "print"
pcv.params.debug = "print"
pcv.params.debug_outdir = cache_dir
_, _ = pcv.roi.from_binary_image(bin_img=bin_img, img=rgb_img)
# Test with debug = "plot"
pcv.params.debug = "plot"
_, _ = pcv.roi.from_binary_image(bin_img=bin_img, img=rgb_img)
# Test with debug = None
pcv.params.debug = None
roi_contour, roi_hierarchy = pcv.roi.from_binary_image(bin_img=bin_img, img=rgb_img)
# Assert the contours and hierarchy lists contain only the ROI
assert np.shape(roi_contour) == (1, 3600, 1, 2)
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)
# Read image
device = 0
img = cv2.imread(args.image, flags=0)
path, img_name = os.path.split(args.image)
# Read in image which is average of average of backgrounds
img_bkgrd = cv2.imread("/home/mgehan/LemnaTec/plantcv/masks/nir_tv/background_nir_z3500.png", flags=0)
# NIR images for burnin2 are up-side down. This may be fixed in later experiments
img = ndimage.rotate(img, 0)
img_bkgrd = ndimage.rotate(img_bkgrd, 0)
# Subtract the image from the image background to make the plant more prominent
device, bkg_sub_img = pcv.image_subtract(img, img_bkgrd, device, args.debug)
if args.debug:
pcv.plot_hist(bkg_sub_img, 'bkg_sub_img')
device, bkg_sub_thres_img = pcv.binary_threshold(bkg_sub_img, 150, 255, 'dark', device, args.debug)
bkg_sub_thres_img = cv2.inRange(bkg_sub_img, 50, 190)
if args.debug:
cv2.imwrite('bkgrd_sub_thres.png', bkg_sub_thres_img)
#device, bkg_sub_thres_img = pcv.binary_threshold_2_sided(img_bkgrd, 50, 190, device, args.debug)
# if a region of interest is specified read it in
roi = cv2.imread(args.roi)
# Start by examining the distribution of pixel intensity values
if args.debug:
pcv.plot_hist(img, 'hist_img')
# Will intensity transformation enhance your ability to isolate object of interest by thesholding?
# Read image
device = 0
img = cv2.imread(args.image, flags=0)
path, img_name = os.path.split(args.image)
# Read in image which is average of average of backgrounds
img_bkgrd = cv2.imread("/home/mgehan/LemnaTec/plantcv/masks/nir_tv/background_nir_z3500.png", flags=0)
# NIR images for burnin2 are up-side down. This may be fixed in later experiments
img = ndimage.rotate(img, 0)
img_bkgrd = ndimage.rotate(img_bkgrd, 0)
# Subtract the image from the image background to make the plant more prominent
device, bkg_sub_img = pcv.image_subtract(img, img_bkgrd, device, args.debug)
if args.debug:
pcv.plot_hist(bkg_sub_img, 'bkg_sub_img')
device, bkg_sub_thres_img = pcv.binary_threshold(bkg_sub_img, 150, 255, 'dark', device, args.debug)
bkg_sub_thres_img = cv2.inRange(bkg_sub_img, 50, 190)
if args.debug:
cv2.imwrite('bkgrd_sub_thres.png', bkg_sub_thres_img)
#device, bkg_sub_thres_img = pcv.binary_threshold_2_sided(img_bkgrd, 50, 190, device, args.debug)
# if a region of interest is specified read it in
roi = cv2.imread(args.roi)
# Start by examining the distribution of pixel intensity values
if args.debug:
pcv.plot_hist(img, 'hist_img')
# Will intensity transformation enhance your ability to isolate object of interest by thesholding?
def read_data(filename):
"""Read hyperspectral image data from file.
Inputs:
filename = name of image file
Returns:
spectral_array = image object as numpy array
:param filename: str
:return spectral_array: __main__.Spectral_data
"""
# Store debug mode
debug = params.debug
params.debug = None
# Initialize dictionary
header_dict = {}
headername = filename + ".hdr"
with open(headername, "r") as f:
# Replace characters for easier parsing
hdata = f.read()
hdata = hdata.replace(",\n", ",")
hdata = hdata.replace("\n,", ",")
hdata = hdata.replace("{\n", "{")
hdata = hdata.replace("\n}", "}")
hdata = hdata.replace(" \n ", "")
hdata = hdata.replace(";", "")
hdata = hdata.split("\n")
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,
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, 200, 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,-750)
# 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, 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, 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)
# 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, 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