[OpenCv] 밸브 값 에 적응 하 는 이치 화 처리

4030 단어 OpenCv영상 처리
#include 
#include 
#include 

using namespace std;
double TwoDimentionOtsu(IplImage *image);
int main()  
{ 
        IplImage* srcImage = cvLoadImage( "C:\\Users\\Administrator\\Desktop\\111.jpg",0 ); 
        assert(NULL != srcImage);

        cvNamedWindow("src",0);
        cvShowImage("src",srcImage); 

       // clock_t start_time=clock();

        //       
        double threshold = TwoDimentionOtsu(srcImage);//70,125

      //  clock_t end_time=clock();
       // cout<< "Running time is: "<(end_time-start_time)/CLOCKS_PER_SEC*1000<width;  
        int height = image->height; 
        double dHistogram[256][256]={0.0};                                //           
        unsigned long sum0 = 0,sum1 = 0;                                //sum0           ,sum1  3x3        
        uchar* data = (uchar*)image->imageData; 
        for(int i=0; iwidthStep + j];//nData1     (i,j)    
                        sum0 += nData1;
                        unsigned char nData2 = 0;  //nData2      (i,j)             
                        int nData3 = 0;                                        //nData3      (i,j)           ,  9              
                        for(int m=i-1; m<=i+1; m++)  
                        {  
                                for(int n=j-1; n<=j+1; n++)  
                                {  
                                        if((m>=0)&&(m=0)&&(nwidthStep + n];
                                }  
                        }  
                        nData2 = (unsigned char)(nData3/9);    //            ,    
                        sum1 += nData2;
                        dHistogram[nData1][nData2]++;  
                }  
        }

        long N = height*width;                //      
        t = sum0/N;                                        //       
        s = sum1/N;                                        //          

        s0 = s;
        t0 = t; 
        for(int j=0; j<256; j++)
                for(int i=0; i<256; i++) 
                {
                        dHistogram[i][j] = dHistogram[i][j]/N;  //           
                }

                double w0 = 0.0,w1 = 0.0,u0i = 0.0,u1i = 0.0,u0j = 0.0,u1j = 0.0;

                do
                {
                        t0 = t;
                        s0 = s;
                        w0 = w1 = u0i = u1i = u0j = u1j = 0.0;
                        for (int i = 0,j; i < 256; i++)
                        {
                                for (j = 0; j < s0; j++)
                                {
                                        w0 += dHistogram[i][j];
                                        u0j += dHistogram[i][j] * j;
                                }

                                for (; j < 256; j++)
                                {
                                        w1 += dHistogram[i][j];
                                        u1j += dHistogram[i][j] * j;
                                }

                        }
                        for (int j = 0,i = 0; j < 256; j++)
                        {
                                for (i = 0; i < t0; i++)
                                        u0i += dHistogram[i][j] * i;
                                for (; i < 256; i++)
                                        u1i += dHistogram[i][j] * i;
                        }
                        u0i /= w0;
                        u1i /= w1 ;
                        u0j /= w0;
                        u1j /= w1;

                        t = (u0i + u1i)/2;
                        s = (u0j + u1j)/2;
                }while ( t != t0);//             ,    ,     

                return t;//  t    ,       ,          ,     

}

좋은 웹페이지 즐겨찾기