Opencv,腐蚀,膨胀,轮廓检测,轮廓外接多边形

//形态学腐蚀
    cvErode(pDstImage,pDstImage,0,3);
    //形态学膨胀
    cvDilate(pDstImage,pDstImage,0,4);
    //中值滤波
    cvSmooth(pDstImage,pDstImage, CV_MEDIAN);//默认窗口大小为3*3
    cvShowImage("vei",pDstImage);
    if(pNum == 0)
        storage = cvCreateMemStorage(0);
    contours = NULL;
    //找出轮廓保存到countours中
    cvFindContours(pDstImage, storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

    bitmap->Canvas->BeginScene(); //bitmap开始显示

    //从轮廓中取出外接矩形
    cv::Rect r1;
    double minArea = 350;
    std::vector<cv::Rect >curMan,preMan;
    //从轮廓中取出外接矩形
    while(contours)
    {
        r1 = cvBoundingRect(contours,1);
        double tmparea = fabs(cvContourArea(contours));
        contours = contours->h_next;
        if(tmparea<minArea)continue;
        preMan.push_back(r1);
        cvRectangleR(pDstImage,r1,CV_RGB(255,255,255),2);

    }
    cvShowImage("vi",pDstImage);
    //取出嵌套的多余矩形
    for(size_t i=0; i<preMan.size(); i++)
    {
        cv::Rect r1=preMan[i];
        size_t j=0 ;
        for(; j<preMan.size();j++)
        {
            if(i!=j && (r1&preMan[j])==r1)
            {
                break;
            }
        }
        if(j == preMan.size())
        {
            curMan.push_back(r1);
        }
    }

 

posted @ 2016-03-31 16:16  一样菜  阅读(3308)  评论(0编辑  收藏  举报