【问题标题】:How to Segment handwritten and printed digit without losing information in opencv?如何在不丢失opencv信息的情况下分割手写和打印的数字?
【发布时间】:2019-03-30 10:33:24
【问题描述】:

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

示例:

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

【问题讨论】:

  • 如果我理解你的问题,你有两个问题,1)数字的底部可以被裁剪,2)如何从 BW 图像中分割数字(对象).. 对吗?
  • 是的,你是对的。

标签: python-3.x opencv image-processing computer-vision digits


【解决方案1】:

从图像中分割字符 -

方法-

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

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)

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

输入图像-

阈值 -

扩张-

轮廓 -

保存的字符 -

【讨论】:

  • 由于隐私问题,您能否从您的答案中删除图片?
  • 好的,我会用不同的图片替换这些图片
【解决方案2】:

手写数字被腐蚀/裁剪的问题: 你可以在识别步骤中解决这个问题,甚至在图像改进步骤(识别之前)。

  • 如果只裁剪了一小部分数字(例如您的图像示例),则将图像填充 1 或 2 个像素就足以使分割过程变得容易。或者一些形态过滤器(扩张)即使在填充之后也可以改善你的数字。 (这些解决方案在 Opencv 中可用)
  • 如果裁剪了足够好的数字部分,则需要将降级/裁剪的数字模式添加到用于数字识别算法的训练数据集中(即数字 3 以及所有可能的裁剪情况......等)

字符分离问题:

  • opencv 提供的斑点检测算法可以很好地解决您的问题(为凹凸参数选择正确的值)

  • opencv 还提供轮廓检测器(canny() 函数),它有助于检测角色的轮廓,然后您可以找到合适的边界(Opencv 也提供:cv2.approxPolyDP(contour,..,..)) 每个字符周围的框

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 2020-01-11
    相关资源
    最近更新 更多