![](/img/trans.png)
[英]How to remove background noise while performing OpenCV Image subtraction?
[英]How to apply opencv background subtraction to an image
我正在嘗試將cv2.createBackgroundSubtractorMOG()
應用於此圖像:
消除所有背景亮度,只留下中間的兩個明亮物體以供進一步分析。 這是完成這項任務的正確方法嗎? 如果沒有,我該怎么做?
import cv2
img = cv2.imread('image.png')
sharp_img = cv2.createBackgroundSubtractorMOG().apply(img)
Output:
Traceback (most recent call last):
File "/home/artur/Desktop/test.py", line 4, in <module>
sharp_img = cv2.createBackgroundSubtractorMOG().apply(img)
AttributeError: module 'cv2.cv2' has no attribute 'createBackgroundSubtractorMOG
編輯:
MOG 似乎不起作用。
代碼:
import cv2
img = cv2.imread('image.png')
sharp_img = cv2.bgsegm.createBackgroundSubtractorMOG().apply(img)
cv2.imwrite('image2.png', sharp_img)
Output:
Traceback (most recent call last):
File "/home/artur/Desktop/test.py", line 4, in <module>
sharp_img = cv2.bgsegm.createBackgroundSubtractorMOG().apply(img)
AttributeError: module 'cv2.cv2' has no attribute 'bgsegm'
MOG2 似乎有效但沒有令人滿意的結果:
代碼:
import cv2
img = cv2.imread('image.png')
sharp_img = cv2.createBackgroundSubtractorMOG2().apply(img)
cv2.imwrite('image2.png', sharp_img)
Output 圖片:
我嘗試使用文檔中的 MOG2 方法的參數,但沒有任何改變。
從文檔中嘗試以下操作:
sharp_img = cv.bgsegm.createBackgroundSubtractorMOG().apply(img)
要么
sharp_img = cv2.createBackgroundSubtractorMOG2().apply(img)
import cv2
img = cv2.imread('image.png')
max,min = img.max(),imgg.min()
print(max,min) #helps in giving thresholding values
threshold_img = cv2.threshold(blurred, 127, 255,cv2.THRESH_BINARY) #good starting point to give t1 value as half of max value of image
cv2.imshow(threshold_img)
我個人認為這種方法是你的一個很好的起點,因為你有兩個明亮的峰,你想從噪音中分離出來。 一旦確定了所需的閾值限制。 您應該能夠將這兩個點與背景噪音隔離開來。 如果需要進一步消除噪音,您可以進一步使用 cv2.erode 和 cv2.dilate。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.