您好,登錄后才能下訂單哦!
使用opencv怎么解析三幀差法?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
#include<iostream> #include<opencv2\core\core.hpp> #include<opencv2\highgui\highgui.hpp> #include<opencv2\imgproc\imgproc.hpp> using namespace cv; using namespace std; const unsigned char FORE_GROUD = 255;int thresh = 10; int main(int argc,char*argv[]) { VideoCapture video(argv[1]); //判斷如果video是否可以打開 if(!video.isOpened()) return -1; //用于保存當前幀的圖片 Mat currentBGRFrame; //用來保存上一幀和當前幀的灰度圖片 Mat previousSecondGrayFrame; Mat previousFirstGrayFrame; Mat currentGaryFrame; //保存兩次的幀差 Mat previousFrameDifference; //previousFrameFirst - previousFrameSecond的差分 Mat currentFrameDifference; //currentFrame - previousFrameFirst; //用來保存幀差的絕對值 Mat absFrameDifferece; //用來顯示前景 Mat previousSegmentation; Mat currentSegmentation; Mat segmentation; //顯示前景 namedWindow("segmentation",1); createTrackbar("閾值:","segmentation",&thresh,FORE_GROUD,NULL); //幀數 int numberFrame = 0; //形態學處理用到的算子 Mat morphologyKernel = getStructuringElement(MORPH_RECT,Size(3,3),Point(-1,-1)); for(;;) { //讀取當前幀 video >> currentBGRFrame; //判斷當前幀是否存在 if(!currentBGRFrame.data) break; numberFrame++; //顏色空間的轉換 cvtColor(currentBGRFrame,currentGaryFrame,COLOR_BGR2GRAY); if( numberFrame == 1) { //保存當前幀的灰度圖 previousSecondGrayFrame = currentGaryFrame.clone(); //顯示視頻 imshow("video",currentBGRFrame); continue; } else if( numberFrame == 2) { //保存當前幀的灰度圖 previousFirstGrayFrame = currentGaryFrame.clone(); //previousFirst - previousSecond subtract(previousFirstGrayFrame,previousSecondGrayFrame,previousFrameDifference,Mat(),CV_16SC1); //取絕對值 absFrameDifferece = abs(previousFrameDifference); //位深的改變 absFrameDifferece.convertTo(absFrameDifferece,CV_8UC1,1,0); //閾值處理 threshold(absFrameDifferece,previousSegmentation,double(thresh),double(FORE_GROUD),THRESH_BINARY); //顯示視頻 imshow("video",currentBGRFrame); continue; } else { //src1-src2 subtract(currentGaryFrame,previousFirstGrayFrame,currentFrameDifference,Mat(),CV_16SC1); //取絕對值 absFrameDifferece = abs(currentFrameDifference); //位深的改變 absFrameDifferece.convertTo(absFrameDifferece,CV_8UC1,1,0); //閾值處理 threshold(absFrameDifferece,currentSegmentation,double(thresh),double(FORE_GROUD),THRESH_BINARY); //與運算 bitwise_and(previousSegmentation,currentSegmentation,segmentation); //中值濾波 medianBlur(segmentation,segmentation,3); //形態學處理(開閉運算) //morphologyEx(segmentation,segmentation,MORPH_OPEN,morphologyKernel,Point(-1,-1),1,BORDER_REPLICATE); morphologyEx(segmentation,segmentation,MORPH_CLOSE,morphologyKernel,Point(-1,-1),2,BORDER_REPLICATE); //找邊界 vector< vector<oint> > contours; vector<Vec4i> hierarchy; //復制segmentation Mat tempSegmentation = segmentation.clone(); findContours( segmentation, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );//CV_RETR_TREE vector< vector<oint> > contours_poly( contours.size() ); /*存儲運動物體*/ vector<Rect> boundRect; boundRect.clear(); //畫出運動物體 for(int index = 0;index < contours.size() ;index++) { approxPolyDP( Mat(contours[index]), contours_poly[index], 3, true ); Rect rect = boundingRect( Mat(contours_poly[index]) ); rectangle(currentBGRFrame,rect,Scalar(0,255,255),2); } //顯示視頻 imshow("video",currentBGRFrame); //前景檢測 imshow("segmentation",segmentation); //保存當前幀的灰度圖 previousFirstGrayFrame = currentGaryFrame.clone(); //保存當前的前景檢測 previousSegmentation = currentSegmentation.clone(); } if(waitKey(33) == 'q') break; } return 0; }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。