【问题标题】:How to extract digit in image that overide rect area如何提取覆盖矩形区域的图像中的数字
【发布时间】:2018-12-23 01:50:34
【问题描述】:

现在,我有一个项目使用图像处理来检测数字,但我不知道当数字覆盖矩形区域时如何提取。

这是输入:

我想得到类似底部图片的东西:

【问题讨论】:

  • 请使用您已经尝试过的任何代码更详细地解释问题。另外,请更具体地了解标签。你的标签暗示了三件通常不在一起的事情。
  • 对不起,我现在没有代码。我想知道算法。我想显示图片,但我无法在堆栈溢出中上传。现在我已经更新了问题。请检查
  • 你必须使用 OpenCV 吗?
  • 我认为没有。但我不知道在 android 中使用的图像库了

标签: opencv image-processing convex-hull


【解决方案1】:

可以使用 OpenCV 使用轮廓概念提取数字。以下实现是在 Python 中。

执行以下步骤:

  • 将您的图像转换为灰度图像
  • 应用适当的阈值,使数字为白色,而其他所有数字均为黑色。
  • 现在找到轮廓。由于您有小星星,因此它们也会被拾取。为避免捡起它们,请通过一个合适的区域,在该区域上方必须找到轮廓。
  • 计算每个轮廓的凸包并在原始图像上对其进行遮罩。

提取轮廓后,您需要使用单个凸包。

代码:

img = cv2.imread(r'C:\Users\Jackson\Desktop\2_new.jpg', 1)
img2 = img.copy()
cv2.imshow("original.jpg", img)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

_, threshed_img = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
image, contours, hier = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

#--- Black image to be used to draw individual convex hull ---
black = np.zeros_like(img)
#cv2.imshow("black.jpg", black)

contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0]) #added by OP : this sorts contours left to right, so images come in order

for cnt in contours:
    if cv2.contourArea(cnt) > 200:
        hull = cv2.convexHull(cnt)

        black2 = black.copy()

        #--- Here is where I am filling the contour after finding the convex hull ---
        cv2.drawContours(black2, [hull], -1, (255, 255, 255), -1)
        g2 = cv2.cvtColor(black2, cv2.COLOR_BGR2GRAY)
        _, t2 = cv2.threshold(g2, 127, 255, cv2.THRESH_BINARY)
        cv2.imshow("t2.jpg", t2)

        masked = cv2.bitwise_and(img2, img2, mask = t2)    
        cv2.imshow("masked.jpg", masked)

        print(len(hull))
        cv2.waitKey(0)

cv2.destroyAllWindows()

你应该能够得到类似以下的东西:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-30
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 2020-08-27
    • 2019-11-05
    相关资源
    最近更新 更多