您的位置:首页 > 文旅 > 美景 > 【深度学习图像】拼接图的切分

【深度学习图像】拼接图的切分

2024/12/23 14:33:14 来源:https://blog.csdn.net/x1131230123/article/details/140548942  浏览:    关键词:【深度学习图像】拼接图的切分

用户常常将多张图拼成一张图。

如果将这张图拆为多个子图,下面是一种opencv的办法,后面要训练一个模型来识别边缘更为准确。

import osimport cv2
import numpy as npdef 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:continuey_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:continuefor 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 ifx_image.shape[0] / x_image.shape[1] < 5 or x_image.shape[1] / x_image.shape[0] < 5]return x_imagesdef 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 resultsrc = 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)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com