#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
char ctrl=NULL;
void backGroundDiff(Mat srcImg,Mat froundImg,Mat backGroundImg,int nFrmNum,int threshold_method);
void otsu(Mat srcImg, int *thresholdValue);
void printVedioInfo(VideoCapture *pCapture, Mat& srcImg);
void vedioControl(); //未實作
int main()
{
Mat pFrame;//原視頻
Mat pFroundImg;
Mat pBackgroundImg;
Mat pFroundImg_c;
Mat pBackgroundImg_c;
VideoCapture capture;
capture.open("走過1.avi");
int nFrmNum=0;//幀號
namedWindow("video");
namedWindow("background");
namedWindow("OTSU foreground");
namedWindow("改進的OTSU foreground");
moveWindow("video",30,0);
moveWindow("background",360,0);
moveWindow("OTSU FOREGROUND",690,0);
moveWindow("改進的OTSU foreground",690,320);
while(true)
{
cout<<"Current frame"<<nFrmNum<<endl;
capture>>pFrame;
if(!pFrame.empty())//控制視頻播放結束跳出
{
imshow("pFrame",pFrame);
}
else
{
break;
}
if ((ctrl = waitKey(100)) == 's')
{
waitKey();//等待下一個按鍵
}
else if ((char*)ctrl=="q")//waitkey回傳整形,ctrl需要強制轉換,即const char*轉為char*
{
break;
}
nFrmNum++;
if(nFrmNum==1)
{
pBackgroundImg.create(pFrame.rows,pFrame.cols,CV_8U);
pFroundImg.create(pFrame.rows,pFrame.cols,CV_8U);
pBackgroundImg_c.create(pFrame.rows,pFrame.cols,CV_8U);
pFroundImg_c.create(pFrame.rows,pFrame.cols,CV_8U);
}
backGroundDiff(pFrame,pFroundImg,pBackgroundImg,nFrmNum,CV_THRESH_OTSU);//普通OTSU
backGroundDiff(pFrame,pFroundImg,pBackgroundImg,nFrmNum,CV_THRESH_BINARY);//改進OTSU
printVedioInfo(&capture, pFroundImg);
imshow("video",pFrame);
imshow("background",pBackgroundImg);
imshow("OTSU foreground",pFroundImg);
imshow("改進的OTSU foreground",pFroundImg_c);
}
capture.release();
return 0;
}
void backGroundDiff(Mat srcImg,Mat FroundImg,Mat BackGroundImg,int nFrmNum,int threshold_method)
{
Mat SrcImg_gray;//源影像的灰度影像
Mat SrcImg_grayf;//單通道浮點影像用于背景建模
Mat FroundImgf;//前景浮點圖
Mat BackgroundImgf;//浮點影像,背景
Mat FroundImg_temp;//差景
if (nFrmNum == 1)
{
SrcImg_gray.create(srcImg.cols,srcImg.rows,CV_8U);
FroundImg_temp.create(srcImg.cols,srcImg.rows,CV_8U);
BackgroundImgf.create(srcImg.cols,srcImg.rows,CV_32F);
FroundImgf.create(srcImg.cols,srcImg.rows,CV_32F);
SrcImg_grayf.create(srcImg.cols,srcImg.rows,CV_32F);
cvtColor(srcImg,BackGroundImg,CV_RGB2GRAY);
cvtColor(srcImg,FroundImg,CV_RGB2GRAY);
BackGroundImg.convertTo(BackgroundImgf,CV_32F);
FroundImg.convertTo(FroundImgf,CV_32F);
}
else
{
cvtColor(srcImg, SrcImg_gray, CV_RGB2BGR); //SrcImg_gray在上次函式退出的時候被程式堆疊回收
SrcImg_gray.convertTo(SrcImg_grayf,CV_32F);
//當前幀跟背景圖相減
absdiff(SrcImg_gray, BackGroundImg, FroundImgf);
FroundImgf.convertTo(FroundImg_temp,CV_32F);
//二值化前景圖
int threshold_otsu = 0;
otsu(FroundImg_temp, &threshold_otsu);
if (threshold_method == CV_THRESH_OTSU)
{
threshold(FroundImg_temp, FroundImg, 0, 255.0, CV_THRESH_OTSU); //對比自適應閾值化
// cvAdaptiveThreshold(FroundImg_temp, FroundImg, 255.0, 0, 0, 51); //src和dst必須同時是8bit或浮點影像
}
else
{
threshold(FroundImg_temp, FroundImg, threshold_otsu, 255.0, CV_THRESH_BINARY);
}
//cvSegmentFGMask(FroundImg); //對前景做連通域分割
//更新背景
//cvRunningAvg(SrcImg_grayf, BackgroundImgf, 0.003, 0); //必須是浮點影像,因為會有小數出現
BackgroundImgf.convertTo(BackGroundImg,CV_8U);
}
}
void otsu(Mat src, int *thresholdValue)
{
int deltaT = 0; //光照調節引數
uchar grayflag = 1;
Mat gray;
if (src.channels() != 1) //檢查源影像是否為灰度影像
{
gray .create(src.rows,src.cols,CV_8U);
cvtColor(src, gray, CV_RGB2BGR);
grayflag = 0;
}
else gray = src;
uchar* ImgData = gray.ptr<uchar>(0);//獲得影像首地址,<uchar>指定影像資料型別
int thresholdValue_temp = 1;
int ihist[256]; //影像直方圖,256個點
int i, imgsize; //回圈變數,影像尺寸
int n, n1, n2; //n 非零像素個數, n1 前景像素個數, n2 背景像素個數
double m1, m2, sum, csum, fmax, sb;//m1前景灰度均值,m2背景灰度均值,均值為取值*概率
//對直方圖置零 將緩沖區設定為指定的字符即零,大小為256
memset(ihist, 0, sizeof(ihist));
//生成直方圖
imgsize = (gray.cols)*(gray.rows);//影像資料總數,一個像素一個位元組,widthstep為行大小,以位元組為單位,heigh為列大小
for (i = 0; i<imgsize; i++)
{
ihist[((int)(*ImgData)) & 255]++;//灰度統計 '&255'防止指標溢位,即大于255的書會強制置為0-255之間,正常情況下不變 ,uchar[0,255]
ImgData++;//像素遍歷 //*ImgData表示取值,取首個地址的值x轉為int類,判斷是否溢位,ihist【x】值加1,表示灰度級為x的像素有一個,ImgData++指向下一個地址
}//獲取了所有像素灰度級的個數,耗時較長
// set up everything
sum = csum = 0.0;
n = 0;
for (i = 0; i<=255; i++)
{
sum += (double)i*(double)ihist[i]; // x*f(x)質量矩,即灰度級*像素值,0*ihist[0]+1*ihist[1]+....
n += ihist[i]; //f(x)質量 像素總數,n=n+ ihist[i],n=imgsize=86400
}
deltaT = (int)(sum / imgsize); //像素平均灰度,即均值,均值等于像素值*概率
deltaT = deltaT >> 1; //與之矯正,delatT = v*n; v=0.5
if (!n)
{//影像全黑,輸出警告
fprintf(stderr, "NOT NORMAL thresholdValue=https://bbs.csdn.net/topics/160/n");
}
// OTSU演算法
fmax = -1.0;
n1 = 0;
/***計算灰度在0到255之間的閾值**/
for (i = 0; i<=255; i++)//i灰度級別
{
n1 += ihist[i];//n1灰度級別為i的像素個數
if (n1 == 0) { continue; }//跳到下一次回圈,表示ihist[i]為零,沒有灰度級為i的像素
n2 = n - n1;//n是像素總數
if (n2 == 0) { break; }
csum += (double)i *ihist[i];//質量
m1 = csum / n1;//
m2 = (sum - csum) / n2;
sb = (double)n1*(double)n2*(m1 - m2)*(m1 - m2); //計算類間方差, 公式已簡化,可推出
if (sb>fmax)
{
fmax = sb;//經過所有回圈得到最大類間方差
thresholdValue_temp = i; //找到使類間方差最大的灰度值i
}
}
if (thresholdValue_temp < 20)
*thresholdValue = 20; //閾值篩選
else *thresholdValue = thresholdValue_temp;
if (ctrl == 'p') //ctrl = cvWaitKey(100),且是全域變數
{
cout << "OTSU thresholdValue = " << thresholdValue_temp <<
", Returned thresholdValue = " << *thresholdValue << '\n' << endl;
}
}
void printVedioInfo(VideoCapture* pCapture, Mat &srcImg)
{
assert(pCapture != NULL);//如果條件回傳錯誤,,即pCapture為空,終止程式執行
double frames = pCapture->get(CV_CAP_PROP_POS_FRAMES); //視頻當前幀數 double cvGetCaptureProperty( CvCapture* capture, int property_id );
double fps = pCapture->get(CV_CAP_PROP_FPS); //獲得視頻每秒幀數 CV_CAP_PROP_FPS - 幀率
char str[255];
sprintf(str, "%4.2f FPS %4.2f frames", fps, frames); /* 將浮點數轉化為字串 原型
int sprintf(char *buffer, const char *format, [argument] …);
引數串列
buffer:char型指標,指向將要寫入的字串的緩沖區。
format:格式化字串。
[argument]...:可選引數,可以是任何型別的資料。*/
Point location = Point(20, 20); // 建立字串列印的位置
/*
以下是CvPoint的原型:
typedef struct CvPoint
{
int x; /* X坐標, 通常以0為基點
int y; /* y坐標, 通常以0為基點
}
CvPoint;
*/
/*1. inline CvScalar cvScalar(double val0, double val1 = 0, double val2 = 0, double val3 = 0);
//最通用的,可初始化0-4個通道
舉例:
a) 存放單通道影像中像素:cvScalar(255);
b) 存放三通道影像中像素:cvScalar(255, 255, 255);*/
//CvFont font; //建立字體變數
//cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1.0,1.0); //字體設定
putText(srcImg, str, location,FONT_HERSHEY_PLAIN ,1.0,Scalar(255, 255, 255),2,8,0); //列印文本到影像
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/52028.html
標籤:基礎類
上一篇:【STM32】HAL庫開發教程(四)—串口FIFO使用
下一篇:微信小程式 藍牙重連例外 errCode:10004,errMsg:notifyBLECharacteristicValueChange:fail setNot
