当前位置: 首页 > news >正文

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

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

如果将这张图拆为多个子图,下面是一种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)

相关文章:

  • GIS技能应用(1)
  • Web前端:HTML篇(二)元素属性
  • SpringBoot缓存注解使用
  • 如何在Linux中打开core文件
  • 【手撕数据结构】拿捏单链表
  • 前后端分离项目部署,vue--nagix发布部署,.net--API发布部署。
  • TYPE-C接口PD取电快充协议芯片ECP5701:支持PD 2.0和PD 3.0(5V,9V,12V,15V,20V)
  • 【数据结构】探索排序的奥秘
  • HTML零基础自学笔记(上)-7.18
  • laravel框架基础通识-新手
  • 【计算机视觉】siamfc论文复现实现目标追踪
  • 基于 Electron+Vite+Vue3+Sass 框架搭建
  • Python爬虫(2) --爬取网页页面
  • HydraRPC: RPC in the CXL Era——论文阅读
  • 计算机视觉9 全卷积网络
  • 网络传输文件的问题
  • JS中 map, filter, some, every, forEach, for in, for of 用法总结
  • [译]如何构建服务器端web组件,为何要构建?
  • 【407天】跃迁之路——程序员高效学习方法论探索系列(实验阶段164-2018.03.19)...
  • 【css3】浏览器内核及其兼容性
  • 【翻译】babel对TC39装饰器草案的实现
  • 5、React组件事件详解
  • Angular 响应式表单之下拉框
  • CentOS6 编译安装 redis-3.2.3
  • Java 23种设计模式 之单例模式 7种实现方式
  • JavaScript-Array类型
  • JS字符串转数字方法总结
  • linux安装openssl、swoole等扩展的具体步骤
  • Promise面试题2实现异步串行执行
  • React-生命周期杂记
  • React中的“虫洞”——Context
  • Sublime Text 2/3 绑定Eclipse快捷键
  • 如何设计一个微型分布式架构?
  • 微信如何实现自动跳转到用其他浏览器打开指定页面下载APP
  • 问:在指定的JSON数据中(最外层是数组)根据指定条件拿到匹配到的结果
  • 我从编程教室毕业
  • 《码出高效》学习笔记与书中错误记录
  • 从如何停掉 Promise 链说起
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • #DBA杂记1
  • #git 撤消对文件的更改
  • (2024)docker-compose实战 (8)部署LAMP项目(最终版)
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (delphi11最新学习资料) Object Pascal 学习笔记---第13章第6节 (嵌套的Finally代码块)
  • (层次遍历)104. 二叉树的最大深度
  • (分布式缓存)Redis持久化
  • (附源码)springboot课程在线考试系统 毕业设计 655127
  • (介绍与使用)物联网NodeMCUESP8266(ESP-12F)连接新版onenet mqtt协议实现上传数据(温湿度)和下发指令(控制LED灯)
  • (删)Java线程同步实现一:synchronzied和wait()/notify()
  • (转)ObjectiveC 深浅拷贝学习
  • (转)winform之ListView
  • **CI中自动类加载的用法总结
  • .gitignore文件—git忽略文件
  • .NET 6 Mysql Canal (CDC 增量同步,捕获变更数据) 案例版
  • .net core开源商城系统源码,支持可视化布局小程序