我对opencv真的很陌生。如何在不丢失信息的情况下消除背景噪音?
我是从这个开始的:Otsu 对它进行了阈值化。我试过腐 eclipse 、膨胀、双边过滤。我的目标是在边界上得到一个矩形,这样我就可以透视变换阈值图片,但它很难找到轮廓。或者也许有不同的更好的方法?
最佳答案
这是在 Python/OpenCV 中执行此操作的一种方法。
输入:
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 阈值图像:
大通结果:
图像结果:
关于python - 如何清理这张图片(opencv-python)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61130257/