当您应用光学字符辨认(OCR)或任何数据或对象辨认结果时,起首要做的是预措置。这里的预措置意味着提取我们信息地址的位置。提取位置后,将对该图象实施任何机械算法。
当您必须检测位于任何表/框或行列格局的对象时,会呈现结果。假定图象是多么的,那么你必须检测边框并一一提取它们。此刻该当精确地完成一切图象。作为示例,请拜见以下图象:
用于提取信息的图象的示例
这里,关于该图象,我想要对一切等式遏制光学字符辨认。我想一一提取每个单位格(不是任何空白)来检测这些数字。提取每个单位后,我将对一切的数字遏制瓜分,并应用我的机械进修模型遏制辨认。关于这个算法,我们将应用python措辞应用opencv和numpy,一个一个末尾提取每个单位格:
起首导入一些库:
import cv2
import numpy as np
此刻读取图象,将其转换为灰度,遏制阈值措置并反转图象
# Read the image
img = cv2.imread(img_for_box_extraction_path, 0)
# Thresholding the image
(thresh, img_bin) = cv2.threshold(img, 128, 255,cv2.THRESH_BINARY| cv2.THRESH_OTSU)
# Invert the image
img_bin = 255-img_bin
cv2.imwrite("Image_bin.jpg",img_bin)
所以我们的图象看起来像多么:
此刻我们需要检测边框。为此我们将应用外形学操作。为此,我们将根据图象的长度和宽度定义矩形内核(kernel )。我们将定义两个内核。1)内核检测程度线。2)内核检测垂直线。
# Defining a kernel length
kernel_length = np.array(img).shape[1]//80
# A verticle kernel of (1 X kernel_length), which will detect all the verticle lines from the image.
verticle_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_length))
# A horizontal kernel of (kernel_length X 1), which will help to detect all the horizontal line from the image.
hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1))
# A kernel of (3 X 3) ones.
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
此刻在定义内核以后,我们将遏制外形学操作来检测垂直和程度线。下面的代码显示包含垂直线的图象。
# Morphological operation to detect vertical lines from an image
img_temp1 = cv2.erode(img_bin, verticle_kernel, iterations=3)
verticle_lines_img = cv2.dilate(img_temp1, verticle_kernel, iterations=3)
cv2.imwrite("verticle_lines.jpg",verticle_lines_img)
# Morphological operation to detect horizontal lines from an image
img_temp2 = cv2.erode(img_bin, hori_kernel, iterations=3)
horizontal_lines_img = cv2.dilate(img_temp2, hori_kernel, iterations=3)
cv2.imwrite("horizontal_lines.jpg",horizontal_lines_img)
包含垂直线的图象
包含程度线的图象
此刻我们将添加这两个图象。这将只要框,并且框中写入的信息将被删除。是以我们可以精确地检测框,并且不会呈现虚假框提取的噪音。
# Weighting parameters, this will decide the quantity of an image to be added to make a new image.
alpha = 0.5
beta = 1.0 - alpha
# This function helps to add two image with specific weight parameter to get a third image as summation of two image.
img_final_bin = cv2.addWeighted(verticle_lines_img, alpha, horizontal_lines_img, beta, 0.0)
img_final_bin = cv2.erode(~img_final_bin, kernel, iterations=2)
(thresh, img_final_bin) = cv2.threshold(img_final_bin, 128,255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imwrite("img_final_bin.jpg",img_final_bin)
终究图象仅包含框
此刻我们将对这个图象应用findContours()编制。这将找到一切的边框,我们将从上到下对它们遏制排序。为了对轮廓遏制排序,我们将应用https://www.pyimagesearch.com/2015/04/20/sorting-contours-using-python-and-opencv/供给的函数。我们将采纳自上而下的编制
# Find contours for image, which will detect all the boxes
im2, contours, hierarchy = cv2.findContours(img_final_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Sort all the contours by top to bottom.
(contours, boundingBoxes) = sort_contours(contours, method="top-to-bottom")
此刻轮回遍历一切轮廓,找到一切框的位置并裁剪具有矩形的零件并将其保管到一个文件夹中
idx = 0
for c in contours:
# Returns the location and width,height for every contour
x, y, w, h = cv2.boundingRect(c)
if (w > 80 and h > 20) and w > 3*h:
idx += 1
new_img = img[y:y+h, x:x+w]
cv2.imwrite(cropped_dir_path+str(idx) + '.png', new_img)
# If the box height is greater then 20, widht is >80, then only save it as a box in "cropped/" folder.
if (w > 80 and h > 20) and w > 3*h:
idx += 1
new_img = img[y:y+h, x:x+w]
cv2.imwrite(cropped_dir_path+str(idx) + '.png', new_img)
此刻它完成了!检讨您的文件夹,您将看到包含每个提取的框的图象。以下所示
所以此刻你可以应用这个图象进一步完成。你可以经过过程增加更改kernel_length参数来获得异常大年夜大年夜的图象中的优胜输入。
寄望:此编制合用于任何处所,用于检测从OMR表就任何Excel表的数据。该编制应用正常的外形学操作,并且它擦除一切外部信息,是以不会呈现用于缺点检测边框的噪声。您可以应用以下编建造为预措置并获得优胜的输入。:)
边框检测的完全Python代码在这里:
import cv2
import numpy as np
def box_extraction(img_for_box_extraction_path, cropped_dir_path):
img = cv2.imread(img_for_box_extraction_path, 0) # Read the image
(thresh, img_bin) = cv2.threshold(img, 128, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU) # Thresholding the image
img_bin = 255-img_bin # Invert the image
cv2.imwrite("Image_bin.jpg",img_bin)
# Defining a kernel length
kernel_length = np.array(img).shape[1]//40
# A verticle kernel of (1 X kernel_length), which will detect all the verticle lines from the image.
verticle_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_length))
# A horizontal kernel of (kernel_length X 1), which will help to detect all the horizontal line from the image.
hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1))
# A kernel of (3 X 3) ones.
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# Morphological operation to detect verticle lines from an image
img_temp1 = cv2.erode(img_bin, verticle_kernel, iterations=3)
verticle_lines_img = cv2.dilate(img_temp1, verticle_kernel, iterations=3)
cv2.imwrite("verticle_lines.jpg",verticle_lines_img)
# Morphological operation to detect horizontal lines from an image
img_temp2 = cv2.erode(img_bin, hori_kernel, iterations=3)
horizontal_lines_img = cv2.dilate(img_temp2, hori_kernel, iterations=3)
cv2.imwrite("horizontal_lines.jpg",horizontal_lines_img)
# Weighting parameters, this will decide the quantity of an image to be added to make a new image.
alpha = 0.5
beta = 1.0 - alpha
# This function helps to add two image with specific weight parameter to get a third image as summation of two image.
img_final_bin = cv2.addWeighted(verticle_lines_img, alpha, horizontal_lines_img, beta, 0.0)
img_final_bin = cv2.erode(~img_final_bin, kernel, iterations=2)
(thresh, img_final_bin) = cv2.threshold(img_final_bin, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
# For Debugging
# Enable this line to see verticle and horizontal lines in the image which is used to find boxes
cv2.imwrite("img_final_bin.jpg",img_final_bin)
# Find contours for image, which will detect all the boxes
im2, contours, hierarchy = cv2.findContours(
img_final_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Sort all the contours by top to bottom.
(contours, boundingBoxes) = sort_contours(contours, method="top-to-bottom")
idx = 0
for c in contours:
# Returns the location and width,height for every contour
x, y, w, h = cv2.boundingRect(c)
# If the box height is greater then 20, widht is >80, then only save it as a box in "cropped/" folder.
if (w > 80 and h > 20) and w > 3*h:
idx += 1
new_img = img[y:y+h, x:x+w]
cv2.imwrite(cropped_dir_path+str(idx) + '.png', new_img)
box_extraction("41.jpg", "./Cropped/")