如何检测图像中的单独数字?

问题描述 投票:2回答:2

我有一个类似于以下的图像。我想分离两个数字74,如图所示,因为我想为这两个对象中的每一个都有一个边界框。

enter image description here

我怎么能用OpenCV做到这一点?我不知道,我怎么能这样做,并且正在考虑使用Sobel算子是否有某种方法。我厌倦的唯一一件事就是获得Sobel。

s = cv2.Sobel(img, cv2.CV_64F,1,0,ksize=5)

enter image description here

但不知道如何从这里开始。

python opencv image-processing computer-vision
2个回答
3
投票

要分割和检测图像中的图形,主要思想如下:

  1. 使用cv2.cvtColor()将图像转换为灰度
  2. 使用cv2.GaussianBlur()模糊图像
  3. cv2.Canny()找到边缘
  4. 使用cv2.findContours()查找轮廓
  5. 遍历每个轮廓 使用cv2.boundingRect()获取边界矩形 使用Numpy切片查找每个轮廓的ROI 使用cv2.Rectangle()绘制边界框矩形

模糊的enter image description here

Canny边缘检测enter image description here

检测到的轮廓enter image description here

产量

检测到轮廓:2

import numpy as np
import cv2

original_image = cv2.imread("1.png")
image = original_image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
canny = cv2.Canny(blurred, 120, 255, 1)

cv2.imshow("canny", canny)

# Find contours in the image
cnts = cv2.findContours(canny.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

contours = []

for c in cnts:
    # Obtain bounding rectangle for each contour
    x,y,w,h = cv2.boundingRect(c)

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

    # Draw bounding box rectangle
    cv2.rectangle(original_image,(x,y),(x+w,y+h),(0,255,0),3)
    contours.append(c)

cv2.imshow("detected", original_image) 
print('contours detected: {}'.format(len(contours)))
cv2.waitKey(0)

1
投票

按照步骤:

  1. 将图像转换为灰度。
  2. 使用阈值将图像转换为二进制图像,在您的问题中我认为adaptive gausian将是最有益的。
  3. 应用轮廓检测​​,然后您可以围绕轮廓制作边界框。

您可能需要根据大小或位置过滤轮廓。

© www.soinside.com 2019 - 2024. All rights reserved.