簡體   English   中英

python - 根據空白分割圖像

[英]python - Split an image based on white space

我試圖將這個圖像分成相等的部分,其中氣泡行之間的“空白”是,然后取出所有這些子圖像並將它們排列成一個長圖像,以便問題是垂直排序的。 如何使用 python 以這種方式分割圖像? 我這樣做是為了使用 OpenCV 對氣泡表進行分級。 請記住,我是 python 新手(但不是編碼),所以如果你能解釋每個代碼塊的用途會很好。

這是在 Python/OpenCV 中執行此操作的一種方法。

  • 讀取輸入
  • 邊框顏色上的顏色閾值作為遮罩
  • 使用蒙版使邊框變白
  • 稍微擴大蒙版以包括黑色邊框線
  • 將該結果轉換為灰度
  • 大津門檻
  • 應用形態學打開以將文本連接到列狀區域並反轉
  • 獲取列表中的外部輪廓及其邊界框
  • 計算所有框的最大寬度
  • 循環遍歷每個框,使用填充裁剪並與前一個框堆疊
  • 保存結果

輸入:

在此處輸入圖像描述

import cv2
import numpy as np

# read input image
img = cv2.imread('abcd_test.png')

# define border color
lower = (0, 80, 110)
upper = (0, 120, 150)

# threshold on border color
mask = cv2.inRange(img, lower, upper)

# dilate threshold
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 15))
mask = cv2.morphologyEx(mask, cv2.MORPH_DILATE, kernel)

# recolor border to white
img[mask==255] = (255,255,255)

# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# otsu threshold
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU )[1] 

# apply morphology open
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (17,17))
morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
morph = 255 - morph

# find contours and bounding boxes
bboxes = []
bboxes_img = img.copy()
contours = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
for cntr in contours:
    x,y,w,h = cv2.boundingRect(cntr)
    cv2.rectangle(bboxes_img, (x, y), (x+w, y+h), (0, 0, 255), 1)
    bboxes.append((x,y,w,h))

# get largest width of bboxes
maxwidth = max(bboxes)[2]

# sort bboxes on x coordinate
def takeFirst(elem):
    return elem[0]

bboxes.sort(key=takeFirst)

# stack cropped boxes with 10 pixels padding all around
result = np.full((1,maxwidth+20,3), (255,255,255), dtype=np.uint8)
for bbox in bboxes:
    (x,y,w,h) = bbox
    crop = img[y-10:y+h+10, x-10:x+maxwidth+10]
    result = np.vstack((result, crop))

# save result
cv2.imwrite("abcd_test_mask.jpg", mask)
cv2.imwrite("abcd_test_white_border.jpg", img)
cv2.imwrite("abcd_test_thresh.jpg", thresh)
cv2.imwrite("abcd_test_morph.jpg", morph)
cv2.imwrite("abcd_test_bboxes.jpg", bboxes_img)
cv2.imwrite("abcd_test_column_stack.png", result)

# show images
cv2.imshow("mask", mask)
cv2.imshow("img", img)
cv2.imshow("thresh", thresh)
cv2.imshow("morph", morph)
cv2.imshow("bboxes_img", bboxes_img)
cv2.imshow("result", result)
cv2.waitKey(0)

邊框蒙版圖像:

在此處輸入圖像描述

帶邊框的圖像變為白色:

在此處輸入圖像描述

閾值和形態圖像:

在此處輸入圖像描述

邊界框圖像:

在此處輸入圖像描述

裁剪和堆疊的列圖像:

在此處輸入圖像描述

暫無
暫無

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

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