python - 使用 Python/OpenCV 对非浅色/彩色数字进行数字识别

标签 python opencv image-processing image-recognition digits

我正在使用 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 脚本来简化事情。现在假设图像已经是二进制的、变换的和扭曲的。提取后如何检测数字并在图像或控制台上显示信息?我还没有学会这样做。我很难找到数字的坐标,我只是找到了图像的矩形边界,但不是我要找的。

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

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()

关于python - 使用 Python/OpenCV 对非浅色/彩色数字进行数字识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58548034/

相关文章:

python - 不支持的输入图像深度 : 'VDepth::contains(depth)' where 'depth' is 4 (CV_32S)

python - 将图像中的红线转换为 numpy 列表

android - Libpng 漏洞

python - 使用OpenCV删除车牌边框(python)

python - 使用 Telegram Bot 按计划发送消息

python - 使用 Django 作为 openlayer UI 的 map 源

matlab - 使用MatLab计算信号:noise ratio

c++ - 将白色像素区域合并为一个区域 opencv c++

python - Pandas :根据特定列的值计数选择行

python - 将 df1 与 df2 匹配并将其替换为索引值(非内部连接)