opencv——全图canny及先找轮廓再canny对比时间

似乎分块canny还没有全图canny快。

在没有明显噪声情况下二者提取轮廓相同。 

int main( int argc, char** argv ) 
{
    //vector<Mat> test;
    Mat test = imread(".\\3.bmp", 0);
    //readImgs(".\\", 1, test);
    unsigned char* imgtest=NULL,*outimg=NULL;
    imgtest = test.data;

    //Mat img/*(test[0].rows, test[0].cols, CV_8UC1)*/; //img = test[0];这样是使用同一段空间
    //img = test[0].clone();//这样不使用同一段空间,拷贝一段相同数据

    Mat img(2048, 2592, CV_8UC1, imgtest);
    
    Mat imgTempforContour = img.clone();
    Mat imgTempforCanny = img.clone();
    Mat partImg;

    //先找轮廓,再分别扩3像素分块canny求平均用时
    //for (int i = 0; i < 1000; ++i)
    //{
    //    vector<vector<Point>> contours;
    //    vector<Vec4i> hierarchy;
    //    threshold(imgTempforContour, imgTempforContour, 100, 255, CV_THRESH_BINARY);
    //    findContours(imgTempforContour, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE);//获得最小外接矩形,且需经过二值化,该矩形横平竖直
    //    vector<Rect> boundRect(contours.size());  //定义外接矩形集合
    //    Mat dst(2048, 2592, CV_8UC1, Scalar::all(0));//用于拼接图

    //    for (int i = 0; i < contours.size(); i++)
    //    {
    //        boundRect[i] = boundingRect(Mat(contours[i]));
    //        /*rectangle(imgTempforContour, Point(boundRect[i].x-5, boundRect[i].y-5),
    //            Point(boundRect[i].x + boundRect[i].width+5, boundRect[i].y + boundRect[i].height+5),
    //            Scalar::all(150), 1, 8);*///只是画矩形

    //        img(Range(boundRect[i].y - 3, boundRect[i].height + boundRect[i].y + 3), Range(boundRect[i].x - 3, boundRect[i].width + boundRect[i].x + 3)
    //            /*boundRect[i]*/).copyTo(partImg);  //由轮廓往外扩3pixel取块
    //        Mat cannyImg(partImg.rows, partImg.cols, CV_8UC1);
    //        Canny(partImg, cannyImg, 100, 150, 3);  //对分块做canny

              //分块做完canny后还回原位
    //        //cannyImg.copyTo(dst(Rect(boundRect[i].x - 3, boundRect[i].y - 3, boundRect[i].width + 6, boundRect[i].height + 6)));
    //    }
    //}

    //对分块拼回的轮廓输出坐标
    /*outimg = dst.data;
    int step = dst.cols;
    ofstream contour;
    contour.open("partstoallcontour.txt", ios::app);
    for (int row = 0; row < img.rows; ++row)
    {
        for (int col = 0; col < img.cols; ++col)
        {
            if (255 == outimg[row*step + col])
                contour << row << "  " << col << endl;
        }
    }
    contour.close();*/

    //全图canny
    Mat cannyImage(img.rows, img.cols, CV_8UC1);

    Canny(imgTempforCanny, cannyImage, 100, 150, 3);//canny之前可不经过二值化

    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    //threshold(imgTempforContour, imgTempforContour, 100, 255, CV_THRESH_BINARY);
    findContours(cannyImage, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE);//对canny后的轮廓获得最小外接矩形,且需经过二值化,该矩形横平竖直
    vector<Rect> boundRect(contours.size());  //定义外接矩形集合
    imgTempforContour.setTo(Scalar::all(255));
    for (int i = 0; i < contours.size(); i++)
    {
        boundRect[i] = boundingRect(Mat(contours[i]));
        rectangle(imgTempforContour, Point(boundRect[i].x-5, boundRect[i].y-5),
            Point(boundRect[i].x + boundRect[i].width+5, boundRect[i].y + boundRect[i].height+5),
            Scalar::all(255), 1, 8);//只是画矩形
    }

    //outimg = cannyImage.data;
    //Mat dst(2048, 2592, CV_8UC1, outimg);
    //int step = cannyImage.cols;

    //全图canny后输出轮廓
    /*ofstream contour;
    contour.open("cannycontour.txt", ios::app);
    for (int row = 0; row < img.rows; ++row)
    {
        for (int col = 0; col < img.cols; ++col)
        {
            if (255 == outimg[row*step + col])
                contour << row << "  " << col << endl;
        }
    }
    contour.close();*/

    Mat scaledst;
    resize(imgTempforContour, scaledst, Size(), 1, 1);
    imshow("rect", scaledst);

    /*resize(dst, scaledst, Size(), 0.6, 0.6);
    imshow("Components", scaledst);

    resize(imgTempforCanny, scaledst, Size(), 0.6, 0.6);
    imshow("last", scaledst);
    */
    waitKey();

    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值