1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include "opencv2/imgproc.hpp" #include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include <iostream>
using namespace cv; using namespace std;
Mat image, src, src_gray, grad; int ksize = 3; double scale = 1; double delta = 0; int ddepth = CV_16S; int lowThreshold = 40; int highThreshold = 120;
void onSobel(int, void *) { Mat grad_x, grad_y; Mat abs_grad_x, abs_grad_y; Sobel(src_gray, grad_x, ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT); Sobel(src_gray, grad_y, ddepth, 0, 1, ksize, scale, delta, BORDER_DEFAULT);
convertScaleAbs(grad_x, abs_grad_x); convertScaleAbs(grad_y, abs_grad_y); addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
const string winname = "Sobel Edge Detector"; imshow(winname, grad); }
void onScharr(int, void *) { Mat grad_x, grad_y; Mat abs_grad_x, abs_grad_y; Scharr(src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT); Scharr(src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT);
convertScaleAbs(grad_x, abs_grad_x); convertScaleAbs(grad_y, abs_grad_y); addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
const string winname = "Scharr Edge Detector"; imshow(winname, grad); }
void onLaplacian(int, void *) { Mat grad, abs_grad; Laplacian(src_gray, grad, ddepth, ksize, scale, delta, BORDER_DEFAULT);
convertScaleAbs(grad, abs_grad);
const string winname = "Laplacian Edge Detector"; imshow(winname, abs_grad); }
void onCanny(int, void *) { Mat dst, detected_edges;
Canny(src, detected_edges, lowThreshold, highThreshold, ksize); dst = Scalar::all(0); src.copyTo(dst, detected_edges);
const string winname = "Canny Edge Detector"; imshow(winname, dst); }
int main(int argc, char **argv) { string imageName = "../lena.jpg"; image = imread(imageName, IMREAD_COLOR); if (image.empty()) { printf("Error opening image: %s\n", imageName.c_str()); return 1; }
GaussianBlur(image, src, Size(5, 5), 1.4, 1.4, BORDER_DEFAULT); cvtColor(src, src_gray, COLOR_BGR2GRAY);
double t0 = cv::getTickCount(); onSobel(0, nullptr); double t1 = cv::getTickCount(); onScharr(0, nullptr); double t2 = cv::getTickCount(); onLaplacian(0, nullptr); double t3 = cv::getTickCount(); onCanny(0, nullptr); double t4 = cv::getTickCount();
double tickFrequency = cv::getTickFrequency();
cout << "sobel: " << (t1 - t0) / tickFrequency << endl; cout << "scharr: " << (t2 - t1) / tickFrequency << endl; cout << "laplacian: " << (t3 - t2) / tickFrequency << endl; cout << "canny: " << (t4 - t3) / tickFrequency << endl;
waitKey(0); return 0; }
|