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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| #include <opencv2/opencv.hpp> #include <iostream> #include <string> #include <cmath> using namespace std; using namespace cv;
void HarrisCornerDetector(Mat& src, Mat& R, int aperture_size, double k);
int main(int argc, const char** argv) { char* filename = new char[100]; int apertureSize = 3; double k = 0.04;
if (argc != 4) { cout << "Illegal Input." << endl; cout << "HarrisDetector.exe $path $k $aperture_size." << endl; filename = "Sydney_Opera_House_Sails_edit02_adj.jpg"; } else { sscanf_s(argv[1], "%s", filename, 99); sscanf_s(argv[2], "%lf", &k); sscanf_s(argv[3], "%d", &apertureSize); }
Mat src = imread(filename); if (!src.data) { cout << "imread failed" << endl; return 0; } Mat dst;
cout << "path: " << filename << endl; cout << "k: " << k << endl; cout << "aperture_size: " << apertureSize << endl;
HarrisCornerDetector(src, dst, apertureSize, k); return 0; }
int maxValue(Mat& img, int size, int y, int x) { uchar maxval = 0; for (int i = y-size; i<y + size; ++i) { if (i < 0 || i >= img.rows)continue; for (int j = x-size; j< x + size; ++j) { if (j<0 || j >= img.cols)continue; if (img.at<uchar>(i,j) > maxval) { maxval = img.at<uchar>(i, j); } } } return maxval; }
void HarrisCornerDetector(Mat& src, Mat& R, int aperture_size, double k) { Mat src_gray; cvtColor(src, src_gray, cv::COLOR_BGR2GRAY); normalize(src_gray, src_gray, 0, 255, NORM_MINMAX); convertScaleAbs(src_gray, src_gray);
R.create(src_gray.size(), CV_32FC1); Mat Ix, Iy;
Sobel(src_gray, Ix, CV_32FC1, 1, 0, aperture_size); Sobel(src_gray, Iy, CV_32FC1, 0, 1, aperture_size);
Mat IxIx(src_gray.size(), CV_32FC1); Mat IxIy(src_gray.size(), CV_32FC1); Mat IyIy(src_gray.size(), CV_32FC1); Mat largeEigen(src_gray.size(), CV_32FC1); Mat smallEigen(src_gray.size(), CV_32FC1);
Size size = src_gray.size(); for (int i = 0; i < size.height; ++i) { for (int j = 0; j < size.width; ++j) { IxIx.at<float>(i,j) = Ix.at<float>(i, j) * Ix.at<float>(i, j); IxIy.at<float>(i,j) = Ix.at<float>(i, j) * Iy.at<float>(i, j); IyIy.at<float>(i,j) = Iy.at<float>(i, j) * Iy.at<float>(i, j); } }
Size block(3,3); GaussianBlur(IxIx, IxIx, block, 0); GaussianBlur(IxIy, IxIy, block, 0); GaussianBlur(IyIy, IyIy, block, 0);
for (int i = 0; i < size.height; ++i) { for (int j = 0; j < size.width; ++j) { float a = IxIx.at<float>(i, j); float b = IxIy.at<float>(i, j); float c = b; float d = IyIy.at<float>(i, j); R.at<float>(i,j) = (a*d - b*c) - k*(a + d)*(a + d); largeEigen.at<float>(i, j) = ((a + d) + sqrt((a + d)*(a + d) - 4 * (a*d - b*c))) / 2; smallEigen.at<float>(i, j) = ((a + d) - sqrt((a + d)*(a + d) - 4 * (a*d - b*c))) / 2; } } cout << endl; normalize(R, R, 0, 255, NORM_MINMAX, CV_32FC1, Mat()); convertScaleAbs(R, R); int threshold = 50; int NMS_size = 15; for (int i = 0; i < size.height; ++i) { for (int j = 0; j < size.width; ++j) { if ((int)R.at<uchar>(i, j) > threshold) { if (R.at<uchar>(i, j) == maxValue(R, NMS_size, i, j)) { circle(src, Point(j, i), 5, Scalar(0, 0, 255.0), 2, 8, 0); } } } }
imshow("LargeEigen", largeEigen); imshow("SmallEigen", smallEigen); imshow("R", R); imshow("result", src);
waitKey(0);
imwrite("LargeEigen.png", largeEigen); imwrite("SmallEigen.png", smallEigen); imwrite("R.png", R); imwrite("result.png", src); destroyAllWindows(); }
|