如何在不丢失opencv信息的情况下分割手写和打印数字?

Zar*_*ara 3 opencv image-processing digits computer-vision python-3.x

我编写了一种能够检测打印和手写数字并对其进行分割的算法,但使用clear -border从ski-image包中删除外部矩形手写数字时丢失了.任何防止信息的建议.

样品:
在此输入图像描述

如何分别获得所有5个字符?

Dev*_*sad 6

从图像中分割字符 -

方法 -

  1. 阈值图像(将其转换为BW)
  2. 进行扩张
  3. 检查轮廓是否足够大
  4. 找到矩形轮廓
  5. 获取投资回报率并保存角色

Python代码 -

# 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)
Run Code Online (Sandbox Code Playgroud)

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

输入图像 -

输入

门槛 -

Thresh的

扩张 -

膨胀

轮廓 -

等高线

保存的字符 -

CHAR2 char0 CHAR1 CHAR3