日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

latex 表格中虚线_如何识别和修复表格识别中的虚线

發(fā)布時(shí)間:2023/12/15 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 latex 表格中虚线_如何识别和修复表格识别中的虚线 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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.

通過掃描或照片對(duì)文檔進(jìn)行數(shù)字化處理時(shí),圖像質(zhì)量可能會(huì)因設(shè)置錯(cuò)誤或狀況不佳而受到影響。 在表識(shí)別的情況下,這可能導(dǎo)致表結(jié)構(gòu)損壞。 因此,某些生產(chǎn)線可能會(huì)出現(xiàn)光缺陷或什至有Kong,因此整個(gè)工作臺(tái)無法識(shí)別為連貫的系統(tǒng)。 有時(shí),表格的創(chuàng)建在單元格的某些側(cè)面也沒有線。 在這種情況下,系統(tǒng)用上面的單元格的行關(guān)閉行。 表和單元格類型多種多樣,因此通常所提出的代碼可能并不適合所有人。 但是,只需進(jìn)行少量修改,它就可以在許多情況下使用。

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!

大多數(shù)細(xì)胞識(shí)別算法均基于細(xì)胞系和細(xì)胞結(jié)構(gòu)。 由于“被遺忘”的細(xì)胞,沒有線會(huì)導(dǎo)致不良的識(shí)別率。 這種方法也是如此。 線是必需的。 如果您的表格沒有明顯可識(shí)別的行,它將無法正常工作。 但是現(xiàn)在,讓我們看一下代碼!

First, we need to do the imports. In this case, it is restricted only to two imports: OpenCV and NumPy.

首先,我們需要進(jìn)行進(jìn)口。 在這種情況下,它僅限于兩個(gè)導(dǎo)入:OpenCV和NumPy。

import cv2
import 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.)

然后,我們需要加載包含表的圖像/文檔。 如果是整個(gè)文檔,并且表格周圍有文字,則需要首先識(shí)別該表格,然后將圖像切成表格大小。 (要了解有關(guān)表格識(shí)別和削減表格尺寸的更多信息, 請(qǐng)單擊此處 。)

# 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.

如您在輸入圖像中看到的,第二行中的單元格線未完全連接。 在表識(shí)別中,由于單元格不是封閉的框,因此算法將無法識(shí)別和考慮第二行。 本文提出的解決方案不僅適用于這種情況。 它也適用于表格中的其他虛線或Kong。

Now, we need to obtain the size of the image (height and width) and store it in the variables hei and wid.

現(xiàn)在,我們需要獲取圖像的大小(高度和寬度)并將其存儲(chǔ)在變量hei和wid中。

(hei,wid,_) = image.shape

The 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.

下一步是通過高斯濾鏡進(jìn)行灰度和模糊處理。 這有助于識(shí)別線條。 有關(guān)灰度的更多信息, 請(qǐng)單擊此處 。

#Grayscale and blur the image
gray = 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).

然后,我們需要對(duì)圖像進(jìn)行閾值處理。 如果您想了解有關(guān)閾值的更多信息,可以在本文中閱讀有關(guān)閾值的信息: 單擊此處 (一直到“ 對(duì)圖像進(jìn)行二值化”部分)。

#Threshold the image
thresh = 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算法獲取所有輪廓的位置。 對(duì)于所有輪廓,將繪制一個(gè)邊界矩形以創(chuàng)建表格的框/單元格。 然后將這些框與四個(gè)值x,y,寬度,高度一起存儲(chǔ)在列表框中。

#Retrieve contours
contours, 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分別存儲(chǔ)在列表中,并計(jì)算最小高度,寬度以及x和y。 此外,最大y和x是必需的。

#Create separate lists for all values
heights=[]
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.

現(xiàn)在使用存儲(chǔ)的值來了解表的位置。 最小y值可用于獲取表的最上一行,該行可以視為表的起點(diǎn)。 x的最小值是表格的左邊緣。 要獲得近似大小,我們需要檢索最大y值,該值是表底部的單元格或行。 最后一行的y值表示單元格的上邊緣,而不是單元格的底部。 要考慮單元格和表格的整體大小,必須將最后一行的單元格高度加到最大y以檢索表格的完整高度。 最大的x將是表格的最后一列,并連續(xù)地是表格的最右邊的單元格/行。 x值是每個(gè)單元格的左邊緣,并且連續(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).

在下一步中,將提取所有水平線和垂直線并分別存儲(chǔ)。 這是通過創(chuàng)建一個(gè)閾值并應(yīng)用形態(tài)運(yùn)算的內(nèi)核來完成的。 水平內(nèi)核的大小為(50,1)。 您可以根據(jù)圖像的大小來調(diào)整大小。 垂直內(nèi)核的大小為(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)學(xué)操作根據(jù)檢測(cè)到的結(jié)構(gòu)的幾何形狀進(jìn)行轉(zhuǎn)換(Soille,第50頁,1998年)。 擴(kuò)張是應(yīng)用最廣泛,最基本的形態(tài)學(xué)操作之一。 如果內(nèi)核下的至少一個(gè)像素為白色,則原始圖像的正在查看的像素將被視為白色。 因此,白色區(qū)域變大了。 請(qǐng)注意,由于反轉(zhuǎn),背景為黑色,前景為白色,這意味著表格行當(dāng)前為白色。 擴(kuò)張可以被視為最重要的步驟。 現(xiàn)在修復(fù)Kong和虛線,為了進(jìn)一步識(shí)別表,將考慮所有單元格。

# 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操作將水平和垂直兩個(gè)蒙版合并到一張表中。 要檢索原始的前后前景,可通過從255中減去cv2.bitwise_or來反轉(zhuǎn)圖像。

# Bitwise-and masks together
result = 255 — cv2.bitwise_or(vertical_mask, horizontal_mask)Merging and inverting the horizontal and vertical mask.合并和反轉(zhuǎn)水平和垂直蒙版。

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.

如果桌子被文本包圍而不是一個(gè)人站立(在我的示例中,它沒有被包圍),我們將其剪下并放在白色背景上。 現(xiàn)在我們需要前面檢索的表的大小。 我們使用最小y(頂部的邊緣),最大y +最大y單元格的高度(底部的邊緣),最小x(即左邊緣)和最大x +最大x個(gè)像元的寬度(這是右邊緣)。 然后將圖像裁剪為表格的大小。 將創(chuàng)建文檔原始大小的新背景,并完全用白色像素填充。 檢索圖像的中心,將修復(fù)后的表格與白色背景合并,并設(shè)置在圖像的中心。

#Cropping the image to the table size
crop_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.得到的圖像帶有修復(fù)的虛線。

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.

這就是結(jié)果。 該方法可用于表格中的虛線,間隙和Kong的多種類型。 結(jié)果是進(jìn)一步的表識(shí)別的基礎(chǔ),正如我在另一篇文章中所解釋的。 解釋的方法應(yīng)用于空表。 您也可以將其應(yīng)用于包含文本或被文本包圍的表上。 對(duì)于包含文本的表,仍然有必要將包含表的原始圖像與數(shù)據(jù)與具有修復(fù)Kong的最終圖像合并。 這可以通過OpenCV按位操作來完成,并且不應(yīng)太復(fù)雜。

I hope you enjoyed my tutorial and can use this for your own projects.

希望您喜歡我的教程,并可以將其用于自己的項(xiàng)目。

翻譯自: https://towardsdatascience.com/how-to-recognize-and-fix-broken-lines-in-table-recognition-1a19f22d59f2

latex 表格中虛線

總結(jié)

以上是生活随笔為你收集整理的latex 表格中虚线_如何识别和修复表格识别中的虚线的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。