字母似乎总是在数字的末尾。如果这是真的,您可以采用更简单的方法:找到所有轮廓

创建边界框列表(即每个轮廓对应一个框)

确定哪一个是最右边的边界框

使用所有其他框的(x,y,width,height)信息来创建一个ROI并只裁剪数字

Python 2.7和opencv2.4的源代码:import cv2

### load input image and convert it to grayscale

img = cv2.imread("input.png")

print("img shape=", img.shape)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#### extract all contours

_, contours, _ = cv2.findContours(gray.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# debug: draw all contours

#cv2.drawContours(img, contours, -1, (0, 0, 255), 2)

#cv2.imwrite("all_contours.jpg", img)

#### create one bounding box for every contour found

bb_list = []

for c in contours:

bb = cv2.boundingRect(c)

# save all boxes except the one that has the exact dimensions of the image (x, y, width, height)

if (bb[0] == 0 and bb[1] == 0 and bb[2] == img.shape[1] and bb[3] == img.shape[0]):

continue

bb_list.append(bb)

# debug: draw boxes

#img_boxes = img.copy()

#for bb in bb_list:

# x,y,w,h = bb

# cv2.rectangle(img_boxes, (x, y), (x+w, y+h), (0, 0, 255), 2)

#cv2.imwrite("boxes.jpg", img_boxes)

#### sort bounding boxes by the X value: first item is the left-most box

bb_list.sort(key=lambda x:x[0])

# debug: draw the last box of the list (letter M)

#print("letter M @ ", bb_list[-1])

#x,y,w,h = bb_list[-1]

#cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2)

#cv2.imwrite("last_contour.jpg", img)

### remove the last item from the list, i.e. remove box for letter M

bb_list = bb_list[:-1]

### and now the fun part: create one large bounding box to rule them all

x_start, _, _, _ = bb_list[0]

x_end, _, w_end, _ = bb_list[-1]

x = x_start

w = (x_end + w_end) - x_start

bb_list.sort(key=lambda y:y[1]) # sort by Y value: the first item has the smallest Y value

_, y, _, _ = bb_list[0]

bb_list.sort(key=lambda y:y[3]) # sort by Height value: the last item has the largest Height value

_, _, _, h = bb_list[-1]

print("x=", x, "y=", y, "w=", w, "h=", h)

# debug: draw the final region of interest

roi_img = img.copy()

cv2.rectangle(roi_img, (x, y), (x+w, y+h), (0, 0, 255), 2)

cv2.imwrite("roi.jpg", roi_img)

# crop to the roi

crop_img = img[y:y+h, x:x+w]

cv2.imwrite("crop.jpg", crop_img)

python图片找字_如何用python查找图像中的字母相关推荐

  1. 怎么用python读取excel图_如何用Python读取Excel中图片?

    公众号: 早起Python 作者:刘早起 大家好,在使用Python进行办公自动化操作时,一定少不了与Excel表格的交互,我们通常是用pandas处理表格数据,但大多数情况下,都是读取表格中的数值进 ...

  2. python如何让图片镜像翻转_如何用 Python 增量备份 Roam Research 笔记图片?

    消除后顾之忧,轻松输入卡片笔记. 题图:Photo by Markus Spiske on Unsplash 前些日子,我用视频的方式给你介绍了 Roam Research 这款工具.如果你还没有来得 ...

  3. python用import xlwt出现红字_如何用python处理excel

    最近看到有很多的python课程是教人怎么用python处理excel,我看了一下价格收费还贼高...这么初级毫无水平的操作我的粉丝们就不要花钱去报课程了..我免费教你们怎么做. 首先我们先要安装两个 ...

  4. python 矩阵运算 for循环_如何用 Python 科学计算中的矩阵替代循环

    展开全部 因为在Mathematica中使用循环确实是低效的.32313133353236313431303231363533e78988e69d8331333361313961..... 深层次的原 ...

  5. 如何制作python检查小软件_如何用Python制作整蛊小程序

    原标题:如何用Python制作整蛊小程序 下面的整蛊程序,千万不要发代码,否则就实现不了你整蛊的目的了.完成后一定要打包成一个exe程序,再发给朋友使用 . 1. 使用 pip install pyi ...

  6. 用python做一张图片_如何用python下载一张图片

    如何用python下载一张图片 这里要用到的主要工具是requests这个工具,需要先安装这个库才能使用,该库衍生自urllib这个库,但是要比它更好用.多数人在做爬虫的时候选择它,是个不错的选择. ...

  7. python可视化迷宫求解_如何用 Python 制作一个迷宫游戏

    相信大家都玩过迷宫的游戏,对于简单的迷宫,我们可以一眼就看出通路,但是对于复杂的迷宫,可能要仔细寻找好久,甚至耗费数天,然后可能还要分别从入口和出口两头寻找才能找的到通路,甚至也可能找不到通路. 虽然 ...

  8. python编程代码画画_如何用python编写一个绘制马赛克图像的自写程序

    Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发. 这篇教程将会展示如何用python的图形化包"Pygame"和基础的文件I/O来创建一 ...

  9. python 文本翻译 项目_如何用python批量翻译文本?

    首先,看一下百度翻译的官方api文档. http://api.fanyi.baidu.com/api/trans/product/apidoc # coding=utf-8 #authority:bi ...

最新文章

  1. 面试系列12 redis和memcached有什么区别
  2. 类加载器-线程上下文
  3. python可变序列_python序列中可变数据类型有什么
  4. 记一次解决问题的掉坑过程
  5. 重磅公开!阿里语音识别模型端核心技术,让你“听”见未来
  6. GPUImage – 亮度平均 GPUImageLuminosity
  7. python前端调用后端模型_前端调用后端的方法(基于restful接口的mvc架构)
  8. html常用的符号实体
  9. VMware 安装 Linux---错误-未找到要在其中创建新文件系统的有效设备
  10. css如何让图片不平铺,css怎么设置图片不平铺
  11. [转载]Web前端开发工程师编程能力飞升之路
  12. 小甲鱼python【easyGUI】学习笔记
  13. 多功能s扫描器 php168,S扫描器–速度惊人的扫描器
  14. 局域网唤醒(Wake On LAN)+树莓派实现远程设备唤醒
  15. 人工神经网络编程内容,神经网络用什么编程
  16. 轻量化网络(二)MobileNetV2: Inverted Residuals and Linear Bottlenecks
  17. CAD闪退的解决方法
  18. Excel引用外部数据链接地址修改/引用地址修改/公式更改
  19. 主机ping不通虚拟机 TTL传输中过期的解决办法
  20. Red_Hat_Linux忘记root密码解决办法

热门文章

  1. ng-repeat part2 - How li ng-repeat=nameF in Ionames{{nameF}}/li is parsed
  2. where is os type and version determined for a ui5 html
  3. SAP CRM WebClient UI on new focus工作原理
  4. Backbone - create model
  5. Webpack 10分钟入门
  6. python网络套接字_Python网络编程 Python套接字编程
  7. access工资明细表_《ACCESS》工资管理完整(整理).doc
  8. python兼职平台信号处理_如何在Windows机器上处理python中的信号
  9. python中的def语句_Python def 函数
  10. java array 元素的位置_java中的两种排序工具Arrays和Collections的使用