found perfect parameters 2/2

This commit is contained in:
千住柱間 2025-04-21 01:37:12 -04:00
commit 3f35d90a0b
Signed by: hashirama
GPG key ID: 53E62470A86BC185

View file

@ -98,12 +98,31 @@ class TextProcessor:
return final_text
def adjust_gamma(image, gamma=1.0):
import cv2
import numpy as np
def adjust_gamma(image, gamma=1.0, alpha=1.0, beta=0):
"""
Adjust the gamma and contrast of a grayscale image.
Parameters:
image (numpy.ndarray): Input grayscale image.
gamma (float): Gamma correction factor. Values < 1 darken the image, > 1 brighten it.
alpha (float): Contrast control. 1.0 means no change, < 1.0 reduces contrast, > 1.0 increases contrast.
beta (int): Brightness control. Positive values brighten the image, negative values darken it.
Returns:
numpy.ndarray: Adjusted image.
"""
# Apply gamma correction
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
return cv2.LUT(image, table)
gamma_corrected = cv2.LUT(image, table)
# Adjust contrast and brightness
adjusted = cv2.convertScaleAbs(gamma_corrected, alpha=alpha, beta=beta)
return adjusted
def preprocess_image(image_path):
@ -111,7 +130,7 @@ def preprocess_image(image_path):
gray = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# Convert grayscale to 3-channel RGB by duplicating the gray channel
img = adjust_gamma(cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB),gamma=2)
img = adjust_gamma(cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB),gamma=2,alpha=1.5,beta=-0.5) # perfect tunning
mat_in = ncnn.Mat.from_pixels_resize(
img, ncnn.Mat.PixelType.PIXEL_RGB, img.shape[1], img.shape[0], 224, 224
)