您的位置:首页 > 新闻 > 会展 > 中国建筑材料网官网_bss123_头条新闻今日头条_惠州seo网站管理

中国建筑材料网官网_bss123_头条新闻今日头条_惠州seo网站管理

2025/6/9 13:30:32 来源:https://blog.csdn.net/weixin_42452716/article/details/143143586  浏览:    关键词:中国建筑材料网官网_bss123_头条新闻今日头条_惠州seo网站管理
中国建筑材料网官网_bss123_头条新闻今日头条_惠州seo网站管理

在这里插入图片描述to
在这里插入图片描述

查找最佳的轮廓模式

import cv2 as cv
import numpy as np
from matplotlib import pyplot as pltimg = 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()
import cv2 as cv
import numpy as np
from matplotlib import pyplot as pltimg = 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()

在这里插入图片描述

轮廓检测

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()

版权声明:

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

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