用户常常将多张图拼成一张图。
如果将这张图拆为多个子图,下面是一种opencv的办法,后面要训练一个模型来识别边缘更为准确。
import os
import cv2
import numpy as np
def detect_lines(image_path):
# 读取图片
image = cv2.imread(image_path)
if image is None:
raise ValueError("无法读取图片,请检查路径是否正确")
# 将图片转为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 使用Canny边缘检测
edges = cv2.Canny(gray, 20, 240, apertureSize=3)
# 使用霍夫变换检测线段
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=300, maxLineGap=10)
chuizhi = []
shuiping = []
# 筛选出水平和垂直的线段并绘制
if lines is not None:
for line in lines:
for x1, y1, x2, y2 in line:
if abs(y1 - y2) < 5: # 水平线段
shuiping.append((x1, y1, x2, y2))
elif abs(x1 - x2) < 5:
chuizhi.append((x1, y1, x2, y2))
if len(shuiping) == 0 and len(chuizhi) == 0:
return [image]
# 拆图
ys = []
for x1, y1, x2, y2 in shuiping:
ys.append(y1)
ys.append(y2)
ys.sort()
ys = [0] + ys + [image.shape[0]]
y_images = []
for i in range(len(ys) - 1):
if ys[i + 1] - ys[i] < 100:
continue
y_images.append(image[ys[i]:ys[i + 1], :])
xs = []
for x1, y1, x2, y2 in chuizhi:
xs.append(x1)
xs.append(x2)
xs.sort()
xs = [0] + xs + [image.shape[1]]
x_images = []
for i in range(len(xs) - 1):
if xs[i + 1] - xs[i] < 100:
continue
for y_image in y_images:
x_images.append(y_image[:, xs[i]:xs[i + 1]])
# 去除宽高比超过5的
x_images = [x_image for x_image in x_images if
x_image.shape[0] / x_image.shape[1] < 5 or x_image.shape[1] / x_image.shape[0] < 5]
return x_images
def listPathAllfiles(dirname):
result = []
for maindir, subdir, file_name_list in os.walk(dirname):
for filename in file_name_list:
apath = os.path.join(maindir, filename)
result.append(apath)
return result
src = r"C:\Users\Administrator\Pictures\girl_no_train\mangguo"
dst = r"C:\Users\Administrator\Pictures\girl_no_train\mangguo_dst"
if not os.path.exists(dst):
os.makedirs(dst)
files = listPathAllfiles(src)
for file in files:
x_images = detect_lines(file)
for i, x_image in enumerate(x_images):
cv2.imwrite(f"{dst}/{os.path.basename(file)}_{i}.jpg", x_image)