aihot  2017-05-28 07:13:08  OpenCV |   查看评论   
  •  
  •         }
  •  
  •     }
  •  
  •  
  •  
  • f:
  •  
  •     int l_dis = abs(pos[0].y – pos[1].y);
  •  
  •     int s_dis = abs(pos[2].x – pos[3].x);
  •  
  •     int tmp_dis;
  •  
  •     if(l_dis > s_dis)
  •  
  •     {
  •  
  •         printf("偏心率:%f\n",l_dis*1.0/s_dis);
  •  
  •     }
  •  
  •     else
  •  
  •     {
  •  
  •         tmp_dis = l_dis;
  •  
  •         l_dis = s_dis;
  •  
  •         s_dis = tmp_dis;
  •  
  •         printf("偏心率:%f\n",l_dis*1.0/s_dis);
  •  
  •     }
  •  
  •  
  •  
  •     return 0;
  •  
  • }
  •  
  •  
  •  
  • void Getprobability(IplImage *src)
  •  
  • {
  •  
  •     memset(per,0,sizeof(per));
  •  
  •     int width = src->width;
  •  
  •     int height = src->height;
  •  
  •     for(int i = 0; i < height; i++) {
  •  
  •         for(int j = 0; j < width; j++) {
  •  
  •             per[(int)cvGet2D(src,i,j).val[0]]++;
  •  
  •         }
  •  
  •     }
  •  
  •     int PixlNum = width * height;
  •  
  •     for(i = 0; i < 256; i++)
  •  
  •         per[i] = per[i] / PixlNum;
  •  
  •  
  •  
  • }
  •  
  •  
  •  
  • int GetThreshold(double *const prob)
  •  
  • {
  •  
  •     int threshold = 0;
  •  
  •     double maxf = 0;
  •  
  •     for (int crrctThrshld = 1; crrctThrshld < 256 – 1; ++crrctThrshld) {
  •  
  •         double W0 = 0, W1 = 0, U0 = 0, U1 = 0;
  •  
  •         int i = 0;
  •  
  •         for (i = 0; i <= crrctThrshld; ++i) { 
  •  
  •             U0 += i * prob[i];
  •  
  •             W0 += prob[i];
  •  
  •         }
  •  
  •         for (; i < 256; ++i) {
  •  
  •             U1 += i * prob[i];
  •  
  •             W1 += prob[i];
  •  
  •         }
  •  
  •         if (W1 == 0 || W1 == 0)
  •  
  •             continue;
  •  
  •         U0 /= W0;
  •  
  •         U1 /= W1;
  •  
  •         double D0 = 0, D1= 0;
  •  
  •         for (i = 0; i <= crrctThrshld; ++i) 
  •  
  •             D0 += pow((i – U0) * prob[i], 2.0);
  •  
  •         for (; i < 256; ++i) 
  •  
  •             D1 += pow((i – U1) * prob[i], 2.0);
  •  
  •         D0 /= W0;
  •  
  •         D1 /= W1;
  •  
  •         double Dw = pow(D0, 2.0) * W0 + pow(D1, 2.0) * W1;
  •  
  •         double Db = W0 * W1 * pow((U1 – U0), 2.0);
  •  
  •         double f = Db / (Db + Dw);
  •  
  •         if (maxf < f) {
  •  
  •             maxf = f;
  •  
  •             threshold = crrctThrshld;
  •  
  •         }
  •  
  •     }
  •  
  •     return threshold;
  •  
  • }
  •  
  •  
  •  
  • void proBorder(IplImage *src) // 边界的处理
  •  
  • {
  •  
  •     int i,j;
  •  
  •     int height = src->height;
  •  
  •     int width = src->width;
  •  
  •  
  •  
  •     int N = 100;
  •  
  •  
  •  
  •     for(i = 0; i < N * width; i += width) // i表示向下走左上角
  •  
  •     {
  •  
  •         for(j = 0; j < N ; j++)
  •  
  •         {
  •  
  •             int index = i + j;
  •  
  •             src->imageData[index] = (char)255;
  •  
  •         }
  •  
  •     }
  •  
  •  
  •  
  •     int NN = 150;
  •  
  •     int sw = width * (height – NN);// 左下角 三角形
  •  
  •     int  t = 1;
  •  
  •     for(i = sw; i < sw + NN * width; i += width,t++)
  •  
  •     {
  •  
  •         for(j = 0; j < t; j++)
  •  
  •         {
  •  
  •             int index = i + j;
  •  
  •             src->imageData[index] = (char)255;
  •  
  •         }
  •  
  •     }
  •  
  •  
  •  
  •     int se = (height – NN – 1) * width; // 右下角
  •  
  •     t = 0;
  •  
  •     for(i = se; i < width * height ; i += width,t++)
  •  
  •     {
  •  
  •         for(j = 0; j < t; j++)
  •  
  •         {
  •  
  •             int index = i + j – t;
  •  
  •             src->imageData[index] = (char)255;
  •  
  •         }
  •  
  •     }
  •  
  •  
  •  
  •     int ne = width – NN;                   // 右上角 三角形剪切
  •  
  •     t = 0;
  •  
  •     for(i = ne; i < NN * width; i +=width,t++)
  •  
  •     {
  •  
  •         for(j = 0; j < NN – t; j++)
  •  
  •         {
  •  
  •             int index = i + j + t;
  •  
  •             src->imageData[index] = (char)255;
  •  
  •         }
  •  
  •     }
  •  
  •  
  •  
  • }
  •  
  •  
  •  
  • void Threshold(IplImage *src)
  •  
  • {
  •  
  •     int width = src->width;
  •  
  •     int height = src->height;
  •  
  •     float minpixel = cvGet2D(src,0,0).val[0];
  •  
  •     float maxpixel = cvGet2D(src,0,0).val[0];
  •  
  •     CvScalar s;
  •  
  •     for(int i = 0; i < height; i++){
  •  
  •         for(int j = 0; j < width; j++){
  •  
  •             s = cvGet2D(src,i,j);
  •  
  •             if(s.val[0] > maxpixel)
  •  
  •                 maxpixel = s.val[0];
  •  
  •             if(s.val[0] < minpixel)
  •  
  •                 minpixel = s.val[0];
  •  
  •         }
  •  
  •     }
  •  
  •     float firstgrey = (maxpixel + minpixel) / 2;
  •  
  •     printf("%f\n",firstgrey);
  •  
  •  
  •  
  •     float lastgrey;
  •  
  •     float sum1 = 0,sum2 = 0;
  •  
  •     int num1 = 0,num2 = 0;
  •  
  •     int result = 0;
  •  
  •     while(1)
  •  
  •     {
  •  
  •         result ++;
  •  
  •         for(i = 0; i < height; i++){
  •  
  •             for(int j = 0; j < width; j++){
  •  
  •                 s = cvGet2D(src,i,j);
  •  
  •                 if(s.val[0] < firstgrey)
  •  
  •                 {
  •  
  •                     sum1 += s.val[0];
  •  
  •                     num1++;
  •  
  •                 }
  •  
  •                 if(s.val[0] < firstgrey)
  •  
  •                 {
  •  
  •                     sum2 += s.val[0];
  •  
  •                     num2++;
  •  
  •                 }
  •  
  •             }
  •  
  •         }
  •  
  •         lastgrey = (sum1/num1 + sum2/num2)/2;
  •  
  •  
  •  
  •         if((int)lastgrey == (int)firstgrey)
  •  
  •             break;
  •  
  •         else
  •  
  •         {
  •  
  •             firstgrey = lastgrey;
  •  
  •             sum1 = sum2 = 0;
  •  
  •             num1 = num2 = 0;
  •  
  •         }
  •  
  •     }
  •  
  •  
  •  
  •     lastgrey = (sum1/num1 + sum2/num2)/2;
  •  
  •     printf("%f %d\n",firstgrey,result);
  •  
  • }
  •  

    除特别注明外,本站所有文章均为 赢咖4注册 原创,转载请注明出处来自OpenCV提取轮廓(去掉面积小的轮廓)

    留言与评论(共有 0 条评论)
       
    验证码:
    [lianlun]1[/lianlun]