计算机|opencv——全图canny及先找轮廓再canny对比时间

似乎分块canny还没有全图canny快。
在没有明显噪声情况下二者提取轮廓相同。

int main( int argc, char** argv ) { //vector 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 contours; //vector hierarchy; //threshold(imgTempforContour, imgTempforContour, 100, 255, CV_THRESH_BINARY); //findContours(imgTempforContour, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE); //获得最小外接矩形,且需经过二值化,该矩形横平竖直 //vector 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 contours; vector hierarchy; //threshold(imgTempforContour, imgTempforContour, 100, 255, CV_THRESH_BINARY); findContours(cannyImage, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE); //对canny后的轮廓获得最小外接矩形,且需经过二值化,该矩形横平竖直 vector 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; }

【计算机|opencv——全图canny及先找轮廓再canny对比时间】

    推荐阅读