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()
相关推荐
美狐美颜SDK开放平台3 分钟前
直播APP集成美颜SDK,如何梳理第三方组件的数据权限?
开发语言·人工智能·深度学习·音视频·第三方美颜sdk
沉下心来学鲁班5 分钟前
具身智能进阶:让 MicroDuck 踩着高跷走路
人工智能·python·机器人
紧跟先前的步伐8 分钟前
Go AI Agent 实战 08|HTTP 模型 API 的工作原理与请求流程
人工智能·http·ai·golang
紧跟先前的步伐11 分钟前
Go AI Agent 实战 07|模型幻觉、能力边界与成本控制
大数据·人工智能·golang
dghongcheng15 分钟前
工业涂装数字化转型:从经验喷涂到数据驱动
人工智能·喷涂·喷漆设备·自动喷涂线·自动喷涂
liuyanqun-parbie16 分钟前
从零设计一个通用智能电话平台:FreeSWITCH + ESL + AI 实战架构
人工智能·架构
文慧的科技江湖16 分钟前
2026年10月21日智慧停车行业早报:番禺把单次封顶从132元砍到58元,同一天黄埔要往下挖59米只放200个车位——收费在往下走,车位在往深处长
人工智能·开源·智慧城市·智慧停车·城市管理·静态交通·停车saas
comedate20 分钟前
【AI】Jev API 接入实战:用 Python 调用 TypeSafe Jev 做结构化判断
人工智能·python·jev·typesafe·快速判断模型
workflower25 分钟前
无人机注册量与飞行量
大数据·网络·人工智能·云计算·汽车·无人机
鶴哥只手遮天26 分钟前
Spring AI Alibaba 深度技术系列-第 05 章 编译与执行引擎:CompiledGraph、GraphRunner与三段式 Executor
java·人工智能·开源