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

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

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

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 22:10:01       104 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 22:10:01       115 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 22:10:01       94 阅读
  4. Python语言-面向对象

    2024-07-20 22:10:01       100 阅读

热门阅读

  1. dp算法第三天(暑期提升)

    2024-07-20 22:10:01       28 阅读
  2. RabbitMQ 全面解析与常见问题解答

    2024-07-20 22:10:01       26 阅读
  3. 关于大数据技术栈的一些总结

    2024-07-20 22:10:01       25 阅读
  4. 酒茶元宇宙:探索科技与传统文化的融合

    2024-07-20 22:10:01       22 阅读
  5. 移动支付行业现状及其特点

    2024-07-20 22:10:01       26 阅读
  6. Kubernetes v1.30:只读卷挂载可以实现只读了

    2024-07-20 22:10:01       25 阅读
  7. Unity运行时节点编辑器——互动电影案例

    2024-07-20 22:10:01       21 阅读
  8. flask渲染页码

    2024-07-20 22:10:01       21 阅读
  9. 23年oppo提前批B组笔试真题-小欧的卡牌

    2024-07-20 22:10:01       26 阅读