簡體   English   中英

如何清理這張圖片(opencv-python)?

[英]How can I clean this picture up (opencv-python)?

我對 opencv 真的很陌生。 如何在不丟失信息的情況下消除背景噪音?

我是從這個開始的:Otsu 對它進行了閾值化。 我試過腐蝕、膨脹、雙邊過濾。 我的目標是在邊界上獲得一個矩形,這樣我就可以透視變換閾值圖片,但它很難找到輪廓。 或者也許有不同的更好的方法?

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

  • 讀取輸入
  • 模糊它
  • 轉換為 HSV 並提取飽和通道
  • 閾值飽和圖像
  • 用形態關閉和打開將其清理並保存為蒙版
  • 重新創建您的 OTSU 閾值圖像
  • 將黑色寫入掩碼為黑色(零)的 OTSU 圖像
  • 為了比較,將黑色寫入掩碼為黑色(零)的輸入圖像
  • 保存結果

輸入:

在此處輸入圖像描述

import cv2
import numpy as np

# read image
img = cv2.imread('circuit_board.jpg')

# blur
blur = cv2.GaussianBlur(img, (3,3), 0)

# convert to hsv and get saturation channel
sat = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)[:,:,1]

# threshold saturation channel
thresh = cv2.threshold(sat, 50, 255, cv2.THRESH_BINARY)[1]

# apply morphology close and open to make mask
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=1)
mask = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel, iterations=1)

# do OTSU threshold to get circuit image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
otsu = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# write black to otsu image where mask is black
otsu_result = otsu.copy()
otsu_result[mask==0] = 0

# write black to input image where mask is black
img_result = img.copy()
img_result[mask==0] = 0

# write result to disk
cv2.imwrite("circuit_board_mask.png", mask)
cv2.imwrite("circuit_board_otsu.png", otsu)
cv2.imwrite("circuit_board_otsu_result.png", otsu_result)
cv2.imwrite("circuit_board_img_result.png", img_result)


# display it
cv2.imshow("IMAGE", img)
cv2.imshow("SAT", sat)
cv2.imshow("MASK", mask)
cv2.imshow("OTSU", otsu)
cv2.imshow("OTSU_RESULT", otsu_result)
cv2.imshow("IMAGE_RESULT", img_result)
cv2.waitKey(0)


蒙版圖片:

在此處輸入圖像描述

OTSU 閾值圖像:

在此處輸入圖像描述

大通結果:

在此處輸入圖像描述

圖像結果:

在此處輸入圖像描述

暫無
暫無

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

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