opencv删除二值图中较小的噪点色块

 CvSeq* contour = NULL;   
   double minarea = 100.0;   
   double tmparea = 0.0;   
CFileDialog dlg(true);   
if (dlg.DoModal()==IDOK)   
{   
    CvMemStorage* storage = cvCreateMemStorage(0);   
       
    IplImage* img_src= cvLoadImage(dlg.GetPathName(),CV_LOAD_IMAGE_ANYCOLOR);   
    IplImage* img_Clone=cvCloneImage(img_src);   
    //访问二值图像每个点的值   
    uchar *pp;   
    //显示原始图像   
    cvNamedWindow("img_src",CV_WINDOW_AUTOSIZE);   
    cvShowImage("img_src", img_src);   
       
    IplImage* img_dst = cvCreateImage(cvGetSize(img_src),IPL_DEPTH_8U,1);   
       
    //------------搜索二值图中的轮廓,并从轮廓树中删除面积小于某个阈值minarea的轮廓-------------//   
    CvScalar color = cvScalar(255,0,0);//CV_RGB(128,0,0);   
    CvContourScanner scanner = NULL;   
scanner = cvStartFindContours(img_src,storage,sizeof(CvContour),CV_RETR_CCOMP,CV_CHAIN_APPROX_NONE,cvPoint(0,0));   
    //开始遍历轮廓树   
    CvRect rect;   
    while (contour=cvFindNextContour(scanner))   
    {   
        tmparea = fabs(cvContourArea(contour));   
            rect = cvBoundingRect(contour,0);      
        if (tmparea < minarea/*||tmparea>4900*/)   
        {   
           
        //当连通域的中心点为黑色时,而且面积较小则用白色进行填充   
            pp=(uchar*)(img_Clone->imageData + img_Clone->widthStep*(rect.y+rect.height/2)+rect.x+rect.width/2);   
            if (pp[0]==0)   
            {   
                for(int y = rect.y;y<rect.y+rect.height;y++)   
                {   
                    for(int x =rect.x;x<rect.x+rect.width;x++)   
                    {   
                        pp=(uchar*)(img_Clone->imageData + img_Clone->widthStep*y+x);   
                           
                        if (pp[0]==0)   
                        {   
                            pp[0]=255;   
                        }   
                    }   
                }   
            }   
           
        }   
    }   
cvSaveImage("c://temp//aav.bmp",img_Clone);  

 

posted @ 2016-03-30 16:12 一样菜 阅读(...) 评论(...) 编辑 收藏