latex 表格中虚线_如何识别和修复表格识别中的虚线
latex 表格中虛線
When documents are digitalized via scanning or via photo, the image quality can suffer from wrong settings or bad conditions. In the case of table recognition, this can lead to a broken table structure. Consequently, some lines might have light flaws or even holes and the table as a whole is not recognized as a coherent system. Sometimes the tables are also created without lines on some sides of the cells. In this case, the system closes the lines with the lines of the cells above. There is a great variety of tables and cell types and as so often the proposed code might not work perfectly for all. Nonetheless, with small adaptations its useful for many cases.
通過掃描或照片對文檔進行數字化處理時,圖像質量可能會因設置錯誤或狀況不佳而受到影響。 在表識別的情況下,這可能導致表結構損壞。 因此,某些生產線可能會出現光缺陷或什至有Kong,因此整個工作臺無法識別為連貫的系統(tǒng)。 有時,表格的創(chuàng)建在單元格的某些側面也沒有線。 在這種情況下,系統(tǒng)用上面的單元格的行關閉行。 表和單元格類型多種多樣,因此通常所提出的代碼可能并不適合所有人。 但是,只需進行少量修改,它就可以在許多情況下使用。
Most cell recognition algorithms are based on lines and cell structure. Not having lines leads to a bad recognition rate due to “forgotten” cells. The same is the case for this approach. The lines are necessary. In case your table does not have clearly recognizable lines it won’t work. But now, let’s take a look at the code!
大多數細胞識別算法均基于細胞系和細胞結構。 由于“被遺忘”的細胞,沒有線會導致不良的識別率。 這種方法也是如此。 線是必需的。 如果您的表格沒有明顯可識別的行,它將無法正常工作。 但是現在,讓我們看一下代碼!
First, we need to do the imports. In this case, it is restricted only to two imports: OpenCV and NumPy.
首先,我們需要進行進口。 在這種情況下,它僅限于兩個導入:OpenCV和NumPy。
import cv2import numpy as np
Then we need to load the image/document containing the table. In case it is a whole document with text surrounding the table you need to recognize the table first and cut the image to the table size. (To read more about table recognition and cutting to the table size, click here.)
然后,我們需要加載包含表的圖像/文檔。 如果是整個文檔,并且表格周圍有文字,則需要首先識別該表格,然后將圖像切成表格大小。 (要了解有關表格識別和削減表格尺寸的更多信息, 請單擊此處 。)
# Load the imageimage = cv2.imread(‘/Users/marius/Desktop/holes.png’, -1)The input image.輸入圖像。As you can see in the input image the lines of the cells in the second row are not fully connected. In table recognition, the second row would not be recognized and considered by the algorithm because the cells are not a closed box. The solution proposed in this article works not only for this case of gaps. It works also for other broken lines or holes in tables.
如您在輸入圖像中看到的,第二行中的單元格線未完全連接。 在表識別中,由于單元格不是封閉的框,因此算法將無法識別和考慮第二行。 本文提出的解決方案不僅適用于這種情況。 它也適用于表格中的其他虛線或Kong。
Now, we need to obtain the size of the image (height and width) and store it in the variables hei and wid.
現在,我們需要獲取圖像的大小(高度和寬度)并將其存儲在變量hei和wid中。
(hei,wid,_) = image.shapeThe next step is the grayscaling and blurring via a Gaussian Filter. This helps in the recognition of the lines. For more on grayscaling, click here.
下一步是通過高斯濾鏡進行灰度和模糊處理。 這有助于識別線條。 有關灰度的更多信息, 請單擊此處 。
#Grayscale and blur the imagegray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
Then we need to threshold our image. If you want to learn a bit more about thresholding, you can read about it in this article: Click Here (It’s all the way down to the section Binarizing an image).
然后,我們需要對圖像進行閾值處理。 如果您想了解有關閾值的更多信息,可以在本文中閱讀有關閾值的信息: 單擊此處 (一直到“ 對圖像進行二值化”部分)。
#Threshold the imagethresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
The findContours algorithm of OpenCV is then used to get the position of all contours. For all contours, a bounding rectangle is drawn to create the boxes/cells of the table. The boxes are then stored in the list box with the four values x, y, width, height.
然后使用OpenCVfindContours算法獲取所有輪廓的位置。 對于所有輪廓,將繪制一個邊界矩形以創(chuàng)建表格的框/單元格。 然后將這些框與四個值x,y,寬度,高度一起存儲在列表框中。
#Retrieve contourscontours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)#Create box-list
box = []# Get position (x,y), width and height for every contour for c in contours:
x, y, w, h = cv2.boundingRect(c)
box.append([x,y,w,h])
All heights, widths, x’s and y’s are then separately stored in lists and the minimum height, width and x and y are computed. Furthermore, the maximum y and x are necessary.
然后將所有高度,寬度,x和y分別存儲在列表中,并計算最小高度,寬度以及x和y。 此外,最大y和x是必需的。
#Create separate lists for all valuesheights=[]
widths=[]
xs=[]
ys=[]#Store values in listsfor b in box:
heights.append(b[3])
widths.append(b[2])
xs.append(b[0])
ys.append(b[1])#Retrieve minimum and maximum of listsmin_height = np.min(heights)
min_width = np.min(widths)
min_x = np.min(xs)
min_y = np.min(ys)
max_y = np.max(ys)
max_x = np.max(xs)
The stored values are now used to understand where the table is located. The minimum y value can be used to get the uppermost row of the table, which can be regarded as the starting point of the table. The minimum x value is the left edge of the table. To get the approximate size we need to retrieve the maximum y value, which is the cell or row at the bottom of the table. The y-value of the last row represents the upper edge of the cell and not the bottom of the cell. To regard the whole size of the cell and the table it is necessary to add the last rows cell height to the maximum y to retrieve the full height of the table. The maximum x will be the last column and consecutively, the right-most cell/row of the table. The x-value is the left edge of each cell and consecutively, we need to add the width of the last column to the maximum x-value to retrieve the full width of the table.
現在使用存儲的值來了解表的位置。 最小y值可用于獲取表的最上一行,該行可以視為表的起點。 x的最小值是表格的左邊緣。 要獲得近似大小,我們需要檢索最大y值,該值是表底部的單元格或行。 最后一行的y值表示單元格的上邊緣,而不是單元格的底部。 要考慮單元格和表格的整體大小,必須將最后一行的單元格高度加到最大y以檢索表格的完整高度。 最大的x將是表格的最后一列,并連續(xù)地是表格的最右邊的單元格/行。 x值是每個單元格的左邊緣,并且連續(xù)地,我們需要將最后一列的寬度添加到最大x值以檢索表的完整寬度。
#Retrieve height where y is maximum (edge at bottom, last row of table)for b in box:
if b[1] == max_y:
max_y_height = b[3]#Retrieve width where x is maximum (rightmost edge, last column of table)for b in box:
if b[0] == max_x:
max_x_width = b[2]
In the next step, all horizontal and vertical lines are extracted and stored separately. This is done by creating a kernel which thresholds and applies morphological operations. The horizontal kernel has a size of (50,1). You can play around with the size depending on the size of your image. The vertical kernel has a size of (1,50).
在下一步中,將提取所有水平線和垂直線并分別存儲。 這是通過創(chuàng)建一個閾值并應用形態(tài)運算的內核來完成的。 水平內核的大小為(50,1)。 您可以根據圖像的大小來調整大小。 垂直內核的大小為(1,50)。
Morphological operations perform transformations of the detected structures based on their geometry (Soille, p.50, 1998). Dilation is one of the most applied and most basic morphological operations. If at least one pixel under the kernel is white, the pixel being viewed of the original image will be considered white. Consequently, the white areas are enlarged. Please be aware that due to inversion the background is black and foreground white, meaning that the table lines are currently white. The dilation can be seen as the most important step. The holes and broken lines are now repaired and for further table recognition, all cells will be regarded.
形態(tài)學操作根據檢測到的結構的幾何形狀進行轉換(Soille,第50頁,1998年)。 擴張是應用最廣泛,最基本的形態(tài)學操作之一。 如果內核下的至少一個像素為白色,則原始圖像的正在查看的像素將被視為白色。 因此,白色區(qū)域變大了。 請注意,由于反轉,背景為黑色,前景為白色,這意味著表格行當前為白色。 擴張可以被視為最重要的步驟。 現在修復Kong和虛線,為了進一步識別表,將考慮所有單元格。
# Obtain horizontal lines maskhorizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50,1))horizontal_mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=1)
horizontal_mask = cv2.dilate(horizontal_mask, horizontal_kernel, iterations=9)# Obtain vertical lines mask
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,50))
vertical_mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=1)
vertical_mask= cv2.dilate(vertical_mask, vertical_kernel, iterations=9)The horizontal and vertical mask of the table.表格的水平和垂直蒙版。
Both masks, the horizontal and the vertical, are then merged to one table using the bitwise_or operation of OpenCV. To retrieve the original back- and foreground the image is inverted by subtracting the cv2.bitwise_or from 255.
然后使用OpenCVbitwise_or操作將水平和垂直兩個蒙版合并到一張表中。 要檢索原始的前后前景,可通過從255中減去cv2.bitwise_or來反轉圖像。
# Bitwise-and masks togetherresult = 255 — cv2.bitwise_or(vertical_mask, horizontal_mask)Merging and inverting the horizontal and vertical mask.合并和反轉水平和垂直蒙版。
In case the table is surrounded by text and not standing alone (in my example it’s not surrounded), we cut it out and set it on a white background. Now we need the size of the table retrieved earlier. We cut the final image to the table size by using the minimum y (which is the edge at top), the maximum y + the height of the maximum y cells (which is the edge at the bottom), the minimum x (which is the left edge) and the maximum x + the width of the maximum x cells (which is the right edge). The image is then cropped to the size of the table. A new background of the original size of the document is created and completely filled with white pixels. The center of the image is retrieved and the repaired table is merged with the white background and set right into the center of the image.
如果桌子被文本包圍而不是一個人站立(在我的示例中,它沒有被包圍),我們將其剪下并放在白色背景上。 現在我們需要前面檢索的表的大小。 我們使用最小y(頂部的邊緣),最大y +最大y單元格的高度(底部的邊緣),最小x(即左邊緣)和最大x +最大x個像元的寬度(這是右邊緣)。 然后將圖像裁剪為表格的大小。 將創(chuàng)建文檔原始大小的新背景,并完全用白色像素填充。 檢索圖像的中心,將修復后的表格與白色背景合并,并設置在圖像的中心。
#Cropping the image to the table sizecrop_img = result[(min_y+5):(max_y+max_y_height), (min_x):(max_x+max_x_width+5)]#Creating a new image and filling it with white backgroundimg_white = np.zeros((hei, wid), np.uint8)
img_white[:, 0:wid] = (255)#Retrieve the coordinates of the center of the image
x_offset = int((wid — crop_img.shape[1])/2)
y_offset = int((hei — crop_img.shape[0])/2)#Placing the cropped and repaired table into the white background
img_white[ y_offset:y_offset+crop_img.shape[0], x_offset:x_offset+crop_img.shape[1]] = crop_img#Viewing the result
cv2.imshow(‘Result’, img_white)
cv2.waitKey()The resulting image with repaired broken lines.得到的圖像帶有修復的虛線。
This is the result. The method can be used for multiple typed of broken lines, gaps, and holes in tables. The result is the basis for further table recognition as explained in my other article. The explained method was applied on an empty table. You can also apply it on a table containing text or surrounded by text. For a table containing text, it is still necessary to merge the original image containing a table with data with the final image with repaired holes. This can be done with a OpenCV bitwise operation and should not be to complex.
這就是結果。 該方法可用于表格中的虛線,間隙和Kong的多種類型。 結果是進一步的表識別的基礎,正如我在另一篇文章中所解釋的。 解釋的方法應用于空表。 您也可以將其應用于包含文本或被文本包圍的表上。 對于包含文本的表,仍然有必要將包含表的原始圖像與數據與具有修復Kong的最終圖像合并。 這可以通過OpenCV按位操作來完成,并且不應太復雜。
I hope you enjoyed my tutorial and can use this for your own projects.
希望您喜歡我的教程,并可以將其用于自己的項目。
翻譯自: https://towardsdatascience.com/how-to-recognize-and-fix-broken-lines-in-table-recognition-1a19f22d59f2
latex 表格中虛線
總結
以上是生活随笔為你收集整理的latex 表格中虚线_如何识别和修复表格识别中的虚线的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 局部敏感哈希Locality Sensi
- 下一篇: 构建强化学习_如何构建强化学习项目(第1