如何在不丢失opencv信息的情况下分割手写和打印的数字? [英] How to Segment handwritten and printed digit without losing information in opencv?

查看:22
本文介绍了如何在不丢失opencv信息的情况下分割手写和打印的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个算法,可以检测打印和手写数字并对其进行分割,但是在使用滑雪图像包中的 clear_border 删除外部矩形手写数字时会丢失.任何阻止信息的建议.

I've written an algorithm that would detect printed and handwritten digit and segment it but while removing outer rectangle handwritten digit is lost using clear_border from ski-image package. Any suggestion to prevent information.

示例:

如何分别获取全部 5 个字符?

How to get all 5 characters separately?

推荐答案

从图像中分割字符 -

方法-

  1. 对图像设置阈值(将其转换为 BW)
  2. 执行扩张
  3. 检查轮廓是否足够大
  4. 查找矩形轮廓
  5. 获取 ROI 并保存角色

Python 代码 -

Python Code -

# import the necessary packages
import numpy as np
import cv2
import imutils

# load the image, convert it to grayscale, and blur it to remove noise
image = cv2.imread("sample1.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)

# threshold the image
ret,thresh1 = cv2.threshold(gray ,127,255,cv2.THRESH_BINARY_INV)

# dilate the white portions
dilate = cv2.dilate(thresh1, None, iterations=2)

# find contours in the image
cnts = cv2.findContours(dilate.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]

orig = image.copy()
i = 0

for cnt in cnts:
    # Check the area of contour, if it is very small ignore it
    if(cv2.contourArea(cnt) < 100):
        continue

    # Filtered countours are detected
    x,y,w,h = cv2.boundingRect(cnt)

    # Taking ROI of the cotour
    roi = image[y:y+h, x:x+w]

    # Mark them on the image if you want
    cv2.rectangle(orig,(x,y),(x+w,y+h),(0,255,0),2)

    # Save your contours or characters
    cv2.imwrite("roi" + str(i) + ".png", roi)

    i = i + 1 

cv2.imshow("Image", orig) 
cv2.waitKey(0)

首先,我对图像进行阈值处理以将其转换为黑白.我将图像的白色部分和背景中的字符变为黑色.然后我将图像放大以使字符(白色部分)变粗,这样可以很容易地找到合适的轮廓.然后find findContours 方法用于查找轮廓.然后我们需要检查轮廓是否足够大,如果轮廓不够大则忽略它(因为该轮廓是噪声).然后使用 boundingRect 方法找到轮廓的矩形.最后保存并绘制检测到的轮廓.

First of all I thresholded the image to convert it to black n white. I get characters in white portion of image and background as black. Then I Dilated the image to make the characters (white portions) thick, this will make it easy to find the appropriate contours. Then find findContours method is used to find the contours. Then we need to check that the contour is large enough, if the contour is not large enough then it is ignored ( because that contour is noise ). Then boundingRect method is used to find the rectangle for the contour. And finally, the detected contours are saved and drawn.

输入图像-

阈值 -

扩张-

轮廓 -

保存的字符 -

这篇关于如何在不丢失opencv信息的情况下分割手写和打印的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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