Skip to content

Commit

Permalink
feat: add new intensity and hue variance sort methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsbarnard committed Dec 5, 2024
1 parent ac8f4b4 commit b13875b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
64 changes: 31 additions & 33 deletions vars_gridview/lib/sort_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,57 +122,55 @@ def key(rect: RectWidget) -> int:
return WidthSort.key(rect) * HeightSort.key(rect)


class MeanIntensitySort(SortMethod):
NAME = "Mean intensity"
class IntensityMeanSort(SortMethod):
NAME = "Intensity mean"

@staticmethod
def key(rect: RectWidget) -> float:
return np.mean(rect.roi, axis=(0, 1, 2))


class MeanHueSort(SortMethod):
NAME = "Mean hue"
class IntensityVarianceSort(SortMethod):
NAME = "Inteinsity variance"

@staticmethod
def key(rect: RectWidget) -> float:
# RGB -> Hue from https://stackoverflow.com/a/23094494
means = np.mean(rect.roi, axis=(0, 1))
means = means / 255
r, g, b = means
min_idx, min_val = min(enumerate(means), key=lambda x: x[1])
max_val = max(means)
return np.var(rect.roi, axis=(0, 1, 2))

if min_idx == 0:
return (g - b) / (max_val - min_val)
elif min_idx == 1:
return 2 + (b - r) / (max_val - min_val)
else:
return 4 + (r - g) / (max_val - min_val)

class HueMeanSort(SortMethod):
NAME = "Hue mean"

class RegionMeanHueSort(SortMethod):
NAME = "Region mean hue (center 1/3)"
@staticmethod
def key(rect: RectWidget) -> float:
roi_hsv = cv2.cvtColor(rect.roi, cv2.COLOR_BGR2HSV)
hue = roi_hsv[:, :, 0]
return np.mean(hue.ravel())


class HueVarianceSort(SortMethod):
NAME = "Hue variance"

@staticmethod
def key(rect: RectWidget) -> float:
roi_hsv = cv2.cvtColor(rect.roi, cv2.COLOR_BGR2HSV)
hue = roi_hsv[:, :, 0]
return np.var(hue.ravel())


class HueMeanCenterRegion(SortMethod):
NAME = "Hue mean (center region)"

@staticmethod
def key(rect: RectWidget) -> float:
sub_roi = rect.roi[
rect.localization.height // 3 : rect.localization.height * 2 // 3,
rect.localization.width // 3 : rect.localization.width * 2 // 3,
]

# RGB -> Hue from https://stackoverflow.com/a/23094494
means = np.mean(sub_roi, axis=(0, 1))
means = means / 255
r, g, b = means
min_idx, min_val = min(enumerate(means), key=lambda x: x[1])
max_val = max(means)

if min_idx == 0:
return (g - b) / (max_val - min_val)
elif min_idx == 1:
return 2 + (b - r) / (max_val - min_val)
else:
return 4 + (r - g) / (max_val - min_val)

roi_hsv = cv2.cvtColor(sub_roi, cv2.COLOR_BGR2HSV)
hue = roi_hsv[:, :, 0]
return np.mean(hue.ravel())


class DepthSort(SortMethod):
Expand All @@ -190,7 +188,7 @@ def key(rect: RectWidget) -> float:

class LaplacianVarianceSort(SortMethod):
NAME = "Sharpness"

@staticmethod
def key(rect: RectWidget) -> float:
roi_gray = cv2.cvtColor(rect.roi, cv2.COLOR_BGR2GRAY)
Expand Down
8 changes: 5 additions & 3 deletions vars_gridview/ui/SortDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ def __init__(self, parent):
sm.WidthSort,
sm.HeightSort,
sm.AreaSort,
sm.MeanIntensitySort,
sm.MeanHueSort,
sm.RegionMeanHueSort,
sm.IntensityMeanSort,
sm.IntensityVarianceSort,
sm.HueMeanSort,
sm.HueMeanCenterRegion,
sm.HueVarianceSort,
sm.DepthSort,
sm.LaplacianVarianceSort,
sm.VerifierSort,
Expand Down

0 comments on commit b13875b

Please sign in to comment.