将单词的图像分解为不同的字符图像 [英] Break the image of word into different images of character

查看:135
本文介绍了将单词的图像分解为不同的字符图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如图所示的单词二进制图像,我想用不同图像中的每个字符来分解图像.输出应具有k,7,2,f,5&的不同图像.米我尝试在python中使用opencv,但是由于某些原因,我无法将其破坏.如果我能在每个文本上画一个方框,那也足够了.

I have a binary image of words as shown, and I want break the image with each character in different image. Output should have different images of k,7,2,f,5 & m. I tried using opencv in python, but due to some reason I'm not able to break it. If I can plot a box over each text then also, it'll be good enough.

推荐答案

这是一种简单的方法:

  • 转换为灰度
  • 大津的门槛
  • 查找轮廓,从左至右对轮廓进行排序,并使用轮廓区域进行过滤
  • 提取投资回报率

在Otsu阈值化后获得二进制图像后,我们使用

After Otsu's thresholding to obtain a binary image, we sort contours from left-to-right using imutils.contours.sort_contours(). This ensures that when we iterate through each contour, we have each character in the correct order. In addition, we filter using a minimum threshold area to remove small noise. Here's the detected characters

我们可以使用Numpy切片来提取每个字符.这是每个保存的角色的投资报酬率

We can extract each character using Numpy slicing. Here's each saved character ROI

如果您想采用其他方式,只需将其反转

If you want the other way, simply invert it

ROI = 255 - image[y:y+h, x:x+w]

import cv2
from imutils import contours

image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray,0,255,cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts, _ = contours.sort_contours(cnts, method="left-to-right")

ROI_number = 0
for c in cnts:
    area = cv2.contourArea(c)
    if area > 10:
        x,y,w,h = cv2.boundingRect(c)
        ROI = 255 - image[y:y+h, x:x+w]
        cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
        cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 1)
        ROI_number += 1
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()

这篇关于将单词的图像分解为不同的字符图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆