Skip to content

Commit

Permalink
Update utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
scap3yvt authored Nov 22, 2023
1 parent 8b22319 commit c169a19
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions GANDLF/data/patch_miner/opm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
from skimage.morphology import remove_small_holes
from skimage.color.colorconv import rgb2hsv
import cv2
#from skimage.exposure import rescale_intensity
#from skimage.color import rgb2hed

# from skimage.exposure import rescale_intensity
# from skimage.color import rgb2hed

# import matplotlib.pyplot as plt
import yaml
Expand Down Expand Up @@ -238,7 +239,8 @@ def alpha_rgb_2d_channel_check(img):
else:
return False

#def pen_marking_check(img, intensity_thresh=225, intensity_thresh_saturation =50, intensity_thresh_b = 128):

# def pen_marking_check(img, intensity_thresh=225, intensity_thresh_saturation =50, intensity_thresh_b = 128):
# """
# This function is used to curate patches from the input image. It is used to remove patches that have pen markings.
# Args:
Expand All @@ -259,7 +261,14 @@ def alpha_rgb_2d_channel_check(img):
# #Assume patch is valid
# return True

def patch_artifact_check(img, intensity_thresh = 250, intensity_thresh_saturation = 5, intensity_thresh_b = 128, patch_size = (256,256)):

def patch_artifact_check(
img,
intensity_thresh=250,
intensity_thresh_saturation=5,
intensity_thresh_b=128,
patch_size=(256, 256),
):
"""
This function is used to curate patches from the input image. It is used to remove patches that are mostly background.
Args:
Expand All @@ -271,23 +280,36 @@ def patch_artifact_check(img, intensity_thresh = 250, intensity_thresh_saturatio
Returns:
bool: Whether the patch is valid (True) or not (False)
"""
#patch_size = config["patch_size"]
# patch_size = config["patch_size"]
patch_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
count_white_pixels = np.sum(np.logical_and.reduce(img > intensity_thresh, axis=2))
percent_pixels = count_white_pixels / (patch_size[0] * patch_size[1])
count_black_pixels = np.sum(np.logical_and.reduce(img < intensity_thresh_b, axis=2))
percent_pixel_b = count_black_pixels / (patch_size[0] * patch_size[1])
percent_pixel_2 = np.sum(patch_hsv[...,1] < intensity_thresh_saturation) / (patch_size[0] * patch_size[1])
percent_pixel_3 = np.sum(patch_hsv[...,2] > intensity_thresh) / (patch_size[0] * patch_size[1])
percent_pixel_2 = np.sum(patch_hsv[..., 1] < intensity_thresh_saturation) / (
patch_size[0] * patch_size[1]
)
percent_pixel_3 = np.sum(patch_hsv[..., 2] > intensity_thresh) / (
patch_size[0] * patch_size[1]
)

if percent_pixel_2 > 0.99 or np.mean(patch_hsv[...,1]) < 5 or percent_pixel_3 > 0.99:
if (
percent_pixel_2 > 0.99
or np.mean(patch_hsv[..., 1]) < 5
or percent_pixel_3 > 0.99
):
if percent_pixel_2 < 0.1:
return False
elif (percent_pixel_2 > 0.99 and percent_pixel_3 > 0.99) or percent_pixel_b > 0.99 or percent_pixels > 0.9:
elif (
(percent_pixel_2 > 0.99 and percent_pixel_3 > 0.99)
or percent_pixel_b > 0.99
or percent_pixels > 0.9
):
return False
# assume that the patch is valid
return True


def parse_config(config_file):
"""
Parse config file and return a dictionary of config values.
Expand All @@ -304,7 +326,7 @@ def parse_config(config_file):
config["value_map"] = config.get("value_map", None)
config["read_type"] = config.get("read_type", "random")
config["overlap_factor"] = config.get("overlap_factor", 0.0)
config["patch_size"] = config.get("patch_size", [256,256])
config["patch_size"] = config.get("patch_size", [256, 256])

return config

Expand Down

0 comments on commit c169a19

Please sign in to comment.