当前位置:   article > 正文

php opencv二值化,opencv 摄像头采集 图像二值化源码

opencv的二值化的源码

#include

#include

#include

#include

int main(int argc,char**argv)

{

IplImage* pFrame = NULL;

IplImage* pFrImg = NULL;

IplImage* pBkImg = NULL;

CvMat* pFrameMat = NULL;

CvMat* pFrMat = NULL;

CvMat* pBkMat = NULL;

CvCapture* pCapture = NULL;

int nFrmNum = 0;

cvNamedWindow("video",1);

cvNamedWindow("background",1);

cvNamedWindow("foreground",1);

cvMoveWindow("video",30,0);

cvMoveWindow("background",360,0);

cvMoveWindow("foreground",690,0);

if(argc>2)

{

fprintf(stderr, "Usage: bkgrd

[video_file_name]\n");

return -1;

}

if(argc==1)

{

if(!(pCapture =

cvCaptureFromCAM(-1)))

{

fprintf(stderr, "Can not open camera.\n");

return

-2;

}

}

if(argc==2)

if( !(pCapture =

cvCaptureFromFile(argv[1])))

{

fprintf(stderr, "Can not open video file %s\n", argv[1]);

return

-2;

}

while(pFrame =

cvQueryFrame( pCapture ))

{

nFrmNum

++;

if(nFrmNum==1)

{

pBkImg =

cvCreateImage(cvSize(pFrame->width,

pFrame->height), IPL_DEPTH_8U,1);

pFrImg =

cvCreateImage(cvSize(pFrame->width,

pFrame->height), IPL_DEPTH_8U,1);

pBkMat =

cvCreateMat(pFrame->height,

pFrame->width, CV_32FC1);

pFrMat =

cvCreateMat(pFrame->height,

pFrame->width, CV_32FC1);

pFrameMat =

cvCreateMat(pFrame->height,

pFrame->width, CV_32FC1);

cvCvtColor(pFrame, pBkImg, CV_BGR2GRAY);

cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);

cvConvert(pFrImg, pFrameMat);

cvConvert(pFrImg, pFrMat);

cvConvert(pFrImg, pBkMat);

}

else

{

cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);

cvConvert(pFrImg, pFrameMat);

cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0);

cvAbsDiff(pFrameMat, pBkMat, pFrMat);

cvThreshold(pFrMat, pFrImg, 60, 255.0, CV_THRESH_BINARY);

cvErode(pFrImg, pFrImg, 0, 1);

cvDilate(pFrImg, pFrImg, 0, 1);

cvRunningAvg(pFrameMat, pBkMat, 0.003, 0);

cvConvert(pBkMat, pBkImg);

cvShowImage("video", pFrame);

cvShowImage("background", pBkImg);

cvShowImage("foreground", pFrImg);

if( cvWaitKey(2) >= 0 )

break;

}

}

cvDestroyWindow("video");

cvDestroyWindow("background");

cvDestroyWindow("foreground");

cvReleaseImage(&pFrImg);

cvReleaseImage(&pBkImg);

cvReleaseMat(&pFrameMat);

cvReleaseMat(&pFrMat);

cvReleaseMat(&pBkMat);

cvReleaseCapture(&pCapture);

return 0;

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/954938
推荐阅读
相关标签
  

闽ICP备14008679号