add resampling

This commit is contained in:
千住柱間 2025-04-21 03:01:34 -04:00
commit f3a78ea261
Signed by: hashirama
GPG key ID: 53E62470A86BC185

View file

@ -107,8 +107,14 @@ class TextProcessor:
return final_text
import cv2
import numpy as np
def bilinear(img):
h, w = img.shape[:2]
# Create identity maps
map_x, map_y = np.meshgrid(np.arange(w, dtype=np.float32),
np.arange(h, dtype=np.float32))
# Remap with INTER_LINEAR
return cv2.remap(img, map_x, map_y, interpolation=cv2.INTER_LINEAR)
def adjust_gamma(image, gamma=1.0, alpha=1.0, beta=0):
"""
@ -137,15 +143,15 @@ def adjust_gamma(image, gamma=1.0, alpha=1.0, beta=0):
def preprocess_image(image_path):
# Load image in grayscale (1-channel)
gray = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
resampled = bilinear(gray)
# Convert grayscale to 3-channel RGB by duplicating the gray channel
img = adjust_gamma(cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB),gamma=1.3,alpha=1.65,beta=-2) # perfect tunning
img = adjust_gamma(cv2.cvtColor(resampled, cv2.COLOR_GRAY2RGB),gamma=1.3,alpha=1.65,beta=-2) # perfect tunning
mat_in = ncnn.Mat.from_pixels_resize(
img, ncnn.Mat.PixelType.PIXEL_RGB, img.shape[1], img.shape[0], 224, 224
)
mean_vals = [0.5, 0.5, 0.5] * 2
norm_vals = [1/255, 1/255, 1/255] * 2
mean_vals = [0.5, 0.5, 0.5] * 3
norm_vals = [1/255, 1/255, 1/255] * 3
mat_in.substract_mean_normalize(mean_vals, norm_vals)
return mat_in