近期在使用opencv_python分析視頻過程中總是遇到各種問題,如cv2.cv2的問題、還有使用opencv_python操作視頻處理類的需求時總是遇到has no attribute 'bgsegm'等問題。從網上找了但缺少完整的說明,故特地說明下。
遇到has no attribute 'bgsegm'等問題,說明安裝的opencv_python版本需要更新了,或者沒有安裝contrib包。
詳細如下:
在opencv3.0以後的版本中,只有createBackgroundSubtractorKNN和createBackgroundSubtractorMOG2函數,而createBackgroundSubtractorGMG與createBackgroundSubtractorMOG被移動到opencv_contrib包中了。
故使用上要如下方式:
cv2.createBackgroundSubtractorKNN([,history[, dist2Threshold[, detectShadows]]])
cv2.createBackgroundSubtractorMOG2([,history[, varThreshold[, detectShadows]]])
要調用createBackgroundSubtractorGMG與createBackgroundSubtractorMOG則採用如下:
cv2.bgsegm.createBackgroundSubtractorGMG([,initializationFrames[, decisionThreshold]])
cv2.bgsegm.createBackgroundSubtractorMOG([,history[, nmixtures[, backgroundRatio[,noiseSigma]]]])
當出現上述問題時,則說明你的python中沒有安裝contrib包。
安裝contrib包時有如下3種方式:
1、可從https://pypi.python.org/pypi/opencv-contrib-python去下載並安裝。
2、到https://www.lfd.uci.edu/~gohlke/pythonlibs/去下載opencv_python‑3.3.1+contrib‑cp36‑cp36m‑win_amd64.whl。
3、contrib包的源代碼地址:https://github.com/opencv/opencv_contrib也可自行編譯。