-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_sift_show_pyr.cpp
More file actions
122 lines (103 loc) · 4.47 KB
/
Copy pathtest_sift_show_pyr.cpp
File metadata and controls
122 lines (103 loc) · 4.47 KB
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
#include "test_utils.h"
#include <vulkansift/vulkansift.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>
int main(int argc, char *argv[])
{
if (argc != 2)
{
std::cout << "Invalid command." << std::endl;
std::cout << "Usage: ./test_sift_show_pyr PATH_TO_IMAGE" << std::endl;
return -1;
}
// Read image with OpenCV
cv::Mat image = cv::imread(argv[1], 0);
if (image.empty())
{
std::cout << "Failed to read image " << argv[1] << ". Stopping program." << std::endl;
return -1;
}
vksift_setLogLevel(VKSIFT_LOG_INFO);
if (vksift_loadVulkan() != VKSIFT_SUCCESS)
{
std::cout << "Impossible to initialize the Vulkan API" << std::endl;
return -1;
}
vksift_Config config = vksift_getDefaultConfig();
config.input_image_max_size = image.cols * image.rows;
vksift_Instance vksift_instance = NULL;
if (vksift_createInstance(&vksift_instance, &config) != VKSIFT_SUCCESS)
{
std::cout << "Impossible to create the vksift_instance" << std::endl;
vksift_unloadVulkan();
return -1;
}
// Run feature detection in which the pyramid will be created to extract keypoints
vksift_detectFeatures(vksift_instance, image.data, image.cols, image.rows, 0u);
uint32_t oct_idx = 0;
uint32_t scale_idx = 0;
uint32_t nb_octaves = vksift_getScaleSpaceNbOctaves(vksift_instance);
uint32_t nb_scales = config.nb_scales_per_octave;
int user_key = 0;
cv::namedWindow("Pyramid viewer", cv::WINDOW_NORMAL);
while (user_key != 'x')
{
uint32_t width, height;
vksift_getScaleSpaceOctaveResolution(vksift_instance, oct_idx, &width, &height);
// Get blurred image
cv::Mat blurred_image;
blurred_image.create(height, width, CV_32F);
vksift_downloadScaleSpaceImage(vksift_instance, oct_idx, scale_idx, (float *)blurred_image.data);
cv::cvtColor(blurred_image, blurred_image, cv::COLOR_GRAY2BGR);
cv::resize(blurred_image, blurred_image, image.size());
// Get next scale blurred image
cv::Mat next_blurred_image;
next_blurred_image.create(height, width, CV_32F);
vksift_downloadScaleSpaceImage(vksift_instance, oct_idx, scale_idx + 1, (float *)next_blurred_image.data);
cv::cvtColor(next_blurred_image, next_blurred_image, cv::COLOR_GRAY2BGR);
cv::resize(next_blurred_image, next_blurred_image, image.size());
// Get DoG image corresponding to the two blurred image substraction
cv::Mat dog_image;
dog_image.create(height, width, CV_32F);
vksift_downloadDoGImage(vksift_instance, oct_idx, scale_idx, (float *)dog_image.data);
cv::Mat color_dog_image = getColormappedDoGImage(dog_image);
cv::resize(color_dog_image, color_dog_image, image.size());
std::array<cv::Mat, 3> image_arr = {blurred_image, next_blurred_image, color_dog_image};
cv::Mat final_image;
cv::hconcat(image_arr, final_image);
// Draw info
cv::putText(final_image, "w/s: change octave", cv::Size{10, final_image.rows - 60}, cv::FONT_HERSHEY_COMPLEX, 0.5f, cv::Scalar(0, 0, 1));
cv::putText(final_image, "d/a: change scale", cv::Size{10, final_image.rows - 40}, cv::FONT_HERSHEY_COMPLEX, 0.5f, cv::Scalar(0, 0, 1));
cv::putText(final_image, "x: exit", cv::Size{10, final_image.rows - 20}, cv::FONT_HERSHEY_COMPLEX, 0.5f, cv::Scalar(0, 1, 0));
std::string blurred_str{"Octave " + std::to_string(oct_idx) + " Scale " + std::to_string(scale_idx)};
std::string next_blurred_str{"Scale " + std::to_string(scale_idx + 1)};
std::string dog_str{"DoG"};
cv::putText(final_image, blurred_str, cv::Size{10, 20}, cv::FONT_HERSHEY_COMPLEX, 0.5f, cv::Scalar(0, 0, 1));
cv::putText(final_image, next_blurred_str, cv::Size{final_image.cols / 3 + 10, 20}, cv::FONT_HERSHEY_COMPLEX, 0.5f, cv::Scalar(0, 0, 1));
cv::putText(final_image, dog_str, cv::Size{final_image.cols / 3 * 2 + 10, 20}, cv::FONT_HERSHEY_COMPLEX, 0.5f, cv::Scalar(0, 0, 1));
cv::imshow("Pyramid viewer", final_image);
user_key = cv::waitKey(0);
// Handle user input
switch (user_key)
{
case 'w': // up key
oct_idx = (oct_idx != nb_octaves - 1) ? oct_idx + 1 : oct_idx;
break;
case 's': // down key
oct_idx = (oct_idx != 0) ? oct_idx - 1 : oct_idx;
break;
case 'd': // right key
scale_idx = (scale_idx != (nb_scales + 1)) ? scale_idx + 1 : scale_idx;
break;
case 'a': // left key
scale_idx = (scale_idx != 0) ? scale_idx - 1 : scale_idx;
break;
default:
break;
}
}
vksift_destroyInstance(&vksift_instance);
vksift_unloadVulkan();
return 0;
}