日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基于OpenCV高斯模型的公共场景人流量统计

發(fā)布時間:2025/5/22 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于OpenCV高斯模型的公共场景人流量统计 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

關(guān)注我,并留言,不定期送各種c++資料
代碼全部在下面,就一個文件,隨便添加一個空項目后就可以將下面代碼替換到工程中。具體操作步驟沒時間過多的寫,可以看注釋。代碼運行沒有問題,我用的是opencv344和vs2017。需要源代碼和資料的請在這里下載,在我的工程里面環(huán)境已經(jīng)配置好https://download.csdn.net/download/qq_41476542/14046884
代碼如下:

#include<iostream> #include<opencv2/opencv.hpp> #include <sstream> using namespace std;//命名空間std using namespace cv;//命名空間cvv b string int2str(const int &int_temp)//數(shù)據(jù)流之間的轉(zhuǎn)換 {stringstream stream;//通過流來實現(xiàn)字符串和數(shù)字的轉(zhuǎn)換string string_temp;//有不限長度的優(yōu)點stream << int_temp; //將int輸入流,向左移4位string_temp = stream.str(); //此處也可以用 stream>>string_temp//從stream中抽取前面插入的int值return string_temp; } int main(int argc, const char** argv) {VideoCapture cap;//打開攝像頭對象bool update_bg_model = true;cap.open(0);// cap.open("http://113.57.246.23:8080/shot.jpg");cap.open("C:\\Users\\Administrator\\Desktop\\acvis09-5950.avi");//cap.open("D:\\qq\\demo\\vtest.avi"); if (!cap.isOpened()){printf("can not open camera or video file\n");return -1;}namedWindow("image", WINDOW_AUTOSIZE);namedWindow("foreground mask", WINDOW_AUTOSIZE);//保持原圖大小namedWindow("foreground image", WINDOW_AUTOSIZE);//namedWindow("mean background image", WINDOW_AUTOSIZE);//原圖大小Ptr<BackgroundSubtractorMOG2> bg_model = createBackgroundSubtractorMOG2();//(100, 3, 0.3, 5);建立背景模型Mat img, fgmask, fgimg;//定義矩陣int i = 0;for (;;)//死循環(huán){i++;cap >> img;//由capture類輸出圖像if (img.empty())break;img = img(Rect(40, 0, 300, img.rows));if (fgimg.empty())fgimg.create(img.size(), img.type());//更新模型bg_model->apply(img, fgmask, update_bg_model ? -1 : 0);//背景模型的跟新medianBlur(fgmask, fgmask, 13);//有效抑制噪聲的中值濾波函數(shù),基本思想是用像素點鄰域灰度值的中值來代替該像素點的灰度值。輸入圖像輸出圖像尺寸大小,必須是大于1的奇數(shù),如3、5、7……threshold(fgmask, fgmask, 150, 255, THRESH_BINARY);//# 閾值化,將非純白色(244~255)的所有像素設(shè)為0Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));// # 為了使效果更好,進行一次膨脹/*erode(fgmask, fgmask, element, Point(0, 0), 3);dilate(fgmask, fgmask, element, Point(0, 0), 3);*/Mat srcGrayImage = fgmask.clone();vector<vector<Point>> vContours;vector<Vec4i> vHierarchy;// # 檢測輪廓findContours(srcGrayImage, vContours, vHierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE, Point(0, 0));int count = 0;RNG rng(12345);//構(gòu)造方法設(shè)定一個具體值,表示下面代碼每次生成的結(jié)果都是一樣的 for (int i = 0; i < vContours.size(); i++){ double area = contourArea(vContours[i], false); //RotatedRect該類表示平面上的旋轉(zhuǎn)矩形,該類對象有三個重要屬性://矩形中心點(質(zhì)心),邊長(長和寬),旋轉(zhuǎn)角度。三種構(gòu)造函數(shù)和三種成員操作函數(shù)RotatedRect smallRect = minAreaRect(vContours[i]);/* RotatedRect該類表示平面上的旋轉(zhuǎn)矩形,有三個屬性:矩形中心點(質(zhì)心)邊長(長和寬)旋轉(zhuǎn)角度 */ Point2f smallRect_center = smallRect.center;float smallRect_width = smallRect.size.width;float smallRect_height = smallRect.size.height;float smallRect_angle = 0;smallRect = RotatedRect(smallRect_center, Size2f(smallRect_height, smallRect_width), smallRect_angle);//若使用Point2f,這里就使用flow1_at_point.x形式存取像素值Point2f P[4];//points()函數(shù)用于求矩形的4個頂點,smallRect.points(P);if (area>600 && area < 3000){count++;for (int j = 0; j <= 3; j++){//劃線,繪制在img上line(img, P[j], P[(j + 1) % 4], Scalar(255, 0, 0), 2);}}if (area>3000&& area < 5000){count += 2;for (int j = 0; j <= 3; j++){line(img, P[j], P[(j + 1) % 4], Scalar(255, 0, 0), 2);}}}//任意值//數(shù)數(shù)的顏色Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));//寫字參數(shù)1:, Mat& img,待寫字的圖片,我們寫在img圖上/*參數(shù)2:,const string& text,待寫入的字,我們下面寫入Hello參數(shù)3:, Point org, 第一個字符左下角坐標,我們設(shè)定在圖片的Point(50, 60)坐標。表示x = 50, y = 60。參數(shù)4:,int fontFace,字體類型,FONT_HERSHEY_SIMPLEX ,FONT_HERSHEY_PLAIN ,FONT_HERSHEY_DUPLEX 等等等。參數(shù)5:,double fontScale,字體大小,我們設(shè)置為2號參數(shù)6:,Scalar color,字體顏色,顏色用Scalar()表示,不懂得去百度。參數(shù)7:, int thickness,字體粗細,我們下面代碼使用的是4號參數(shù)8:, int lineType,線型,我們使用默認值8.*/putText(img, int2str(count), Point(220, 40), FONT_HERSHEY_TRIPLEX, 1, color, 2);//設(shè)定所有的值為0.得到前景fgimg = Scalar::all(0);img.copyTo(fgimg, fgmask);Mat bgimg;得到背景bg_model->getBackgroundImage(bgimg);//?imshow("image", img);/*string windows_name = "Video/image_" + int2str(i);string windows_name_ext = windows_name + ".jpg";imwrite(windows_name_ext, img);*/imshow("foreground mask", fgmask);imshow("foreground image", fgimg);//是否圖像讀入if (!bgimg.empty())imshow("mean background image", bgimg);char k = (char)waitKey(1);if (k == 27) break;if (k == ' '){update_bg_model = !update_bg_model;if (update_bg_model)printf("\t>背景更新(Background update)已打開\n");else printf("\t>背景更新(Background update)已關(guān)閉\n");}}return 0; }

總結(jié)

以上是生活随笔為你收集整理的基于OpenCV高斯模型的公共场景人流量统计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。