Hi, I am trying to implement Harris corner detection in OpenCV C++ without using the built-in function CornerHarris.
This is the code I wrote and is not working. What is the mistake in the code?
int main(int argc, const char** argv) {
Mat src;
Mat grad_x, grad_y;
Mat Ixx,Ixy,Iyy;
Mat H;
Mat Hxx, Hxy, Hyy;
Mat tr, det, Response;
double sigmaX, sigmaY;
Size gaussian_window_size = Size(3, 3);
cv::CommandLineParser parser(argc, argv,
"{@input |D:/Codebases/VS_Workspace/DIP_Book_Images/chessboard.png|input image}"
);
const String window_name = "Harris Corner Demo ";
int ksize = 3;
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
String imageName = parser.get<String>("@input");
src = imread(samples::findFile(imageName), IMREAD_GRAYSCALE);
src.convertTo(src, CV_32F);
if (src.empty())
{
cout << "Error reading image " << imageName << endl;
return EXIT_FAILURE;
}
Sobel(src, grad_x, ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT);
Sobel(src, grad_y, ddepth, 0, 1, ksize, scale, delta, BORDER_DEFAULT);
Ixx = grad_x.mul(grad_x);
Ixy = grad_x.mul(grad_y);
Iyy = grad_y.mul(grad_y);
sigmaX = 0;
sigmaY = 0;
GaussianBlur(Ixx, Hxx, gaussian_window_size, sigmaX, sigmaY, BORDER_DEFAULT);
GaussianBlur(Ixy, Hxy, gaussian_window_size, sigmaX, sigmaY, BORDER_DEFAULT);
GaussianBlur(Iyy, Hyy, gaussian_window_size, sigmaX, sigmaY, BORDER_DEFAULT);
tr = Hxx + Hyy;
det = Hxx.mul(Hyy) - Hxy.mul(Hxy);
Mat tr2 = tr.mul(tr);
Mat tr3 = tr2*0.04f;
Response = det - tr3;
Mat max_resp;
dilate(Response, max_resp, getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)));
Mat t1, t2;
t1 = (Response == max_resp);
t2 = Response > 1e4;
Mat corners = t1&t2;
imshow(window_name, corners);
char key = (char)waitKey(0);
return 0;
}
- Thank you.