求助!人体检测出现错误,编译无错当运行到第二帧就中断,黑屏显示参数不匹配啥的

tianzhiya121 北京印刷学院 信息工程学院  2017-12-10 04:37:35
#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=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); //打印文本到图像
}
...全文
117 回复 点赞 打赏 收藏 举报
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
相关推荐
发帖
C++ Builder
加入

1.3w+

社区成员

C++ Builder相关内容讨论区
申请成为版主
帖子事件
创建了帖子
2017-12-10 04:37
社区公告
暂无公告