82 lines
2.0 KiB
Python
82 lines
2.0 KiB
Python
from typing import List, Tuple
|
|
import cv2
|
|
import numpy as np
|
|
|
|
|
|
class Preprocessing:
|
|
"""Preprocessing class.
|
|
|
|
Parameters
|
|
----------
|
|
clipLimit: float
|
|
default = 5.0
|
|
tileGridSize: Tuple[int, int]
|
|
default = (15, 15)
|
|
thresh1: int
|
|
default = 0
|
|
thresh2: int
|
|
default = 255
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
tileGridSize: Tuple[int, int] = (15, 15),
|
|
clipLimit: float = 5.0,
|
|
thresh1: int = 0,
|
|
thresh2: int = 255,
|
|
) -> None:
|
|
self.tileGridSize = tileGridSize
|
|
self.clipLimit = clipLimit
|
|
self.thresh1 = thresh1
|
|
self.thresh2 = thresh2
|
|
|
|
|
|
def gray(self, image: np.ndarray) -> np.ndarray:
|
|
"""Convert to GRAY for a given image.
|
|
|
|
Parameters
|
|
----------
|
|
image : np.ndarray
|
|
image of chessboard
|
|
|
|
Returns
|
|
-------
|
|
np.ndarray
|
|
image of chessboard converted to GRAY
|
|
"""
|
|
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
|
|
return gray
|
|
|
|
def clahe(self, image: np.ndarray) -> np.ndarray:
|
|
"""Apply Clahe to GRAY Shimage.
|
|
|
|
Parameters
|
|
----------
|
|
image : np.ndarray
|
|
image of chessboard
|
|
|
|
Returns
|
|
-------
|
|
np.ndarray
|
|
image of chessboard converted to GRAY and applied CLAHE
|
|
"""
|
|
clahe = cv2.createCLAHE(clipLimit = self.clipLimit, tileGridSize = self.tileGridSize)
|
|
clahed = clahe.apply(self.gray(image))
|
|
return clahed
|
|
|
|
def threshold(self, image: np.ndarray) -> np.ndarray:
|
|
"""Apply Clahe to GRAY Shimage.
|
|
|
|
Parameters
|
|
----------
|
|
image : np.ndarray
|
|
image of chessboard
|
|
|
|
Returns
|
|
-------
|
|
np.ndarray
|
|
image of chessboard converted to GRAY applied CLAHE and applied THRESHOLD
|
|
"""
|
|
criteria = cv2.THRESH_BINARY + cv2.THRESH_OTSU+1
|
|
ret, threshold = cv2.threshold(self.clahe(image), self.thresh1, self.thresh2, criteria)
|
|
return threshold |