問題描述
我有 .pdf
文件已轉換為該項目的 .jpg
圖像.我的目標是識別您通常會在 .pdf
表單中找到的空白(例如 ____________),這些空白指示用戶填寫某種信息的空間.我一直在使用 cv2.Canny()
和 cv2.HoughlinesP()
函數進行邊緣檢測.
I have .pdf
files that have been converted to .jpg
images for this project. My goal is to identify the blanks (e.g ____________) that you would generally find in a .pdf
form that indicate a space for the user to sign of fill out some kind of information. I have been using edge detection with the cv2.Canny()
and cv2.HoughlinesP()
functions.
這工作得相當好,但有不少誤報似乎不知從何而來.當我查看邊緣"文件時,它會在其他單詞周圍顯示一堆噪音.我不確定這種噪音是從哪里來的.
This works fairly well, but there are quite a few false positives that come about from seemingly nowhere. When I look at the 'edges' file it shows a bunch of noise around the other words. I'm uncertain where this noise comes from.
是否應該繼續調整參數,還是有更好的方法來找到這些空白的位置?
Should I continue to tweak the parameters, or is there a better method to find the location of these blanks?
推薦答案
假設您要在 .pdf
表單上查找水平線,這里有一個簡單的方法:
Assuming that you're trying to find horizontal lines on a .pdf
form, here's a simple approach:
- 將圖像轉換為灰度和自適應閾值圖像
- 構造特殊內核以僅檢測水平線
- 執行形態轉換
- 查找輪廓并在圖像上繪制
使用此示例圖片
轉換為灰度和自適應閾值得到二值圖像
Convert to grayscale and adaptive threshold to obtain a binary image
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
然后我們用 cv2.getStructuringElement()
創建一個內核,并進行形態變換以隔離水平線
Then we create a kernel with cv2.getStructuringElement()
and perform morphological transformations to isolate horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15,1))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
從這里我們可以使用 cv2.HoughLinesP()
來檢測線條,但是由于我們已經對圖像進行了預處理并隔離了水平線,所以我們可以找到輪廓并繪制結果
From here we can use cv2.HoughLinesP()
to detect lines but since we have already preprocessed the image and isolated the horizontal lines, we can just find contours and draw the result
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(image, [c], -1, (36,255,12), 3)
完整代碼
import cv2
image = cv2.imread('2.png')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15,1))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(image, [c], -1, (36,255,12), 3)
cv2.imshow('thresh', thresh)
cv2.imshow('detected_lines', detected_lines)
cv2.imshow('image', image)
cv2.waitKey()
這篇關于使用 OpenCV 檢測 .pdf 表單圖像中的水平空白行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!