forked from vivienmetayer/Generalized_Hough_OpenCV_sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathght_serial.cpp
63 lines (47 loc) · 1.9 KB
/
ght_serial.cpp
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
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
// Load the teimplate and target images in grayscale.
Mat img_template = imread("template_small_2.png", IMREAD_GRAYSCALE);
Mat img = imread("image.png", IMREAD_GRAYSCALE);
if (img_template.empty() || img.empty()) {
cerr << "Error: Could not load images." << endl;
return -1;
}
// Display images verify successful loading.
// namedWindow("Template", WINDOW_NORMAL);
// imshow("Template", img_template);
// namedWindow("Image", WINDOW_NORMAL);
// imshow("Image", img);
// Processing: Edge detection and gradient computation.
Mat canny_template, dx_template, dy_template;
Mat canny, dx, dy;
int cannLow = 150, cannHigh = 230, sobelKernelSize = 5;
double sobelScale = 0.05;
// Apply Canny edge detection.
Canny(img_template, canny_template, cannLow, cannHigh);
Canny(img, canny, cannLow, cannHigh);
// Compute gradients.
Sobel(img_template, dx_template, CV_32F, 1, 0, sobelKernelSize, sobelScale);
Sobel(img_template, dy_template, CV_32F, 0, 1, sobelKernelSize, sobelScale);
Sobel(img, dx, CV_32F, 1, 0, sobelKernelSize, sobelScale);
Sobel(img, dy, CV_32F, 0, 1, sobelKernelSize, sobelScale);
// Display preprocessed results.
namedWindow("Canny Template", WINDOW_NORMAL);
imshow("Canny Template", canny_template);
namedWindow("Canny Image", WINDOW_NORMAL);
imshow("Canny Image", canny);
namedWindow("Gradient X Template", WINDOW_NORMAL);
imshow("Gradient X Template", dx_template);
namedWindow("Gradient Y Template", WINDOW_NORMAL);
imshow("Gradient Y Template", dy_template);
namedWindow("Gradient X Image", WINDOW_NORMAL);
imshow("Gradient X Image", dx);
namedWindow("Gradient Y Image", WINDOW_NORMAL);
imshow("Gradient Y Image", dy);
waitKey(0);
return 0;
}