我打算從
并像這樣拆分成單獨的組件?


uj5u.com熱心網友回復:
我們可以使用(大部分)形態學運算來解決它:
- 將輸入影像讀取為灰度。
- 通過反轉應用閾值。
使用自動閾值cv2.THRESH_OTSU處理效果很好。 - 應用開放形態學操作來去除小偽影(使用內核
np.ones(1, 3)) - 使用非常長的水平內核水平擴展 - 從文本行中制作水平線。
- 垂直應用關閉 - 創建兩個大集群。
垂直內核的大小應根據典型間隙進行調整。 - 使用統計資訊查找連接的組件。
- 迭代連接的組件并在垂直方向裁剪相關區域。
完整的代碼示例:
import cv2
import numpy as np
img = cv2.imread('scanned_image.png', cv2.IMREAD_GRAYSCALE) # Read image as grayscale
thesh = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU cv2.THRESH_BINARY_INV)[1] # Apply automatic thresholding with inversion.
thesh = cv2.morphologyEx(thesh, cv2.MORPH_OPEN, np.ones((1, 3), np.uint8)) # Apply opening morphological operation for removing small artifacts.
thesh = cv2.dilate(thesh, np.ones((1, img.shape[1]), np.uint8)) # Dilate horizontally - make horizontally lines out of the text.
thesh = cv2.morphologyEx(thesh, cv2.MORPH_CLOSE, np.ones((50, 1), np.uint8)) # Apply closing vertically - create two large clusters
nlabel, labels, stats, centroids = cv2.connectedComponentsWithStats(thesh, 4) # Finding connected components with statistics
parts_list = []
# Iterate connected components:
for i in range(1, nlabel):
top = int(stats[i, cv2.CC_STAT_TOP]) # Get most top y coordinate of the connected component
height = int(stats[i, cv2.CC_STAT_HEIGHT]) # Get the height of the connected component
roi = img[top-5:top height 5, :] # Crop the relevant part of the image (add 5 extra rows from top and bottom).
parts_list.append(roi.copy()) # Add the cropped area to a list
cv2.imwrite(f'part{i}.png', roi) # Save the image part for testing
cv2.imshow(f'part{i}', roi) # Show part for testing
# Show image and thesh testing
cv2.imshow('img', img)
cv2.imshow('thesh', thesh)
cv2.waitKey()
cv2.destroyAllWindows()
結果:
階段1:

這是有趣的部分發生的地方。我們假設相鄰的文本/字符是同一個問題的一部分,因此我們將單個單詞合并到一個輪廓中。問題是靠近在一起的單詞的一部分,因此我們將它們擴展以將它們連接在一起。

以綠色突出顯示的個別問題

熱門問題

底部問題

已保存的 ROI 問題(假設從上到下)

代碼
import cv2
from imutils import contours
# Load image, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread('1.png')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7,7), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV cv2.THRESH_OTSU)[1]
# Remove small artifacts and noise with morph open
open_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, open_kernel, iterations=1)
# Create rectangular structuring element and dilate
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
dilate = cv2.dilate(opening, kernel, iterations=4)
# Find contours, sort from top to bottom, and extract each question
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
(cnts, _) = contours.sort_contours(cnts, method="top-to-bottom")
# Get bounding box of each question, crop ROI, and save
question_number = 0
for c in cnts:
# Filter by area to ensure its not noise
area = cv2.contourArea(c)
if area > 150:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image, (x, y), (x w, y h), (36,255,12), 2)
question = original[y:y h, x:x w]
cv2.imwrite('question_{}.png'.format(question_number), question)
question_number = 1
cv2.imshow('thresh', thresh)
cv2.imshow('dilate', dilate)
cv2.imshow('image', image)
cv2.waitKey()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/462341.html
標籤:Python 图片 opencv 图像处理 计算机视觉
下一篇:在棋盤OpenCV中檢測方塊
