python - 如何使用python在图像中查找字母

标签 python image opencv


我有一张包含一些手写米值的图像,我想找到字母 m 的位置,以便裁剪它并只留下数字。

这是一个例子:

原始图像:输入图像如下,实际上这是我能得到的最好的手写输入,通常情况下更差。

enter image description here

Train Image:我有一个 m 字母的多种类型的列表,从我拥有的不同手写图像中截取。

enter image description here

Resulting Image:我想要得到的结果

enter image description here

我已经尝试过使用opencv模板匹配函数但是没有用,还发现了这个github但它也使用模板匹配。 我想知道是否有任何其他方法可以解决这个问题。

最佳答案

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

  1. 找到所有轮廓;

  1. 创建边界框列表(即每个轮廓一个框);

  1. 确定其中哪一个是最右边的边界框;

  1. 使用所有其他框的 (x,y,width,height) 信息创建 ROI 并仅裁剪数字;

Python 2.7 和 OpenCV 2.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在图像中查找字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48768604/

相关文章:

python - 如何在最佳匹配上绘制边界框?

python - 如何获取列值以任何给定字符开头的 Pandas Dataframe 行

Python httpd 将监听已使用的端口,而不会在 Windows 中抛出套接字错误,但在 OS X 中不会?

python - 我怎样才能得到下图的黑白图像?

image - 比较两个矢量图

javascript - 我的翻转图像无法正常工作

php - 如何使用php调整表格中显示的图像的大小

c++ - 如何使用外部高清摄像机作为 Visual Studio、OpenCV 项目的输入?

python - 将 4 色定理应用于图形数组中存储的相邻多边形列表

c++ - 在 opencv2.4.5 中使用 createButton(Qt gui 功能)段错误