TLD_Code/number_processing.py

62 lines
3.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import cv2
import os
def get_num_list(folder_path):
"""
读取文件夹中的所有.png格式图片并进行二值化处理并返回包含二值化图片的列表。
"""
binary_images = []
# 遍历文件夹中的所有文件
for filename in sorted(os.listdir(folder_path)):
# 检查文件是否是.png格式
if filename.endswith('.png'):
# 构建图片的完整路径
img_path = os.path.join(folder_path, filename)
# 使用OpenCV读取图片为灰度图
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
# 检查图片是否成功读取
if img is not None:
# 使用阈值进行二值化处理
# 假设我们使用127作为阈值但这可以根据你的需求进行调整
_, binary_img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
# 将二值化后的图片添加到列表中
binary_images.append(binary_img.tolist())
return binary_images
num_list = [
[[0, 255, 255, 255, 255, 0], [255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 255, 255],
[255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 255, 255],
[255, 255, 0, 0, 255, 255], [0, 255, 255, 255, 255, 0]],
[[0, 0, 255, 255], [255, 255, 255, 255], [0, 0, 255, 255], [0, 0, 255, 255], [0, 0, 255, 255], [0, 0, 255, 255],
[0, 0, 255, 255], [0, 0, 255, 255], [0, 0, 255, 255], [0, 0, 255, 255]],
[[0, 255, 255, 255, 255, 0], [255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 255, 255], [0, 0, 0, 0, 255, 255],
[0, 0, 0, 255, 255, 0], [0, 0, 255, 255, 0, 0], [0, 255, 255, 0, 0, 0], [255, 255, 0, 0, 0, 0],
[255, 255, 0, 0, 0, 0], [255, 255, 255, 255, 255, 255]],
[[0, 255, 255, 255, 255, 0], [255, 255, 0, 0, 255, 255], [0, 0, 0, 0, 255, 255], [0, 0, 0, 0, 255, 255],
[0, 0, 255, 255, 255, 0], [0, 0, 0, 0, 255, 255], [0, 0, 0, 0, 255, 255], [0, 0, 0, 0, 255, 255],
[255, 255, 0, 0, 255, 255], [0, 255, 255, 255, 255, 0]],
[[0, 0, 0, 0, 255, 255], [0, 0, 0, 255, 255, 255], [0, 0, 255, 255, 255, 255], [0, 0, 255, 255, 255, 255],
[0, 255, 255, 0, 255, 255], [0, 255, 255, 0, 255, 255], [255, 255, 0, 0, 255, 255], [255, 255, 255, 255, 255, 255],
[0, 0, 0, 0, 255, 255], [0, 0, 0, 0, 255, 255]],
[[255, 255, 255, 255, 255, 255], [255, 255, 0, 0, 0, 0], [255, 255, 0, 0, 0, 0], [255, 255, 0, 0, 0, 0],
[255, 255, 255, 255, 255, 0], [255, 255, 0, 0, 255, 255], [0, 0, 0, 0, 255, 255], [0, 0, 0, 0, 255, 255],
[255, 255, 0, 0, 255, 255], [0, 255, 255, 255, 255, 0]],
[[0, 255, 255, 255, 255, 0], [255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 0, 0], [255, 255, 0, 0, 0, 0],
[255, 255, 255, 255, 255, 0], [255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 255, 255],
[255, 255, 0, 0, 255, 255], [0, 255, 255, 255, 255, 0]],
[[255, 255, 255, 255, 255, 255], [0, 0, 0, 0, 255, 255], [0, 0, 0, 255, 255, 0], [0, 0, 0, 255, 255, 0],
[0, 0, 255, 255, 0, 0], [0, 0, 255, 255, 0, 0], [0, 0, 255, 255, 0, 0], [0, 255, 255, 0, 0, 0],
[0, 255, 255, 0, 0, 0], [0, 255, 255, 0, 0, 0]],
[[0, 255, 255, 255, 255, 0], [255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 255, 255],
[0, 255, 255, 255, 255, 0], [255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 255, 255],
[255, 255, 0, 0, 255, 255], [0, 255, 255, 255, 255, 0]],
[[0, 255, 255, 255, 255, 0], [255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 255, 255], [255, 255, 0, 0, 255, 255],
[255, 255, 0, 0, 255, 255], [0, 255, 255, 255, 255, 255], [0, 0, 0, 0, 255, 255], [0, 0, 0, 0, 255, 255],
[255, 255, 0, 0, 255, 255], [0, 255, 255, 255, 255, 0]]]