01 漫画解说-图片框的分割

to

查找最佳的轮廓模式

bash 复制代码
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

img = cv.imread('data/test02.png',0)
ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
ret,thresh2 = cv.threshold(img,127,255,cv.THRESH_BINARY_INV)
ret,thresh3 = cv.threshold(img,127,255,cv.THRESH_TRUNC)
ret,thresh4 = cv.threshold(img,127,255,cv.THRESH_TOZERO)
ret,thresh5 = cv.threshold(img,127,255,cv.THRESH_TOZERO_INV)
titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
    plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()
bash 复制代码
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

img = cv.imread('data/test01.png',0)
img = cv.medianBlur(img,5)
ret,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY,11,2)
titles = ['Original Image', 'Global Thresholding (v = 127)',
            'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in range(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

轮廓检测

bash 复制代码
import cv2
import numpy as np
from matplotlib import pyplot as plt

# 读取图像
image = cv2.imread('data/test01.png')

# 转换为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 应用二值化处理
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)

# 进行形态学操作以去除噪声
kernel = np.ones((5, 5), np.uint8)
morph = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)

# 查找轮廓
contours, _ = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# 创建一个用于保存分割文本框的列表
text_boxes = []

# 遍历所有轮廓
for contour in contours:
    # 计算轮廓的边界框
    x, y, w, h = cv2.boundingRect(contour)
    aspect_ratio = w / float(h)
    print(f'x={x}, y={y}, w={w}, h={h}')

    # 过滤掉小的轮廓
    if aspect_ratio > 0.1:
        # 提取文本框区域
        text_box = image[y:y+h, x:x+w]
        text_boxes.append(text_box)
        
        # 在原图上绘制边界框
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

# 保存分割的文本框
text_boxes.reverse()
for i, box in enumerate(text_boxes):
    # cv2.imwrite(f'text_box_{i}.png', box)
    plt.subplot(len(text_boxes),2,i+1),plt.imshow(box,'gray')
    plt.title(f'text_box_{i}.png')
    plt.xticks([]),plt.yticks([])
plt.show()
相关推荐
人工智能培训3 分钟前
图神经网络初探(1)
人工智能·深度学习·知识图谱·群体智能·智能体
love530love44 分钟前
Windows 11 下 Z-Image-Turbo 完整部署与 Flash Attention 2.8.3 本地编译复盘
人工智能·windows·python·aigc·flash-attn·z-image·cuda加速
雪下的新火1 小时前
AI工具-Hyper3D
人工智能·aigc·blender·ai工具·笔记分享
Das11 小时前
【机器学习】01_模型选择与评估
人工智能·算法·机器学习
墨染天姬2 小时前
【AI】AI时代,模组厂商如何建立自己的AI护城河?
人工智能
aigcapi2 小时前
[深度观察] RAG 架构重塑流量分发:2025 年 GEO 优化技术路径与头部服务商选型指南
大数据·人工智能·架构
字节跳动开源2 小时前
Midscene v1.0 发布 - 视觉驱动,UI 自动化体验跃迁
前端·人工智能·客户端
+wacyltd大模型备案算法备案2 小时前
大模型备案怎么做?2025年企业大模型备案全流程与材料清单详解
人工智能·大模型备案·算法备案·大模型上线登记
吾在学习路3 小时前
故事型总结:Swin Transformer 是如何打破 Vision Transformer 壁垒的?
人工智能·深度学习·transformer