提问人:Anthony Kercher 提问时间:7/6/2023 最后编辑:Christoph RackwitzAnthony Kercher 更新时间:8/27/2023 访问量:90
在python中自动找到一个矩形并将图像裁剪成它
Automatically find a rectangle and crop image to it in python
问:
所以说我有很多与这些类似的图像:
我正在尝试获得带有数字的矩形,它是背景较浅的矩形,我也想摆脱这些线条,但这并不是那么重要,这是它应该看起来像的样子:
我真的不知道如何解决这个问题。
这是我拥有的代码,它可以识别形状并用绿色勾勒出它们,
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()
输出:
如果它有助于我制作一个 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')
它拍摄的图像必须类似于以下内容:
编辑:
我觉得有一种方法可以通过使用矩形的颜色来做到这一点,因为它是浅色的,但我不确定它是否有效,因为真实数据将是从相机拍摄的图像(图像将从同一位置的同一相机拍摄)
答:
0赞
User
7/8/2023
#1
如果您有足够数量的输入图像,请训练模型以识别所需的感兴趣区域矩形,并将这些检测到的区域传递给 OCR 以获取文本。
评论
0赞
Gaurav Raj Singh Manohar
8/27/2023
#2
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()
结果如下:CroppedImage
评论