在python中自动找到一个矩形并裁剪图像到它

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

所以说我有很多与这些类似的图像:

2 3

我正在尝试获取里面有数字的矩形,它是背景较浅的矩形,我也想去掉这些线条,但这并不那么重要,它应该是这样的:

5

我真的不知道如何解决这个问题。

这是我的代码,它识别形状并用绿色勾勒出它们的轮廓,

import numpy as np
import matplotlib.pyplot as plt
import cv2
import sys

# read the image from arguments
image = cv2.imread('OCRimgs/test2.jpg')

# convert to grayscale
grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# perform edge detection
edges = cv2.Canny(grayscale, 30, 100)

# detect lines in the image using hough lines technique
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 60, np.array([]), 50, 5)
# iterate over the output lines and draw them
for line in lines:
    for x1, y1, x2, y2 in line:
        cv2.line(image, (x1, y1), (x2, y2), color=(20, 220, 20), thickness=3)

# show the image
plt.imshow(image)
plt.show()

输出:

7

如果有帮助的话,我正在制作一个 OCR,它会接收您在顶部看到的图像并获取它们的图像,这可能看起来毫无意义,但相信我,一旦我完成它,它将对我非常有帮助。

这是我的 OCR 代码(这里没有错误)

import cv2
import pytesseract
# from PIL import Image

img = cv2.imread('small.png')

try:
    t = pytesseract.image_to_string(img, lang='lets', config='--psm 6 tessedit_char_whitelist=0123456789')
    text = int(t.replace('\n', '').replace('\f', ''))
    print(text)
except:
    print('Error processing image')

它拍摄的图像必须与这些类似:

5

编辑:

我觉得可能有一种方法可以通过使用矩形的颜色来做到这一点,因为它很亮,但我不确定它是否会起作用,因为真实数据将是从相机拍摄的图像(图像将被拍摄)来自同一地点的同一相机)

python image ocr image-recognition number-recognition
2个回答
0
投票

如果您有足够数量的输入图像,请训练模型来识别您所需的感兴趣矩形区域,并将这些检测到的区域传递给 OCR 以获取文本。


0
投票
import cv2 as cv
import numpy as np
import pytesseract
import matplotlib.pyplot as plt

# Image path
path= "/Users/gauravsingh/Downloads/ejYknlr.jpeg"

# Read the image.
image = cv.imread(path, cv.IMREAD_COLOR)

# Convert the image to grayScale
imageGray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)


# Apply Gaussian Blur to smooth the images.
imageBlur = cv.GaussianBlur(imageGray, (3, 3), sigmaX= 0)

# Perform Edge Detection
Canny = cv.Canny(imageBlur, 30, 100)

# detect lines in the image using hough lines technique
lines = cv.HoughLinesP(Canny, 1, np.pi/180, 60, np.array([]), 50, 5)
lines = sorted(lines, key=lambda line: np.linalg.norm(lines[0] -       lines[1]), reverse=False)

# Filter out lines which start and End on same points ony axis
logestLine = []

for line in lines:
  for x1, y1, x2, y2 in line:
     if y1 == y2:
        logestLine.append([(x1, y1), (x2, y2)])

# Crop the area
x1, y1= logestLine[0]
x2, y2= logestLine[1]
x= x1[0]
y = x2[1]
xi, yi= y1

imageCropped = image[y:yi, x:xi]


# Display image
plt.imshow(imageCropped[:,:,::-1])
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.