mirror of
https://github.com/283375/arcaea-offline-ocr.git
synced 2025-04-18 13:00:18 +00:00
refactor: CropBlackEdges
This commit is contained in:
parent
42bcd7b430
commit
122a546174
@ -1,10 +1,10 @@
|
|||||||
from math import floor
|
import math
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
__all__ = ["crop_xywh", "crop_black_edges", "crop_black_edges_grayscale"]
|
__all__ = ["crop_xywh", "CropBlackEdges"]
|
||||||
|
|
||||||
|
|
||||||
def crop_xywh(mat: cv2.Mat, rect: Tuple[int, int, int, int]):
|
def crop_xywh(mat: cv2.Mat, rect: Tuple[int, int, int, int]):
|
||||||
@ -12,92 +12,53 @@ def crop_xywh(mat: cv2.Mat, rect: Tuple[int, int, int, int]):
|
|||||||
return mat[y : y + h, x : x + w]
|
return mat[y : y + h, x : x + w]
|
||||||
|
|
||||||
|
|
||||||
def is_black_edge(list_of_pixels: cv2.Mat, black_pixel: cv2.Mat, ratio: float = 0.6):
|
class CropBlackEdges:
|
||||||
pixels = list_of_pixels.reshape([-1, 3])
|
@staticmethod
|
||||||
return np.count_nonzero(np.all(pixels < black_pixel, axis=1)) > floor(
|
def is_black_edge(__img_gray_slice: cv2.Mat, black_pixel: int, ratio: float = 0.6):
|
||||||
len(pixels) * ratio
|
pixels_compared = __img_gray_slice < black_pixel
|
||||||
)
|
return np.count_nonzero(pixels_compared) > math.floor(
|
||||||
|
__img_gray_slice.size * ratio
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_crop_rect(cls, img_gray: cv2.Mat, black_threshold: int = 25):
|
||||||
|
height, width = img_gray.shape[:2]
|
||||||
|
left = 0
|
||||||
|
right = width
|
||||||
|
top = 0
|
||||||
|
bottom = height
|
||||||
|
|
||||||
def crop_black_edges(img_bgr: cv2.Mat, black_threshold: int = 50):
|
for i in range(width):
|
||||||
cropped = img_bgr.copy()
|
column = img_gray[:, i]
|
||||||
black_pixel = np.array([black_threshold] * 3, img_bgr.dtype)
|
if not cls.is_black_edge(column, black_threshold):
|
||||||
height, width = img_bgr.shape[:2]
|
break
|
||||||
left = 0
|
left += 1
|
||||||
right = width
|
|
||||||
top = 0
|
|
||||||
bottom = height
|
|
||||||
|
|
||||||
for i in range(width):
|
for i in sorted(range(width), reverse=True):
|
||||||
column = cropped[:, i]
|
column = img_gray[:, i]
|
||||||
if not is_black_edge(column, black_pixel):
|
if i <= left + 1 or not cls.is_black_edge(column, black_threshold):
|
||||||
break
|
break
|
||||||
left += 1
|
right -= 1
|
||||||
|
|
||||||
for i in sorted(range(width), reverse=True):
|
for i in range(height):
|
||||||
column = cropped[:, i]
|
row = img_gray[i]
|
||||||
if i <= left + 1 or not is_black_edge(column, black_pixel):
|
if not cls.is_black_edge(row, black_threshold):
|
||||||
break
|
break
|
||||||
right -= 1
|
top += 1
|
||||||
|
|
||||||
for i in range(height):
|
for i in sorted(range(height), reverse=True):
|
||||||
row = cropped[i]
|
row = img_gray[i]
|
||||||
if not is_black_edge(row, black_pixel):
|
if i <= top + 1 or not cls.is_black_edge(row, black_threshold):
|
||||||
break
|
break
|
||||||
top += 1
|
bottom -= 1
|
||||||
|
|
||||||
for i in sorted(range(height), reverse=True):
|
assert right > left, "cropped width < 0"
|
||||||
row = cropped[i]
|
assert bottom > top, "cropped height < 0"
|
||||||
if i <= top + 1 or not is_black_edge(row, black_pixel):
|
return (left, top, right - left, bottom - top)
|
||||||
break
|
|
||||||
bottom -= 1
|
|
||||||
|
|
||||||
return cropped[top:bottom, left:right]
|
@classmethod
|
||||||
|
def crop(
|
||||||
|
cls, img: cv2.Mat, convert_flag: cv2.COLOR_BGR2GRAY, black_threshold: int = 25
|
||||||
def is_black_edge_grayscale(
|
) -> cv2.Mat:
|
||||||
gray_value_list: np.ndarray, black_threshold: int = 50, ratio: float = 0.6
|
rect = cls.get_crop_rect(cv2.cvtColor(img, convert_flag), black_threshold)
|
||||||
) -> bool:
|
return crop_xywh(img, rect)
|
||||||
return (
|
|
||||||
np.count_nonzero(gray_value_list < black_threshold)
|
|
||||||
> len(gray_value_list) * ratio
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def crop_black_edges_grayscale(
|
|
||||||
img_gray: cv2.Mat, black_threshold: int = 50
|
|
||||||
) -> Tuple[int, int, int, int]:
|
|
||||||
"""Returns cropped rect"""
|
|
||||||
height, width = img_gray.shape[:2]
|
|
||||||
left = 0
|
|
||||||
right = width
|
|
||||||
top = 0
|
|
||||||
bottom = height
|
|
||||||
|
|
||||||
for i in range(width):
|
|
||||||
column = img_gray[:, i]
|
|
||||||
if not is_black_edge_grayscale(column, black_threshold):
|
|
||||||
break
|
|
||||||
left += 1
|
|
||||||
|
|
||||||
for i in sorted(range(width), reverse=True):
|
|
||||||
column = img_gray[:, i]
|
|
||||||
if i <= left + 1 or not is_black_edge_grayscale(column, black_threshold):
|
|
||||||
break
|
|
||||||
right -= 1
|
|
||||||
|
|
||||||
for i in range(height):
|
|
||||||
row = img_gray[i]
|
|
||||||
if not is_black_edge_grayscale(row, black_threshold):
|
|
||||||
break
|
|
||||||
top += 1
|
|
||||||
|
|
||||||
for i in sorted(range(height), reverse=True):
|
|
||||||
row = img_gray[i]
|
|
||||||
if i <= top + 1 or not is_black_edge_grayscale(row, black_threshold):
|
|
||||||
break
|
|
||||||
bottom -= 1
|
|
||||||
|
|
||||||
assert right > left, "cropped width > 0"
|
|
||||||
assert bottom > top, "cropped height > 0"
|
|
||||||
return (left, top, right - left, bottom - top)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user