Home > Back-end >  For help! Human body detection error, compile error-free when run to the second frame is interrupted
For help! Human body detection error, compile error-free when run to the second frame is interrupted

Time:09-19

#include
#include
#include
#include
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 ();//unrealized
Int main ()
{
Mat pFrame;//the original video
Mat pFroundImg;
Mat pBackgroundImg;
Mat pFroundImg_c;
Mat pBackgroundImg_c;
VideoCapture capture;
The capture. Open (" through 1. Avi ");
Int nFrmNum=0; No.//frame
NamedWindow (" video ");
NamedWindow (" background ");
NamedWindow (" OTSU foreground ");
NamedWindow (" improved OTSU foreground ");
MoveWindow (" video ", 30, 0);
MoveWindow (" background ", 360, 0).
MoveWindow (" OTSU FOREGROUND ", 690, 0).
MoveWindow (" improved OTSU foreground ", 690320);
While (true)
{
Cout<" The Current frame "& lt; Capture> PFrame;
if(! PFrame. Empty ())//control video playback end out
{
Imshow (" pFrame pFrame);
}
The else
{
break;
}

If ((CTRL=waitKey (100))=='s')
{
waitKey();//wait for the next button
}
Else if (CTRL (char *)=="q")//waitkey return to plastic, CTRL to coercion: const char * to a 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);//ordinary OTSU
BackGroundDiff (pFrame pFroundImg, pBackgroundImg nFrmNum, CV_THRESH_BINARY);//improved OTSU
PrintVedioInfo (& amp; The capture, pFroundImg);
Imshow (" video ", pFrame);
Imshow (" background ", pBackgroundImg);
Imshow (" OTSU foreground, "pFroundImg);
Imshow (" improved OTSU foreground, "pFroundImg_c);
}
The capture. Release ();
return 0;

}
Void backGroundDiff (Mat srcImg, Mat FroundImg, Mat BackGroundImg, int nFrmNum, int threshold_method)
{

Mat SrcImg_gray;//the source image grayscale image
Mat SrcImg_grayf;//single channel floating-point image used for background modeling
Mat FroundImgf;//outlook floating-point figure
Mat BackgroundImgf;//floating image, background
Mat FroundImg_temp;//poor scene
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);

}
The else
{
CvtColor (srcImg SrcImg_gray, CV_RGB2BGR);//SrcImg_gray at the last end of the function by the program stack recovery
SrcImg_gray. ConvertTo (SrcImg_grayf CV_32F);
//the current frame and background subtraction
Absdiff (SrcImg_gray BackGroundImg, FroundImgf);
FroundImgf. ConvertTo (FroundImg_temp CV_32F);
//binary picture
Int threshold_otsu=0;
Otsu (FroundImg_temp, & amp; Threshold_otsu);

If (threshold_method==CV_THRESH_OTSU)
{
Threshold (FroundImg_temp FroundImg, 0, 255.0, CV_THRESH_OTSU);//contrast adaptive threshold change
//cvAdaptiveThreshold (FroundImg_temp, FroundImg, 255.0, 0, 0, 51);//SRC and DST must at the same time is 8 bit or floating images
}
The else
{
Threshold (FroundImg_temp FroundImg, threshold_otsu, 255.0, CV_THRESH_BINARY);
}
//cvSegmentFGMask (FroundImg);//on the prospects of connected domain segmentation
//update background
//cvRunningAvg (SrcImg_grayf BackgroundImgf, 0.003, 0);//must be a floating point image, because you will be presented with the decimal
BackgroundImgf. ConvertTo (BackGroundImg CV_8U);
}
}
Void otsu (Mat SRC, int * thresholdValue)
{
Int deltaT=0;//light adjusting parameter
Uchar grayflag=1;
Mat gray;
If (SRC) channels ()!=1)//check if the source image to gray image
{
Gray. The create (SRC) rows, SRC cols, CV_8U);
CvtColor (SRC, gray, CV_RGB2BGR);
Grayflag=0;
}
The else gray=SRC;
Uchar * ImgData=https://bbs.csdn.net/topics/gray.ptr (0);//get the image first address, specifies the image data type
Int thresholdValue_temp=1;
Int ihist [256].//image histogram, 256 points

Int I, imgsize;//loop variable, image size
Int n, n1, and n2;//n non-zero pixel number, pixel number n1 prospects, n2 background pixel number
Double m1, m2, sum, csum fmax, sb;//grayscale average prospect of m1, m2 background grayscale average, with mean value * probability
//the histogram zero set the buffer to the specified character is zero, size of 256
Memset (ihist, 0, sizeof (ihist));
//generated histogram
Imgsize=(gray. Cols) * (gray. Rows);//the total number of image data, a pixel one byte, widthstep row size, in bytes, heigh for column size
for (i=0; i{
ImgData ihist [((int) (*)) & amp; 255] + +;//gray statistics' & amp; 255 'prevent pointer overflow, i.e., more than 255 books will force between 0-255, normally the same, uchar [0255]
ImgData++;//pixel traversal//* ImgData said values, take the first address the value of x to the int class, determine whether overflow, ihist [x] value plus 1, said grayscale pixels have a for x, ImgData++ pointed to the next address
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related