【问题标题】:Digit recognizion of non light/color digits using Python/OpenCV使用 Python/OpenCV 对非浅色/彩色数字进行数字识别
【发布时间】:2020-02-21 04:29:41
【问题描述】:

我正在使用 OpenCV 4 和最新的 Python 版本。也拿到了所有的包裹。

我知道如何调整大小、灰度、模糊、边缘等,但我得到了很多错误,好像我无法检测到。

这是裁剪后的万用表显示图像,我试图从中检测和提取数字。

我需要一个简单的代码或帮助来编码和检索数字,尝试了多个指南但没有成功。 OpenCV 对我来说是新手,但我花了 2-3 天的时间来学习基本的图像处理能力。

数字万用表非彩色

好的,现在我已经更新了代码。我想从扭曲和转换的二进制图像中提取数字。但是我得到了错误或者我需要删除部分代码才能至少向我显示这两个图像。 到目前为止的代码如下:

# import the necessary packages
from imutils.perspective import four_point_transform
from imutils import contours
import imutils
import cv2

# define the dictionary of digit segments so we can identify
# each digit on the thermostat
DIGITS_LOOKUP = {
(1, 1, 1, 0, 1, 1, 1): 0,
(0, 0, 1, 0, 0, 1, 0): 1,
(1, 0, 1, 1, 1, 1, 0): 2,
(1, 0, 1, 1, 0, 1, 1): 3,
(0, 1, 1, 1, 0, 1, 0): 4,
(1, 1, 0, 1, 0, 1, 1): 5,
(1, 1, 0, 1, 1, 1, 1): 6,
(1, 0, 1, 0, 0, 1, 0): 7,
(1, 1, 1, 1, 1, 1, 1): 8,
(1, 1, 1, 1, 0, 1, 1): 9}

# load the example image
image = cv2.imread("Multimeter_1.jpg")

# pre-process the image by converting it to
# graycale, blurring it, and computing an edge map
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7,7), 0)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 
cv2.THRESH_BINARY_INV,87,9)


# find contours in the edge map,
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)

rect = None
# loop over the contours
for c in cnts:
# approximate the contour
x,y,w,h = cv2.boundingRect(c)
#cv2.rectangle(image, (x, y), (x+w, y+h), (36, 255, 12), 1)
cv2.drawContours(image, [c], -1, (36,255,12),3)
rect = c

peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)

if len(approx) == 4:
    rect = approx
    break


# extract the display, apply a perspective transform

warped = four_point_transform(thresh, rect.reshape(4, 2))
output = four_point_transform(image, rect.reshape(4, 2))

# Warp the image and perform morphology to clean it

thresh = cv2.threshold(warped, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU) 
[1]
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)


# pre-process the image by converting it to
# graycale, blurring it, and computing an edge map
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7,7), 0)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 
cv2.THRESH_BINARY_INV,87,9)


# find contours in the edge map,
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)

rect = None
# loop over the contours
for c in cnts:
# approximate the contour
x,y,w,h = cv2.boundingRect(c)
#cv2.rectangle(image, (x, y), (x+w, y+h), (36, 255, 12), 1)
cv2.drawContours(image, [c], -1, (36,255,12),3)
rect = c

peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)

if len(approx) == 4:
    rect = approx
    break


# extract the display, apply a perspective transform

warped = four_point_transform(thresh, rect.reshape(4, 2))
output = four_point_transform(image, rect.reshape(4, 2))
# Warp the image and perform morphology to clean it

thresh = cv2.threshold(warped, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU) 
[1]
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1, 5))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

cnts = 
cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
digitCnts = []

# loop over the digit area candidates
for c in cnts:
(x,y,w,h) = cv2.boundingRect(c)

# if the contour is sufficiently large, it must be a digit
if w >= 25 and (h >= 50 and h <= 60):
digitCnts.append(c)


cv2.imshow("Multimeter", image)
cv2.imshow("Multimeter2", thresh)


cv2.waitKey(0)
cv2.destroyAllWindows()

Warped,Transformed, tresholded

【问题讨论】:

  • 请提供您尝试过的代码示例 - 这将使人们更容易回答您的问题。

标签: python opencv image-processing image-recognition digits


【解决方案1】:

我很难为此编写正确的代码,仍在学习过程中......

我拍摄了那个变形的图像并制作了另一个 Python 脚本来简化事情。现在让我们说图像已经是二进制、转换和扭曲的。提取后如何检测数字并在图像或控制台上显示信息?我还没有学会这样做。我很难找到数字的坐标,我只是找到了图像的矩形边界,但不是我要找的。

换句话说:我需要帮助找到第一个数字的边界框,检测并提取它们。

Image example

到目前为止,这里是简化的示例代码:

# import the necessary packages
from imutils.perspective import four_point_transform
from imutils import contours
import imutils
import cv2
import numpy

image = cv2.imread("Morph.jpg")
copy = image.copy()
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

cnts = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

ROI_number = 0
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
ROI = image[y:y+h, x:x+w]
cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
cv2.rectangle(copy,(x,y),(x+w,y+h),(36,255,12),2)
ROI_number += 1



cv2.imshow('thresh', image)
cv2.imshow('copy', copy)
cv2.imwrite("Morphed_rectangle.jpg",copy)

cv2.waitKey(0)
cv2.destroyAllWindows()

【讨论】:

  • 那么,如何从上面的二进制图像和代码中提取数字呢?有人有任何提示吗?我使用最新的 OpenCV 和 python
【解决方案2】:

您可以使用cv2.adaptiveThreshold() 获取二值图像,然后通过对最大轮廓进行排序来提取显示。一旦你检测到这个轮廓,你就可以应用你的透视变换

import cv2

image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7,7), 0)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,87,9)

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)

rect = None
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    cv2.drawContours(image, [c], -1, (36,255,12), 3)
    # cv2.rectangle(image, (x, y), (x+w, y+h), (36, 255, 12), 1)
    rect = c
    break

cv2.imshow('image', image)
cv2.waitKey()

【讨论】:

  • 好的,非常感谢您的帮助 nathanacy,所以现在我找到了轮廓并执行了透视变换。我该如何进行?需要再次设置阈值,在实际数字的位置绘制轮廓并提取它们?
  • 透视变换后,我会再次阈值,然后进行形态学操作以提取每个数字
  • 不要编辑答案以试图回复。使用评论部分或编辑您自己的帖子
  • 正如你从我上面看到的和更新的代码中看到的那样。我现在需要帮助来实际检测数字,这意味着绘制一个边界框,然后提取它们并将检测到的数字打印到图像或控制台。
猜你喜欢
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 2021-04-10
  • 2011-12-25
  • 2016-08-06
  • 1970-01-01
相关资源
最近更新 更多