opencv识别两个网球





#include "cxcore.h"     
#include "cv.h"     
#include "highgui.h"    
#include "iostream"
using namespace std;
  
// 内轮廓填充     
// 参数:     
// 1. pBinary: 输入二值图像,单通道,位深IPL_DEPTH_8U。    
// 2. dAreaThre: 面积阈值,当内轮廓面积小于等于dAreaThre时,进行填充。     
void FillInternalContours(IplImage *pBinary, double dAreaThre)     
{     
    double dConArea;     
    CvSeq *pContour = NULL;     
    CvSeq *pConInner = NULL;     
    CvMemStorage *pStorage = NULL;    
IplImage *dst = cvCreateImage(cvGetSize(pBinary), 8, 3);
    // 执行条件     
    if (pBinary)     
    {     
        // 查找所有轮廓     
        pStorage = cvCreateMemStorage(0);     
        cvFindContours(pBinary, pStorage, &pContour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);     
        // 填充所有轮廓     
        cvDrawContours(pBinary, pContour, CV_RGB(255, 255, 255), CV_RGB(255, 255, 255), 2, CV_FILLED, 8, cvPoint(0, 0));    
        // 外轮廓循环     
        int wai = 0;    
        int nei = 0;  
double minarea=1000;//这里需要修改参数!
        for (; pContour != NULL; pContour = pContour->h_next)     
        {     
            wai++;    
            // 内轮廓循环  


 double tmparea = fabs(cvContourArea(pContour));  


  printf("%f\n", tmparea);  
  cout<<endl;
     
if(tmparea < minarea)     
{    
cvSeqRemove(pContour, 0); // 删除面积小于设定值的轮廓  
continue;  
}    
CvRect aRect = cvBoundingRect( pContour, 0 ); 
cout<<"轮廓的高度为:"<<aRect.width<<"轮廓的宽度为"<<aRect.height<<endl;
cout<<"轮廓的宽高比例为:"<< aRect.width/aRect.height <<endl;
           //if ((aRect.width/aRect.height)<1)    
           //{    
           //   cvSeqRemove(pContour, 0); //删除宽高比例小于设定值的轮廓  
           //   continue;  
           //}    


            for (pConInner = pContour->v_next; pConInner != NULL; pConInner = pConInner->h_next)     
            {     
                nei++;    
                // 内轮廓面积     
                dConArea = fabs(cvContourArea(pConInner, CV_WHOLE_SEQ));    
cout<<"内轮廓的面积为:"<<endl;
                printf("%f\n", dConArea);     
            }    
            CvRect rect = cvBoundingRect(pContour,0);  
            cvRectangle(dst, cvPoint(rect.x, rect.y), cvPoint(rect.x + rect.width, rect.y + rect.height),CV_RGB(255,0,0), 1, 8, 0);  
cout<<"rect.x"<<rect.x<<"rect.y"<<rect.y<<endl;
cout<<"rect.x + rect.width"<<rect.x + rect.width<<"rect.y + rect.height"<<rect.y + rect.height<<endl;
        }     
     
        printf("wai = %d, nei = %d", wai, nei);    
        cvReleaseMemStorage(&pStorage);     
        pStorage = NULL;     
    }     
cvNamedWindow("dst");    
    cvShowImage("dst", dst); 
}     
int Otsu(IplImage* src)        
{        
    int height=src->height;        
    int width=src->width;            
  
    //histogram        
    float histogram[256] = {0};        
    for(int i=0; i < height; i++)      
    {        
        unsigned char* p=(unsigned char*)src->imageData + src->widthStep * i;        
        for(int j = 0; j < width; j++)       
        {        
            histogram[*p++]++;        
        }        
    }        
    //normalize histogram        
    int size = height * width;        
    for(int i = 0; i < 256; i++)      
    {        
        histogram[i] = histogram[i] / size;        
    }        
  
    //average pixel value        
    float avgValue=0;        
    for(int i=0; i < 256; i++)      
    {        
        avgValue += i * histogram[i];  //整幅图像的平均灰度      
    }         
  
    int threshold;          
    float maxVariance=0;        
    float w = 0, u = 0;        
    for(int i = 0; i < 256; i++)       
    {        
        w += histogram[i];  //假设当前灰度i为阈值, 0~i 灰度的像素(假设像素值在此范围的像素叫做前景像素) 所占整幅图像的比例      
        u += i * histogram[i];  // 灰度i 之前的像素(0~i)的平均灰度值: 前景像素的平均灰度值      
  
        float t = avgValue * w - u;        
        float variance = t * t / (w * (1 - w) );        
        if(variance > maxVariance)       
        {        
            maxVariance = variance;        
            threshold = i;        
        }        
    }        
  
    return threshold;        
}       
  
int main()    
{    
    IplImage *img = cvLoadImage("d:\\twowangqiu.jpg", 0);    
    IplImage *bin = cvCreateImage(cvGetSize(img), 8, 1);    
  
    int thresh = Otsu(img);    
    cvThreshold(img, bin, thresh, 255, CV_THRESH_BINARY);    
  
    FillInternalContours(bin, 200);    
  
    cvNamedWindow("img");    
    cvShowImage("img", img);    
  
    cvNamedWindow("result");    
    cvShowImage("result", bin);    
 
  
    cvWaitKey(-1);    
  
    cvReleaseImage(&img);    
    cvReleaseImage(&bin);    
  
    return 0;    
}    

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值