Improve binary datatype checks #1635
Labels
enhancement
Enhancements to existing features
good first issue/PR
Suitable for a first time contributor/reviewer
help wanted
Request help
update
Updates an existing feature/method
Start a discussion about anything you would like
Many of the functions that take binary images as input will check that the input data is indeed binary. In some cases that means checking for the number of unique values to be exactly 2 (0, and 255 represented). But there are cases (
pcv.fill_holes
and maybe others?) where an empty mask is valid to pass through.Look for and update the logic in these bin-img datatype checks.
pcv.fill_holes
-->if len(np.shape(bin_img)) != 2 or len(np.unique(bin_img)) != 2
pcv.closing
-->if len(np.shape(gray_img)) != 2
orif len(np.unique(gray_img)) == 2
pcv.crop_position_mask
-->if len(np.shape(mask)) == 3
thenmask = mask[0]
pcv.fill
-->if len(np.shape(bin_img)) != 2 or len(np.unique(bin_img)) > 2
pcv.flood_fill
-->if len(np.shape(bin_img)) != 2 or len(np.unique(bin_img)) != 2
List of functions NOT currently doing datatype checks
pcv.apply_mask
does not check themask
but checks theimg
pcv.auto_crop
pcv.background_subtraction
only checking for equal image sizes and resizes if notpcv.canny_edge_detect
no check onmask
but checksimg
byif len(dimensions) == 3
thenimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
pcv.create_labels
no check onmask
and scikit.measure.label will run on an RGB image but will fatally error while trying to plot the labeled mask.pcv.dilate
no current check ongray_img
, and actually the source depth can be greater than one so possible to use on RGB?pcv.distance_transform
no current check onbin_img
pcv.erode
no current check ongray_img
, and actually the source depth can be greater than one so possible to use on RGB?pcv.image_add
no current check on gray image inputs, but also will work when images are not grayscale.pcv.invert
does not currently checkgray_img
but the wrapped cv2 functionbitwise_not
can accept RGB data also.pcv.laplace_filter
no current check ongray_img
but accepts RGB data also.pcv.logical_and
no current check onbin_img
and the underlyingcv2. bitwise_and
can accept RGB data.pcv.logical_or
no current check onbin_img
and the underlyingcv2. bitwise_or
can accept RGB data.pcv.logical_xor
no current check onbin_img
and the underlyingcv2. bitwise_xor
can accept RGB data.pcv.median_blur
no current check ongray_img
but underlyingscipy.ndimage.median_filter
can accept RGB data.pcv.naive_bayes_classifier
no current check onrgb_img
but would fatally err if grayscale image input.pcv.rgb2gray_cymk
,pcv.rgb2gray_hsv
,pcv.rgb2gray_lab
,pcv.rgb2gray
no current check onrgb_img
pcv.sobel_filter
no current check ongray_img
pcv.spatial_clustering
no current check onmask
pcv.watershed
no current check onmask
pcv.filters.obj_props
no current check onbin_img
pcv.filters.eccentricity
no current check onbin_img
pcv.morphology.check_cycles
no current check onskel_img
but should be binary otherwise will err on cv2 step.find_tips
,find_branch_pts
,prune
,segment_id
,segment_skeleton
,segment_sort
,pcv.morphology.fill_segments
no current check onmask
pcv.transform.affine_color_correction
no current check onrgb_img
but would give an error about number of objects if somehow given a gray or bin_imgpcv.transform.find_color_card
no check onrgb_img
pcv.visualize.obj_sizes
no check onmask
Example of good dtype checks
pcv.hist_equalization
-->if len(np.shape(gray_img)) == 3
then fatal error.pcv.image_subtract
-->if len(np.shape(gray_img1)) != 2 or len(np.shape(gray_img2)) != 2
pcv.predict_kmeans
-->if len(train_img.shape) == 2: # gray
andelif len(train_img.shape) == 3 and train_img.shape[2] == 3: # rgb
pcv.opening
-->if len(np.shape(gray_img)) != 2:
then fatal error, # If image is binary use the faster methodif len(np.unique(gray_img)) == 2:
and otherwise use grayscale friendly algorithmpcv.within_frame
-->len(np.shape(mask)) > 2 or len(np.unique(mask)) > 2:
fatal_error("Mask should be a binary image
pcv.analyze.___
functions --> # Skip empty masksif np.count_nonzero(mask) != 0:
pcv.photosynthesis.reassign_frame_labels
--> checks mask to be binary and the same size as the other input img.if mask.shape != ps_da.shape[:2] or len(np.unique(mask)) > 2: fatal_error(f"Mask needs to be binary and have shape {ps_da.shape[:2]}")
The text was updated successfully, but these errors were encountered: