python - 如何在边缘重叠文本的图像中找到矩形轮廓
问题描述
我正在尝试在图像中找到矩形。矩形可以包含如下示例图像中所示的文本。
使用 findContours 和 cv2.approxPolyDP,我只能检测到矩形的一个子集(以绿色突出显示)并且很难找到文本跨越边界的矩形。
是否有更好的机制来查找所有矩形。(尝试增加 approxPolyDP 的 epsilon 参数,但它看起来对我来说是 hack)
附件是我的代码片段
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,0)
image,contours,hierarchy = cv2.findContours(thresh, cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
if len(approx)==4:
x,y,w,h = cv2.boundingRect(cnt)
#print x, y, w, h
carea = cv2.contourArea(cnt, True)
# if carea is less than 0, the contour is a duplicate - counting both inner and outer edge of the rect
if carea<0:
continue
if x==0:
continue
print x, y, w, h
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),5)
cv2.imshow("Original", gray)
cv2.imshow("Contours", img)
解决方案
推荐阅读
- java - 将字节数组中的图像发送到服务器,但它不起作用
- terraform - 如何在脚本开始时在 terraform 中运行 null_resource
- vue.js - 我怎样才能多个两个字段?
- hive - 在 hive 中加入 hive 分区、分桶表,其中只有分桶表(非分区表)
- javascript - 获取选中的复选框数组,并且仅当以特定顺序选择时,然后转到 URL
- r - 将嵌套列表转换为数据框:仅提取感兴趣的特定元素
- flutter - 如何在颤动中动态使用“将商品添加到购物车”
- javascript - 引导模式不再打开
- python - 选择成分以解析树表示
- c - 如何确保输入一定数量的数字?