您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用python在Image中查找字母

5b51 2022/1/14 8:21:35 python 字数 6084 阅读 491 来源 www.jb51.cc/python

我有一个图像,其中包含一些米手写的值,我想找到字母m的位置,所以我可以裁剪它,只留下数字.这是一个例子:原始图像:输入图像如下所示,实际上这是我能得到的最好的手写输入,通常情况会更糟.火车图像:我有一个m字母的列表,从我有的不同手写图像切割.结果图:我想得到的结果我已经尝试过使用opencv模板匹配功能,但它不起作用,也发现这个github,但它也使用模板匹

概述

我有一个图像,其中包含一些米手写的值,我想找到字母m的位置,所以我可以裁剪它,只留下数字.

这是一个例子:

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

enter image description here

火车图像:我有一个m字母的列表,从我有的不同手写图像切割.

enter image description here

结果图:我想得到的结果

enter image description here

我已经尝试过使用opencv模板匹配功能,但它不起作用,也发现这个github,但它也使用模板匹配.
我想知道是否还有其他方法可以解决这个问题.

>找到所有轮廓;

 

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

 

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

 

>使用所有其他框的(x,y,宽度,高度)信息来创建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,-1,(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,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,w,h = bb
#   cv2.rectangle(img_Boxes,(x,y),(x+w,y+h),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,h = bb_list[-1]
#cv2.rectangle(img,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 
_,_ = 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=","w=","h=",h)

# debug: draw the final region of interest
roi_img = img.copy()
cv2.rectangle(roi_img,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在Image中查找字母全部内容,希望文章能够帮你解决如何使用python在Image中查找字母所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶