我有一個網路攝像頭,可以拍攝混凝土板的照片。現在我想檢查平板上是否有物體。物件可以是任何東西,因此不能在類中列舉。不幸的是,我無法直接將網路攝像頭影像與混凝土板上沒有物體的影像進行比較,因為攝像頭的影像在 x 和 y 方向上的移動可能很小,而且照明也不總是相同的。所以我不能使用cv2.substract. 我更喜歡前景和背景的減法,其中背景只是我的混凝土板,前景是物件。但由于物體不動,而是靜止在平板上,我也不能使用cv2.createBackgroundSubtractorMOG2()。
圖片如下所示:
沒有任何物體的混凝土拍打:

與物件的耳光:

uj5u.com熱心網友回復:
在 Python/OpenCV 中,您可以進行除法歸一化以均勻照明并使背景變白。然后做你的減法。然后使用形態學清理小區域。然后找到輪廓并丟棄由于劃分歸一化和形態學后留下的噪聲而產生的任何小區域。
以下是如何進行除法歸一化。
輸入 1:

輸入 2:

import cv2
import numpy as np
# load image
img1 = cv2.imread("img1.jpg")
img2 = cv2.imread("img2.jpg")
# convert to grayscale
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# blur
blur1 = cv2.GaussianBlur(gray1, (0,0), sigmaX=13, sigmaY=13)
blur2 = cv2.GaussianBlur(gray2, (0,0), sigmaX=13, sigmaY=13)
# divide
divide1 = cv2.divide(gray1, blur1, scale=255)
divide2 = cv2.divide(gray2, blur2, scale=255)
# threshold
thresh1 = cv2.threshold(divide1, 200, 255, cv2.THRESH_BINARY)[1]
thresh2 = cv2.threshold(divide2, 200, 255, cv2.THRESH_BINARY)[1]
# morphology
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
morph1 = cv2.morphologyEx(thresh1, cv2.MORPH_OPEN, kernel)
morph2 = cv2.morphologyEx(thresh2, cv2.MORPH_OPEN, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
morph1 = cv2.morphologyEx(morph1, cv2.MORPH_CLOSE, kernel)
morph2 = cv2.morphologyEx(morph2, cv2.MORPH_CLOSE, kernel)
# write result to disk
cv2.imwrite("img1_division_normalize.jpg", divide1)
cv2.imwrite("img2_division_normalize.jpg", divide2)
cv2.imwrite("img1_division_morph1.jpg", morph1)
cv2.imwrite("img1_division_morph2.jpg", morph2)
# display it
cv2.imshow("img1_norm", divide1)
cv2.imshow("img2_norm", divide2)
cv2.imshow("img1_thresh", thresh1)
cv2.imshow("img2_thresh", thresh2)
cv2.imshow("img1_morph", morph1)
cv2.imshow("img2_morph", morph2)
cv2.waitKey(0)
cv2.destroyAllWindows()
Image 1 Normalized:

Image 2 Normalized:

Image 1 thresholded and morphology cleaned:

Image 2 thresholded and morphology cleaned:

In this case, Image 1 becomes completely white. So it (and subtraction) is not really needed. You just need to find contours for the second image result and if necessary discard tiny regions by area. The rest are your objects.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/434243.html
