Python-OpenCV数字分割

问题描述 投票:0回答:1

对于我的图像处理课程,我自己选择了数字识别项目。在该项目中,目的是识别模拟水煤气表上的数字。我的第一个目标是找到数字的位置,并在其上绘制一个矩形。后来我想拍摄矩形图像并对其应用机器学习以识别数字。目前,我停留在数字分割部分。您有什么建议?

“示例图像”“>

import cv2
import imutils
from imutils.perspective import four_point_transform
from imutils import contours
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

img = cv2.imread('/Users/USER/Desktop/digitSegmentation/picture2.jpg')

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

#Image blurring
blurred = cv2.GaussianBlur(gray, (5, 5), 0)

# Thresholding
ret, thresh_img = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY)

# Canny edge detection
edges = cv2.Canny(blurred, 50, 200, 255)

cnts = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
displayCnt = None

for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)

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

thresh = cv2.threshold(edges, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 6))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

lower_blue = np.array([50, 50, 0])
upper_blue = np.array([120, 255, 255])
mask = cv2.inRange(hsv, lower_blue, upper_blue)




img_rect = cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)


cv2.imshow('Original image', img)
cv2.imshow('Gray image', gray)
cv2.imshow("Threshold image", thresh_img)
cv2.imshow('Edged image', edges)
cv2.imshow('Rectangle drawing', img_rect)
cv2.imshow('kernel',kernel)
cv2.imshow('Thresholding with Canny',thresh)
cv2.imshow('Masked image',mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

对于我的图像处理课程,我自己选择了数字识别项目。在该项目中,目的是识别模拟水煤气表上的数字。我的首要目标是找到,其中...

python numpy opencv image-processing image-segmentation
1个回答
0
投票

我需要更多说明/上下文才能为您提供适当的答案。您可以提供更多图片吗?通常的方法是使用ocr(例如tesseract,它易于使用,您可以在opencv中使用它)来查找出现的五个数字,一个逗号然后是3个digist和cube meter单位。这也将为您提供边界框(可能会被检测为单独的单词,因此您可能需要合并步骤。)

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