| 14 | using namespace af; |
| 15 | |
| 16 | static void harris_demo(bool console) { |
| 17 | af::Window wnd("Harris Corner Detector"); |
| 18 | |
| 19 | // Load image |
| 20 | array img_color; |
| 21 | if (console) |
| 22 | img_color = loadImage(ASSETS_DIR "/examples/images/square.png", true); |
| 23 | else |
| 24 | img_color = loadImage(ASSETS_DIR "/examples/images/man.jpg", true); |
| 25 | // Convert the image from RGB to gray-scale |
| 26 | array img = colorSpace(img_color, AF_GRAY, AF_RGB); |
| 27 | // For visualization in ArrayFire, color images must be in the [0.0f-1.0f] |
| 28 | // interval |
| 29 | img_color /= 255.f; |
| 30 | |
| 31 | // Calculate image gradients |
| 32 | array ix, iy; |
| 33 | grad(ix, iy, img); |
| 34 | |
| 35 | // Compute second-order derivatives |
| 36 | array ixx = ix * ix; |
| 37 | array ixy = ix * iy; |
| 38 | array iyy = iy * iy; |
| 39 | |
| 40 | // Compute a Gaussian kernel with standard deviation of 1.0 and length of 5 |
| 41 | // pixels These values can be changed to use a smaller or larger window |
| 42 | array gauss_filt = gaussianKernel(5, 5, 1.0, 1.0); |
| 43 | |
| 44 | // Filter second-order derivatives with Gaussian kernel computed previously |
| 45 | ixx = convolve(ixx, gauss_filt); |
| 46 | ixy = convolve(ixy, gauss_filt); |
| 47 | iyy = convolve(iyy, gauss_filt); |
| 48 | |
| 49 | // Calculate trace |
| 50 | array itr = ixx + iyy; |
| 51 | // Calculate determinant |
| 52 | array idet = ixx * iyy - ixy * ixy; |
| 53 | |
| 54 | // Calculate Harris response |
| 55 | array response = idet - 0.04f * (itr * itr); |
| 56 | |
| 57 | // Gets maximum response for each 3x3 neighborhood |
| 58 | // array max_resp = maxfilt(response, 3, 3); |
| 59 | array mask = constant(1, 3, 3); |
| 60 | array max_resp = dilate(response, mask); |
| 61 | |
| 62 | // Discard responses that are not greater than threshold |
| 63 | array corners = response > 1e5f; |
| 64 | corners = corners * response; |
| 65 | |
| 66 | // Discard responses that are not equal to maximum neighborhood response, |
| 67 | // scale them to original response value |
| 68 | corners = (corners == max_resp) * corners; |
| 69 | |
| 70 | // Gets host pointer to response data |
| 71 | float* h_corners = corners.host<float>(); |
| 72 | |
| 73 | unsigned good_corners = 0; |
no test coverage detected