OpenCV的三帧差法

转自(http://blog.csdn.net/augusdi/article/details/8865499)   
[cpp]  view plain copy
  1. // threeDifferent.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "highgui.h"     
  6. #include "cv.h"     
  7. #include "cxcore.h"     
  8. #include "cvaux.h"     
  9. #include <iostream>     
  10. #include <cstdio>     
  11. #include <cstring>     
  12. #include <cmath>     
  13. #include <algorithm>     
  14. #include <queue>     
  15. #include <vector>     
  16. #include <windows.h>     
  17. using namespace std;     
  18.     
  19. #define GET_IMAGE_DATA(img, x, y)   ((uchar*)(img->imageData + img->widthStep * (y)))[x]     
  20. int T = 10;    
  21. int Num[300];    
  22. int Sum[300];    
  23.     
  24. void InitPixel(IplImage * img, int &_low, int &_top)    
  25. {    
  26.    memset(Num,0,sizeof(Num));    
  27.    memset(Sum,0,sizeof(Sum));    
  28.    _low = 255;    
  29.    _top = 0;    
  30.    for(int i = 0;i < img->height;i++)    
  31.    {    
  32.        for(int j = 0;j < img->width;j++)    
  33.        {    
  34.            int temp = ((uchar*)(img->imageData + img->widthStep*i))[j];    
  35.            if(temp < _low)    
  36.                _low = temp;    
  37.            if(temp > _top)    
  38.                _top = temp;    
  39.            Num[temp] += 1;    
  40.        }    
  41.    }    
  42.    for(int i = 1 ; i < 256 ; i++)    
  43.    {    
  44.        Sum[i] = Sum[i-1]+ i*Num[i];    
  45.        Num[i] += Num[i-1];    
  46.    }    
  47. }    
  48. int otsu (IplImage *img)    
  49. {    
  50.    int _low,_top,mbest=0;    
  51.    float mn = img->height*img->width;    
  52.    InitPixel(img,_low,_top);    
  53.    float max_otsu = 0;    
  54.    mbest = 0;    
  55.    if( _low == _top)    
  56.        mbest = _low;    
  57.    else    
  58.    {    
  59.        for(int i = _low; i< _top ; i++)    
  60.        {    
  61.            float w0 = (float)((Num[_top]-Num[i]) / mn);    
  62.            float w1 = 1 - w0;    
  63.            float u0 = (float)((Sum[_top]-Sum[i])/(Num[_top]-Num[i]));    
  64.            float u1 = (float)(Sum[i]/Num[i]);    
  65.            float u = w0*u0 + w1*u1;    
  66.            float g = w0*(u0 - u)*(u0 - u) + w1*(u1 - u)*(u1 - u);    
  67.            if( g > max_otsu)    
  68.            {    
  69.                mbest = i;    
  70.                max_otsu = g;    
  71.            }    
  72.        }    
  73.    }    
  74.    return mbest;    
  75. }    
  76. int main()    
  77. {    
  78.     int ncount=0;    
  79.     IplImage *image1=NULL;    
  80.     IplImage *image2=NULL;    
  81.     IplImage *image3=NULL;    
  82.     IplImage *Imask =NULL;    
  83.     IplImage *Imask1=NULL;    
  84.     IplImage *Imask2=NULL;    
  85.     IplImage *Imask3=NULL;    
  86.     IplImage *mframe=NULL;    
  87.     CvCapture *capture = cvCreateFileCapture("2.avi");    
  88.     //CvCapture *capture = cvCreateCameraCapture(0);     
  89.     cvNamedWindow("src");    
  90.     cvNamedWindow("dst");    
  91.     cvNamedWindow("Imask1");    
  92.     cvNamedWindow("Imask2");    
  93.     cvNamedWindow("Imask3");    
  94.     //cvCreateTrackbar("T","dst",&T,255,0);     
  95.     while(mframe=cvQueryFrame(capture))    
  96.     {    
  97.         DWORD start=GetTickCount();    
  98.         if(ncount>1000000000)    
  99.             ncount=100;    
  100.         ncount+=1;    
  101.         if(ncount==1)    
  102.         {    
  103.            image1=cvCreateImage(cvGetSize(mframe),IPL_DEPTH_8U,1);    
  104.            image2=cvCreateImage(cvGetSize(mframe),IPL_DEPTH_8U,1);    
  105.            image3=cvCreateImage(cvGetSize(mframe),IPL_DEPTH_8U,1);    
  106.            Imask =cvCreateImage(cvGetSize(mframe),IPL_DEPTH_8U,1);    
  107.            Imask1=cvCreateImage(cvGetSize(mframe),IPL_DEPTH_8U,1);    
  108.            Imask2=cvCreateImage(cvGetSize(mframe),IPL_DEPTH_8U,1);    
  109.            Imask3=cvCreateImage(cvGetSize(mframe),IPL_DEPTH_8U,1);    
  110.     
  111.            cvCvtColor(mframe,image1,CV_BGR2GRAY);    
  112.         }    
  113.         if(ncount==2)    
  114.             cvCvtColor(mframe,image2,CV_BGR2GRAY);    
  115.             
  116.         if(ncount>=3)    
  117.         {    
  118.             if(ncount==3)    
  119.                 cvCvtColor(mframe,image3,CV_BGR2GRAY);    
  120.             else    
  121.             {    
  122.                 cvCopy(image2,image1);    
  123.                 cvCopy(image3,image2);    
  124.                 cvCvtColor(mframe,image3,CV_BGR2GRAY);    
  125.             }    
  126.     
  127.             cvAbsDiff(image2,image1,Imask1);    
  128.             cvAbsDiff(image3,image2,Imask2);    
  129.      
  130.             //cvShowImage("Imask1",Imask1);     
  131.             //cvShowImage("Imask2",Imask2);     
  132.                
  133.             int mbest1 = otsu(Imask1);    
  134.             cvSmooth(Imask1, Imask1, CV_MEDIAN);    
  135.             cvThreshold(Imask1,Imask1,/*mbest1*/10, 255, CV_THRESH_BINARY);    
  136.     
  137.             int mbest2 = otsu(Imask2);    
  138.             cvSmooth(Imask2,Imask2,  CV_MEDIAN);    
  139.             cvThreshold(Imask2,Imask2,/*mbest2*/10, 255, CV_THRESH_BINARY);    
  140.                 
  141.            // cout<<mbest1<<" "<<mbest2<<endl;    
  142.     
  143.             cvAnd(Imask1,Imask2,Imask);    
  144.     
  145.            // cvErode(Imask, Imask);   
  146.            // cvDilate(Imask,Imask);   
  147.     
  148.             DWORD finish=GetTickCount();    
  149.            // cout<<finish-start<<"ms"<<endl;     
  150.                
  151.             cvShowImage("src",image2);    
  152.             cvShowImage("dst",Imask);    
  153.         }    
  154.         char c = cvWaitKey(30);    
  155.         if(c==27)    
  156.             break;    
  157.     }    
  158.     
  159.     return 0;    
  160. }    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值