簡體   English   中英

如何提取圖像中的白色區域

[英]How to extract white region in an image

我有這樣的示例圖像

在此處輸入圖片說明

我正在尋找一種方法來消除圖像中的噪點,以便最終得到在白色背景上只有黑色文字的圖像,以便將其發送到tesseract。

我試過用

kernel = np.ones((4,4),np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
cv2.imshow("opening", opening)

但它似乎不起作用。

我也嘗試尋找輪廓

img = cv2.cvtColor(rotated, cv2.COLOR_BGR2GRAY)
(cnts, _) = cv2.findContours(img, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:1]
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    roi=rotated[y:y+h,x:x+w].copy()
    cv2.imwrite("roi.png", roi)

通過上面的代碼,我得到以下輪廓:

在此處輸入圖片說明

裁剪后會導致此圖像:

在此處輸入圖片說明

這還不夠好。 我想要白色背景上的黑色文本,以便可以將其發送到tesseract OCR,並具有良好的成功率。

還有什么我可以嘗試的嗎?

更新資料

這是另一個類似的圖像。 這個比較容易,因為其中有一個平滑的矩形

在此處輸入圖片說明

以下內容適用於您的給定示例,盡管可能需要進行調整以獲取更大范圍的圖像。

import numpy as np
import cv2

image_src = cv2.imread("input.png")
gray = cv2.cvtColor(image_src, cv2.COLOR_BGR2GRAY)
ret, gray = cv2.threshold(gray, 250,255,0)

image, contours, hierarchy = cv2.findContours(gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
largest_area = sorted(contours, key=cv2.contourArea)[-1]
mask = np.zeros(image_src.shape, np.uint8)
cv2.drawContours(mask, [largest_area], 0, (255,255,255,255), -1)
dst = cv2.bitwise_and(image_src, mask)
mask = 255 - mask
roi = cv2.add(dst, mask)

roi_gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
ret, gray = cv2.threshold(roi_gray, 250,255,0)
image, contours, hierarchy = cv2.findContours(gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

max_x = 0
max_y = 0
min_x = image_src.shape[1]
min_y = image_src.shape[0]

for c in contours:
    if 150 < cv2.contourArea(c) < 100000:
        x, y, w, h = cv2.boundingRect(c)
        min_x = min(x, min_x)
        min_y = min(y, min_y)
        max_x = max(x+w, max_x)
        max_y = max(y+h, max_y)

roi = roi[min_y:max_y, min_x:max_x]
cv2.imwrite("roi.png", roi)

為您提供以下類型的輸出圖像:

在此處輸入圖片說明

和...

在此處輸入圖片說明

該代碼的工作方式是首先找到最大的輪廓區域。 由此創建一個遮罩,該遮罩用於首先僅選擇內部區域,即文本。 然后將遮罩的反面添加到圖像,以將遮罩外部的區域轉換為白色。

最后,再次為該新圖像找到輪廓。 超出合適大小范圍的任何輪廓區域都將被丟棄(用於忽略任何小的噪聲區域),並且為每個輪廓區域找到邊界矩形。 對於這些矩形中的每個矩形,將為所有其余輪廓計算outer邊界rect,並使用這些值進行裁剪以給出最終圖像。

更新 -要獲取圖像的其余部分(即刪除上述區域),可以使用以下內容:

image_src = cv2.imread("input.png")
gray = cv2.cvtColor(image_src, cv2.COLOR_BGR2GRAY)
ret, gray = cv2.threshold(gray, 10, 255,0)
image, contours, hierarchy = cv2.findContours(gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
largest_area = sorted(contours, key=cv2.contourArea)[-1]
mask = np.zeros(image_src.shape, np.uint8)
cv2.drawContours(mask, [largest_area], 0, (255,255,255,255), -1)
image_remainder = cv2.bitwise_and(image_src, 255 - mask)

cv2.imwrite("remainder.png", image_remainder)

這個答案的基本思想是在文本周圍使用邊框。

1)用一個非常大的內核水平 侵蝕 ,比如說100 px的大小或單個預期字符的大小的8倍,諸如此類。 它應該按行進行。 極坐標將給出文本周圍邊界的y位置。

2) 垂直處理以相同方式獲取文本周圍邊界的x位置。 然后使用這些位置裁剪出所需的圖像。

-這種方法的一個好處是您可以將每個句子/單詞分開分割 ,我認為這對OCR很有好處。

快樂編碼:)

Mark Setchell編輯

這是1的演示)

在此處輸入圖片說明

這是2的演示)

在此處輸入圖片說明

我明白了: 結果

源代碼:

if __name__ == '__main__':
  SrcImg = cv2.imread('./Yahi9.png', cv2.CV_LOAD_IMAGE_GRAYSCALE)
  _, BinImg = cv2.threshold(SrcImg, 80, 255, cv2.THRESH_OTSU)

  Contours, Hierarchy = cv2.findContours(image=copy.deepcopy(SrcImg),
                                         mode=cv2.cv.CV_RETR_EXTERNAL,
                                         method=cv2.cv.CV_CHAIN_APPROX_NONE)
  MaxContour, _ = getMaxContour(Contours)
  Canvas = np.ones(SrcImg.shape, np.uint8)
  cv2.drawContours(image=Canvas, contours=[MaxContour], contourIdx=0, color=(255), thickness=-1)
  mask = (Canvas != 255)
  RoiImg = copy.deepcopy(BinImg)
  RoiImg[mask] = 255
  RoiImg = cv2.morphologyEx(src=RoiImg, op=cv2.MORPH_CLOSE, kernel=np.ones((3,3)), iterations=4)
  cv2.imshow('RoiImg', RoiImg)
  cv2.waitKey(0)

功能:

def getMaxContour(contours):
  MaxArea = 0
  Location = 0
  for idx in range(0, len(contours)):
      Area = cv2.contourArea(contours[idx])
      if Area > MaxArea:
          MaxArea = Area
          Location = idx
  MaxContour = np.array(contours[Location])
  return MaxContour, MaxArea

恩,這是python代碼。 僅在白色區域為最大輪廓時有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM