Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
sronilsson committed Mar 7, 2024
2 parents f3e95b7 + e056fb1 commit 07302b8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
18 changes: 14 additions & 4 deletions simba/mixins/geometry_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2698,7 +2698,9 @@ def rank_shapes(
return [shapes[idx] for idx in ranked]

@staticmethod
def contours_to_geometries(contours: List[np.ndarray], force_rectangles: Optional[bool] = True) -> List[Polygon]:
def contours_to_geometries(
contours: List[np.ndarray], force_rectangles: Optional[bool] = True
) -> List[Polygon]:
"""
Convert a list of contours to a list of geometries.
Expand All @@ -2715,9 +2717,17 @@ def contours_to_geometries(contours: List[np.ndarray], force_rectangles: Optiona
>>> GeometryMixin.contours_to_geometries(contours=contours)
"""

check_instance(source=GeometryMixin.contours_to_geometries.__name__, instance=contours, accepted_types=(list,))
for i in contours: check_instance(source=f'{GeometryMixin.contours_to_geometries.__name__} {i}', instance=i,
accepted_types=(np.ndarray,))
check_instance(
source=GeometryMixin.contours_to_geometries.__name__,
instance=contours,
accepted_types=(list,),
)
for i in contours:
check_instance(
source=f"{GeometryMixin.contours_to_geometries.__name__} {i}",
instance=i,
accepted_types=(np.ndarray,),
)
results = []
for contour in contours:
if contour.ndim == 3:
Expand Down
33 changes: 27 additions & 6 deletions simba/mixins/image_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,40 @@ def brightness_intensity(
@staticmethod
def gaussian_blur(img: np.ndarray, kernel_size: Optional[Tuple] = (9, 9)):
check_if_valid_img(data=img, source=ImageMixin.gaussian_blur.__name__)
check_instance(source=ImageMixin.gaussian_blur.__name__, instance=kernel_size, accepted_types=(tuple,))
check_valid_lst(data=list(kernel_size), source=ImageMixin.gaussian_blur.__name__, valid_dtypes=(int,), exact_len=2)
check_instance(
source=ImageMixin.gaussian_blur.__name__,
instance=kernel_size,
accepted_types=(tuple,),
)
check_valid_lst(
data=list(kernel_size),
source=ImageMixin.gaussian_blur.__name__,
valid_dtypes=(int,),
exact_len=2,
)
return cv2.GaussianBlur(img, kernel_size, 0)

@staticmethod
def erode(img: np.ndarray, kernel_size: Optional[Tuple] = (3, 3), iterations: Optional[int] = 3):
def erode(
img: np.ndarray,
kernel_size: Optional[Tuple] = (3, 3),
iterations: Optional[int] = 3,
):
check_if_valid_img(data=img, source=ImageMixin.gaussian_blur.__name__)
check_instance(source=ImageMixin.gaussian_blur.__name__, instance=kernel_size, accepted_types=(tuple,))
check_valid_lst(data=list(kernel_size), source=ImageMixin.gaussian_blur.__name__, valid_dtypes=(int,), exact_len=2)
check_instance(
source=ImageMixin.gaussian_blur.__name__,
instance=kernel_size,
accepted_types=(tuple,),
)
check_valid_lst(
data=list(kernel_size),
source=ImageMixin.gaussian_blur.__name__,
valid_dtypes=(int,),
exact_len=2,
)
check_int(name=ImageMixin.erode.__name__, value=iterations, min_value=1)
return cv2.erode(img, np.ones((3, 3), np.uint8), iterations=3)


@staticmethod
def get_histocomparison(
img_1: np.ndarray,
Expand Down
24 changes: 19 additions & 5 deletions simba/utils/read_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@ def read_config_file(config_path: Union[str, os.PathLike]) -> configparser.Confi
return config


def get_video_meta_data(video_path: Union[str, os.PathLike], fps_as_int: bool = True) -> dict:
def get_video_meta_data(
video_path: Union[str, os.PathLike], fps_as_int: bool = True
) -> dict:
"""
Read video metadata (fps, resolution, frame cnt etc.) from video file (e.g., mp4).
Expand Down Expand Up @@ -648,7 +650,14 @@ def find_all_videos_in_directory(
return video_lst


def read_frm_of_video(video_path: Union[str, os.PathLike, cv2.VideoCapture], frame_index: int = 0, opacity: Optional[float] = None, size: Optional[Tuple[int, int]] = None, greyscale: Optional[bool] = False, clahe: Optional[bool] = False) -> np.ndarray:
def read_frm_of_video(
video_path: Union[str, os.PathLike, cv2.VideoCapture],
frame_index: int = 0,
opacity: Optional[float] = None,
size: Optional[Tuple[int, int]] = None,
greyscale: Optional[bool] = False,
clahe: Optional[bool] = False,
) -> np.ndarray:
"""
Reads single image from video file.
Expand Down Expand Up @@ -710,14 +719,19 @@ def read_frm_of_video(video_path: Union[str, os.PathLike, cv2.VideoCapture], fra
if size:
img = cv2.resize(img, size, interpolation=cv2.INTER_LINEAR)
if greyscale:
if len(img.shape) > 2: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if len(img.shape) > 2:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if clahe:
if len(img.shape) > 2: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if len(img.shape) > 2:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.createCLAHE(clipLimit=2, tileGridSize=(16, 16)).apply(img)
else:
NoDataFoundWarning(msg=f"Frame {frame_index} for video {video_path} could not be read.")
NoDataFoundWarning(
msg=f"Frame {frame_index} for video {video_path} could not be read."
)
return img


def find_video_of_file(
video_dir: Union[str, os.PathLike], filename: str, raise_error: bool = False
) -> Union[str, os.PathLike]:
Expand Down

0 comments on commit 07302b8

Please sign in to comment.