我想要的是 :
當用戶單擊一個按鈕時,程式會在剪貼板中截取螢屏截圖“這部分沒有問題”,檢測感興趣區域 (ZOI),剪切該區域并為我的程式提取多個區域的文本。我試圖從處方中提取資訊。我知道感興趣的區域將始終處于相同的顏色矩形中,但我不確定每次需要提取資訊時處方是否為全尺寸。你可以看到原始影像和我想要的 ZOI 是紅色的。

我嘗試了什么:
- 我第一次嘗試使用字體中的顏色制作區域的輪廓。該程式可以在灰色特定區域周圍創建一個區域,但這不是我想要的(綠色框)。上面可以看到原圖、后處理圖和代碼

import numpy as np
import cv2
# Read input image
img = cv2.imread('test_image.png')
gray = np.all(img == (227,227,227), 2)
# Convert logical matrix to uint8
gray = gray.astype(np.uint8)*255
# Find contours
cnts = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2] # Use index [-2] to be compatible to OpenCV 3 and 4
# Get contour with maximum area
c = max(cnts, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(c)
# Draw green rectangle for testing
cv2.rectangle(img, (x, y), (x w, y h), (0, 255, 0), thickness = 2)
# Show result
cv2.imshow('gray', gray)
cv2.imwrite('niveau_gris.jpg', gray)
cv2.imshow('img', img)
cv2.imwrite('test_image_resultat.jpg', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
- 第二次我嘗試使用兩個引數來制作區域的輪廓,目的是選擇帶有文本的區域。該程式根本不創建區域。你可以看到下面的代碼
import numpy as np
import cv2
frame = cv2.imread('test_image_constrasate.jpg')
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of red color in HSV
lower_red = np.array([189,189,189])
upper_red = np.array([204,203,204])
mask = cv2.inRange (hsv, lower_red, upper_red)
contours = cv2.findContours(mask.copy(),
cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)[-2]
if len(contours) > 0:
red_area = max(contours, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(red_area)
cv2.rectangle(frame,(x, y),(x w, y h),(0, 0, 255), 2)
cv2.imshow('frame', frame)
cv2.imshow('mask', mask)
cv2.waitKey(0)
- 第三次我嘗試使用霍夫線變換,但我沒有足夠的實驗來處理影像我不確定這個程序的可重復性,因為截圖可能不是全尺寸的,我不確定如何使用這條線來制作盒子。
提供研究
我在 StackoverFlow 上使用了以下術語:“OpenCv 選擇矩形”、“OpenCV 選擇基于顏色的區域”、“OpenCV 如何根據他的顏色選擇區域”......
你能幫助我嗎 ?感謝您未來的幫助
uj5u.com熱心網友回復:
標題的方法:
- 找到位于其左側和右側的按鈕,使用
matchTemplate - 標題是相對于那些的矩形
表的方法:
inRange關于表頭的顏色connectedComponentsWithStats- 按高度過濾以僅查找表格標題單元格
- 找到最寬的單元格
- 使用條紋背景分隔行
整件事:
inRange:
# table header, first cell is largest
header_color = (194, 142, 93)
mask = cv.inRange(im, header_color, header_color)
連接組件:
(nlabels, labels, stats, centroids) = cv.connectedComponentsWithStats(mask)
# print(stats) # x, y, w, h, area (ConnectedComponentsTypes)
過濾和排序組件:
comps = [(label, *stat) for label, stat in enumerate(stats)]
# (label, x, y, w, h, area)
comps = [comp for comp in comps if comp[4] == 25] # height: exactly 25 pixels
comps.sort(key=lambda comp: comp[5], reverse=True) # area, descending... or simply max(key=)
header_comp = comps[0] # largest area
header_rect = header_comp[1:5]
(hx,hy,hw,hh) = header_rect
header = crop(im, *header_rect)
imshow(header)

查找表體和行:
# table body
# pixel column just before the header cell (B) contains striped background but no text
# column to the left of that (A) contains only white, until the end
bx = hx
by = hy hh 1
bw = hw
columnA = crop(im, bx-2, by, 1, imh-by)[:,0,1]
(I,) = np.where(columnA != 255)
bh = I.min() # table body height
columnB = crop(im, bx-1, by, 1, bh)[:,0,1]
rowmask = (columnB == 255)#.astype(np.int8)
(I,) = np.where(np.diff(rowmask))
I = 1 # diff shifts things back, edge is on the second pixel, not the first
row_tops = np.concatenate(([0], I[:-1]))
row_bottoms = I
# np.vstack([row_tops, row_bottoms]).T
提取每一行:
print((bx, by, bw, bh))
for i,(top,bottom) in enumerate(zip(row_tops, row_bottoms)):
print(f"row {i 1}:")
imshow(crop(im, bx, by top, bw, bottom-top))
print()

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/480650.html
上一篇:在3D中銳化內核過濾器
