第一次递交:能够实现图片分割,开头数字识别

This commit is contained in:
nizhangdaye 2024-06-17 18:08:24 +08:00
commit a190933e35
85 changed files with 253 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# 默认忽略的文件
/shelf/
/workspace.xml

1
.idea/.name Normal file
View File

@ -0,0 +1 @@
TLD_Code

8
.idea/TLD_Code.iml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="opencv" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

14
.idea/deployment.xml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData" remoteFilesAllowedToDisappearOnAutoupload="false">
<serverData>
<paths name="zhangwh@47.111.72.1:8789 password">
<serverdata>
<mappings>
<mapping local="$PROJECT_DIR$" web="/" />
</mappings>
</serverdata>
</paths>
</serverData>
</component>
</project>

View File

@ -0,0 +1,20 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8Inspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="E402" />
<option value="E302" />
</list>
</option>
</inspection_tool>
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N806" />
</list>
</option>
</inspection_tool>
</profile>
</component>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="opencv" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="opencv" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/TLD_Code.iml" filepath="$PROJECT_DIR$/.idea/TLD_Code.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 934 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 971 KiB

BIN
data/1-DK199+530-470GD.JPG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 263 KiB

BIN
data/2-DK199+580-530GD.JPG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 214 KiB

BIN
data/3-DK199+635-580GD.JPG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 181 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 329 KiB

BIN
data/5-DK199+680-635GD.JPG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 729 KiB

BIN
data/num/num_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 B

BIN
data/num/num_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 B

BIN
data/num/num_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 B

BIN
data/num/num_3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 B

BIN
data/num/num_4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 B

BIN
data/num/num_5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 B

BIN
data/num/num_6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 B

BIN
data/num/num_7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 B

BIN
data/num/num_8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 B

BIN
data/num/num_9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 B

BIN
data/num1.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
data/num2.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
data/num3.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
data/num4.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 MiB

BIN
data/saved_img/image_1.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 315 KiB

BIN
data/saved_img/image_10.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 266 KiB

BIN
data/saved_img/image_11.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 KiB

BIN
data/saved_img/image_12.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 263 KiB

BIN
data/saved_img/image_13.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 273 KiB

BIN
data/saved_img/image_14.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 259 KiB

BIN
data/saved_img/image_15.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 KiB

BIN
data/saved_img/image_16.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 KiB

BIN
data/saved_img/image_17.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 KiB

BIN
data/saved_img/image_18.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

BIN
data/saved_img/image_19.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 KiB

BIN
data/saved_img/image_2.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 335 KiB

BIN
data/saved_img/image_20.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 KiB

BIN
data/saved_img/image_21.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 188 KiB

BIN
data/saved_img/image_22.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 KiB

BIN
data/saved_img/image_23.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 KiB

BIN
data/saved_img/image_24.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 KiB

BIN
data/saved_img/image_25.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 147 KiB

BIN
data/saved_img/image_26.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 KiB

BIN
data/saved_img/image_27.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 KiB

BIN
data/saved_img/image_28.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 KiB

BIN
data/saved_img/image_29.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 169 KiB

BIN
data/saved_img/image_3.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 291 KiB

BIN
data/saved_img/image_30.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

BIN
data/saved_img/image_31.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 214 KiB

BIN
data/saved_img/image_32.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

BIN
data/saved_img/image_33.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB

BIN
data/saved_img/image_34.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 144 KiB

BIN
data/saved_img/image_35.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 KiB

BIN
data/saved_img/image_36.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 172 KiB

BIN
data/saved_img/image_37.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 KiB

BIN
data/saved_img/image_38.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 KiB

BIN
data/saved_img/image_39.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 149 KiB

BIN
data/saved_img/image_4.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 321 KiB

BIN
data/saved_img/image_40.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 136 KiB

BIN
data/saved_img/image_41.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB

BIN
data/saved_img/image_42.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 125 KiB

BIN
data/saved_img/image_43.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 173 KiB

BIN
data/saved_img/image_44.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 147 KiB

BIN
data/saved_img/image_5.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 333 KiB

BIN
data/saved_img/image_6.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 253 KiB

BIN
data/saved_img/image_7.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 283 KiB

BIN
data/saved_img/image_8.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 254 KiB

BIN
data/saved_img/image_9.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 272 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

BIN
data/无效/image_29.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 169 KiB

BIN
data/无效/image_30.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

BIN
data/无效/image_31.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 214 KiB

BIN
data/无效/image_32.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

BIN
data/无效/image_33.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB

89
img_processing.py Normal file
View File

@ -0,0 +1,89 @@
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
import number_processing
def cv_show(name, img):
"""定义cv_show函数用于显示图像"""
cv.namedWindow(name, cv.WINDOW_NORMAL) # 窗口大小可调
cv.resizeWindow(name, img.shape[1], img.shape[0]) # 窗口大小可调
cv.imshow(name, img)
cv.waitKey(0)
cv.destroyAllWindows()
def cv_info(name, img):
"""
定义cv_info函数确认图像是否成功读取并且打印图像的基本信息
"""
print(f"图像 {name} 信息如下:")
if img is None:
print("无法读取图像")
else:
print("图像数据类型:", img.dtype)
print("图像类型:", type(img))
print("图像尺寸:", img.shape)
print("图像行数:", img.shape[0])
print("图像列数:", img.shape[1])
print()
def find_line(img):
"""
定义find_line函数用于寻找分界线
返回一段图像的宽度
"""
for i in range(img.shape[1]): # 遍历 a 列
if np.all(img[0, i] >= [230, 230, 230]):
# 求该列像素均值,判断是否为白色
mean = np.mean(img[:, i])
if mean >= 240: # 像素均值大于 230 判断为白色
print(f"分界线位置:{i}, 像素均值:{mean}", end="\n\n")
return i
return -1 # 如果没有找到符合条件的分界线,则返回图像的宽度
def split_img(img):
"""
定义split_img函数用于将图像分割并保存
无返回值
"""
img_width = find_line(img) # 返回图像的宽度
n = 0 # 用于计数分割得到的图像数
for i in range(0, img.shape[1], img_width + 1): # 遍历 img 的每一列
image = img[:, i: i + img_width]
n += 1
cv.imwrite(f"data/saved_img/image_{n}.PNG", image) # 保存图片 无损压缩格式 PNG
def split_num(img):
"""
定义split_num函数用于分割数字并保存
返回数字
"""
# 将彩色图像转化为灰度图像
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# 二值化处理,你可以根据需要调整阈值
_, thresh = cv.threshold(img_gray, 128, 255, cv.THRESH_BINARY_INV)
# 查找轮廓
contours, _ = cv.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# 对轮廓进行排序,以便从左到右处理数字(可选)
contours = sorted(contours, key=lambda x: cv.boundingRect(x)[0])
# 用于存储识别出的数字字符串
recognized_digits = []
# 遍历每个轮廓,并保存每个数字的图片
for i, contour in enumerate(contours):
x, y, w, h = cv.boundingRect(contour)
digit = thresh[y:y + h, x:x + w]
# print(f"第{i+1}个数字的尺寸:{digit.shape[0]}")
if digit.shape[0] < 5: # 小数点的轮廓太小,跳过
break
recognized_digits.append(str(number_processing.num_list.index(digit.tolist())))
# # 保存数字图片
# cv.imwrite(f'data/saved_img/digit_{i}.png', digit)
return int("".join(recognized_digits))

30
mian.py Normal file
View File

@ -0,0 +1,30 @@
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
import img_processing as ip
print(cv.__version__)
img_path = 'data/1-D2K286+520-300GD P_1.JPG'
img = cv.imread(img_path, cv.IMREAD_COLOR) # 读取图像 三通道彩色图
# 起始点假设为固定位置,即 44 行65 列
data_interest = img[44:, 65:-2] # 感兴趣的数据区域
number_interest = img[8:20, 64:175] # 感兴趣的数字区域 横向
ip.cv_info(f"data_interest", data_interest)
ip.cv_info(f"number_interest", number_interest)
# 切割图像
ip.split_img(data_interest)
# 获取数字
num = ip.split_num(number_interest)
print(type(num))
print(num)
# gi.cv_show('data', data_interest)
# gi.cv_show('number', a)
cv.imwrite('data/saved_img/data_interest.PNG', data_interest) # 保存图片
cv.imwrite('data/saved_img/number_interest.PNG', number_interest) # 保存图片

61
number_processing.py Normal file
View File

@ -0,0 +1,61 @@
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]]]