Код: Выделить всё
def preprocess_with_opencv(img):
img = img.numpy()
# Resize
img = cv2.resize(img, (img_width, img_height), interpolation=cv2.INTER_AREA)
# GaussianBlur
img = cv2.GaussianBlur(img, (3, 3), 0)
# Adaptive Threshold
img = cv2.adaptiveThreshold(
img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2
)
# Chuẩn hóa
img = img.astype("float32") / 255.0
img = np.expand_dims(img, axis=-1)
return img
Код: Выделить всё
def encode_single_sample(img_path, label):
# 1. Đọc ảnh
img = tf.io.read_file(img_path)
# 2. Decode ảnh (grayscale)
img = tf.io.decode_png(img, channels=1)
# 3. Sử dụng OpenCV để xử lý trước
img = tf.py_function(preprocess_with_opencv, [img], Tout=tf.float32)
# 4. Transpose nếu mô hình yêu cầu
img = tf.transpose(img, perm=[1, 0, 2])
# 5. Trả về dictionary (image, label)
return {"image": img, "label": label}
[img]https://i.sstatic. net/tLcpGAyf.png[/img]
Однако после обработки изображения с помощью функции encode_single_sample данные кажутся неверными, а выходное изображение нечетким. Как решить эту проблему и обеспечить точность и четкость обработанного изображения?
Подробнее здесь: https://stackoverflow.com/questions/792 ... tensorflow
Мобильная версия