|
| 1 | +#include <opencv2/opencv.hpp> |
| 2 | +#include <iostream> |
| 3 | + |
| 4 | +using namespace cv; |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +void harrisCornerDetector() { |
| 8 | + |
| 9 | + Mat image, gray; |
| 10 | + Mat output, output_norm, output_norm_scaled; |
| 11 | + |
| 12 | + // Loading the actual image |
| 13 | + //image = imread("C:/Users/kemik/OneDrive/Dokumenter/Downloads/squares.png", IMREAD_COLOR); |
| 14 | + image = imread("C:/Users/kemik/OneDrive/Dokumenter/Downloads/house.jpg", IMREAD_COLOR); |
| 15 | + |
| 16 | + // Edge cases |
| 17 | + if (image.empty()) { |
| 18 | + cout << "Error loading image" << endl; |
| 19 | + } |
| 20 | + cv::imshow("Original image", image); |
| 21 | + |
| 22 | + // Converting the color image into grayscale |
| 23 | + cvtColor(image, gray, cv::COLOR_BGR2GRAY); |
| 24 | + |
| 25 | + // Detecting corners using the cornerHarris built in function |
| 26 | + output = Mat::zeros(image.size(), CV_32FC1); |
| 27 | + cornerHarris(gray, output, |
| 28 | + 3, // Neighborhood size |
| 29 | + 3, // Aperture parameter for the Sobel operator |
| 30 | + 0.04); // Harris detector free parameter |
| 31 | + |
| 32 | + // Normalizing - Convert corner values to integer so they can be drawn |
| 33 | + normalize(output, output_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat()); |
| 34 | + convertScaleAbs(output_norm, output_norm_scaled); |
| 35 | + |
| 36 | + // Drawing a circle around corners |
| 37 | + for (int j = 0; j < output_norm.rows; j++) { |
| 38 | + for (int i = 0; i < output_norm.cols; i++) { |
| 39 | + if ((int)output_norm.at<float>(j, i) > 100) { |
| 40 | + circle(image, Point(i, j), 4, Scalar(0, 0, 255), 2, 8, 0); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Displaying the result |
| 46 | + cv::resize(image, image, cv::Size(), 1.5, 1.5); |
| 47 | + cv::imshow("Output Harris", image); |
| 48 | + cv::waitKey(); |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +void shiTomasiCornerDetector() { |
| 55 | + |
| 56 | + Mat image, gray; |
| 57 | + Mat output, output_norm, output_norm_scaled; |
| 58 | + |
| 59 | + // Loading the actual image |
| 60 | + //image = imread("C:/Users/kemik/OneDrive/Dokumenter/Downloads/squares.png", IMREAD_COLOR); |
| 61 | + image = imread("C:/Users/kemik/OneDrive/Dokumenter/Downloads/house.jpg", IMREAD_COLOR); |
| 62 | + |
| 63 | + // Edge cases |
| 64 | + if (image.empty()) { |
| 65 | + cout << "Error loading image" << endl; |
| 66 | + } |
| 67 | + cv::imshow("Original image", image); |
| 68 | + |
| 69 | + // Converting the color image into grayscale |
| 70 | + cvtColor(image, gray, cv::COLOR_BGR2GRAY); |
| 71 | + |
| 72 | + |
| 73 | + // Detecting corners using the goodFeaturesToTrack built in function |
| 74 | + vector<Point2f> corners; |
| 75 | + goodFeaturesToTrack(gray, |
| 76 | + corners, |
| 77 | + 100, // Max corners to detect |
| 78 | + 0.01, // Minimal quality of corners |
| 79 | + 10, // Minimum Euclidean distance between the returned corners |
| 80 | + Mat(), // Optional region of interest |
| 81 | + 3, // Size of an average block for computing a derivative covariation matrix over each pixel neighbothood |
| 82 | + false, // Use Harri Detector or cornerMinEigenVal - Like when you create your own |
| 83 | + 0.04); // Free parameter for the Harris detector |
| 84 | + |
| 85 | + |
| 86 | + // Drawing a circle around corners |
| 87 | + for (size_t i = 0; i < corners.size(); i++){ |
| 88 | + circle(image, corners[i], 4, Scalar(0, 255, 0), 2, 8, 0); |
| 89 | + } |
| 90 | + |
| 91 | + // Displaying the result |
| 92 | + cv::resize(image, image, cv::Size(), 1.5, 1.5); |
| 93 | + cv::imshow("Output Shi-Tomasi", image); |
| 94 | + cv::waitKey(); |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +int main() |
| 99 | +{ |
| 100 | + |
| 101 | + harrisCornerDetector(); |
| 102 | + shiTomasiCornerDetector(); |
| 103 | + |
| 104 | + return 0; |
| 105 | +} |
0 commit comments