mirror of
https://github.com/283375/arcaea-offline-ocr.git
synced 2025-04-19 05:20:17 +00:00
feat: core hashers
This commit is contained in:
parent
cfe8de043c
commit
413188d86a
0
src/arcaea_offline_ocr/core/__init__.py
Normal file
0
src/arcaea_offline_ocr/core/__init__.py
Normal file
3
src/arcaea_offline_ocr/core/hashers/__init__.py
Normal file
3
src/arcaea_offline_ocr/core/hashers/__init__.py
Normal file
@ -0,0 +1,3 @@
|
||||
from .index import average, dct, difference
|
||||
|
||||
__all__ = ["average", "dct", "difference"]
|
7
src/arcaea_offline_ocr/core/hashers/_common.py
Normal file
7
src/arcaea_offline_ocr/core/hashers/_common.py
Normal file
@ -0,0 +1,7 @@
|
||||
import cv2
|
||||
|
||||
from arcaea_offline_ocr.types import Mat
|
||||
|
||||
|
||||
def _resize_image(src: Mat, dsize: ...) -> Mat:
|
||||
return cv2.resize(src, dsize, fx=0, fy=0, interpolation=cv2.INTER_AREA)
|
35
src/arcaea_offline_ocr/core/hashers/index.py
Normal file
35
src/arcaea_offline_ocr/core/hashers/index.py
Normal file
@ -0,0 +1,35 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
from arcaea_offline_ocr.types import Mat
|
||||
|
||||
from ._common import _resize_image
|
||||
|
||||
|
||||
def average(img_gray: Mat, hash_size: int) -> Mat:
|
||||
img_resized = _resize_image(img_gray, (hash_size, hash_size))
|
||||
diff = img_resized > img_resized.mean()
|
||||
return diff.flatten()
|
||||
|
||||
|
||||
def difference(img_gray: Mat, hash_size: int) -> Mat:
|
||||
img_size = (hash_size + 1, hash_size)
|
||||
img_resized = _resize_image(img_gray, img_size)
|
||||
|
||||
previous = img_resized[:, :-1]
|
||||
current = img_resized[:, 1:]
|
||||
diff = previous > current
|
||||
return diff.flatten()
|
||||
|
||||
|
||||
def dct(img_gray: Mat, hash_size: int = 16, high_freq_factor: int = 4) -> Mat:
|
||||
# TODO: consistency?
|
||||
img_size_base = hash_size * high_freq_factor
|
||||
img_size = (img_size_base, img_size_base)
|
||||
|
||||
img_resized = _resize_image(img_gray, img_size)
|
||||
img_resized = img_resized.astype(np.float32)
|
||||
dct_mat = cv2.dct(img_resized)
|
||||
|
||||
hash_mat = dct_mat[:hash_size, :hash_size]
|
||||
return hash_mat > hash_mat.mean()
|
Loading…
x
Reference in New Issue
Block a user