From 8eb48b9b0a8b2e0d19c936a0abb957a9acd085f6 Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Fri, 12 May 2017 16:11:31 +0200 Subject: [PATCH] [CVV] Added orientation pyramid Signed-off-by: Jim Martens --- ccv/sheet5/CMakeLists.txt | 2 +- ccv/sheet5/main.cpp | 26 +++++++++++++-- ccv/sheet5/oriented_pyramid.cpp | 59 +++++++++++++++++++++++++++++++++ ccv/sheet5/oriented_pyramid.h | 43 ++++++++++++++++++++++++ 4 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 ccv/sheet5/oriented_pyramid.cpp create mode 100644 ccv/sheet5/oriented_pyramid.h diff --git a/ccv/sheet5/CMakeLists.txt b/ccv/sheet5/CMakeLists.txt index 27ae729..871ae90 100644 --- a/ccv/sheet5/CMakeLists.txt +++ b/ccv/sheet5/CMakeLists.txt @@ -5,5 +5,5 @@ set(CMAKE_CXX_STANDARD 11) find_package( OpenCV REQUIRED ) -add_executable(sheet5 gauss_pyramid.cpp laplacian_pyramid.cpp main.cpp) +add_executable(sheet5 gauss_pyramid.cpp laplacian_pyramid.cpp oriented_pyramid.cpp main.cpp) target_link_libraries(sheet5 ${OpenCV_LIBS}) diff --git a/ccv/sheet5/main.cpp b/ccv/sheet5/main.cpp index 0ba5489..c73b556 100644 --- a/ccv/sheet5/main.cpp +++ b/ccv/sheet5/main.cpp @@ -1,4 +1,9 @@ #include +#include "gauss_pyramid.h" +#include "laplacian_pyramid.h" +#include "oriented_pyramid.h" + +void exercise1(cv::Mat& image); int main(int argc, char** argv) { if ( argc != 2 ) @@ -7,6 +12,24 @@ int main(int argc, char** argv) { return -1; } + cv::Mat image = cv::imread(argv[1]); + //exercise1(image); + image.convertTo(image, CV_32F); + + gauss_pyramid gauss_pyramid1 = gauss_pyramid(image, 3.0, 5); + laplacian_pyramid laplacian_pyramid1 = laplacian_pyramid(gauss_pyramid1, 2.0); + oriented_pyramid oriented_pyramid1 = oriented_pyramid(laplacian_pyramid1, 8); + oriented_pyramid1.compute_feature_maps(); + for (int i = 0; i < 8; i++) { + cv::namedWindow("feature map" + std::to_string(i), CV_WINDOW_NORMAL); + cv::imshow("feature map" + std::to_string(i), oriented_pyramid1.get_feature_map(i)); + cv::waitKey(0); + } + + return 0; +} + +void exercise1(cv::Mat &image) { std::vector gabor_filters = std::vector(); cv::Size size = cv::Size(20, 20); double wavelength = 3; @@ -35,7 +58,6 @@ int main(int argc, char** argv) { } // read input image - cv::Mat image = cv::imread(argv[1]); image.convertTo(image, CV_32F); for (unsigned long i = 0; i < 8; i++) { @@ -45,6 +67,4 @@ int main(int argc, char** argv) { cv::imshow("filtered_" + std::to_string(i + 1), filtered_image); cv::waitKey(0); } - - return 0; } diff --git a/ccv/sheet5/oriented_pyramid.cpp b/ccv/sheet5/oriented_pyramid.cpp new file mode 100644 index 0000000..33453d7 --- /dev/null +++ b/ccv/sheet5/oriented_pyramid.cpp @@ -0,0 +1,59 @@ +#include "oriented_pyramid.h" + +oriented_pyramid::oriented_pyramid(const laplacian_pyramid &pyramid, int num_orientations) { + _gabor_filters = std::vector(); + _orientation_maps = std::vector>(); + _feature_maps = std::vector(); + + initialize_gabor_filters(num_orientations); + unsigned long number_of_layers = pyramid.get_number_of_layers(); + for (unsigned long i = 0; i < num_orientations; i++) { + std::vector orientation_vector = std::vector(); + for (int layer = 0; layer < number_of_layers; layer++) { + cv::Mat filtered_image; + cv::filter2D(pyramid.get(layer), filtered_image, -1, _gabor_filters.at(i), cv::Point(-1, -1), 0, cv::BORDER_CONSTANT); + orientation_vector.push_back(filtered_image.clone()); + } + _orientation_maps.push_back(orientation_vector); + } + +} + +void oriented_pyramid::initialize_gabor_filters(float num_orientations) { + cv::Size size = cv::Size(20, 20); + double wavelength = 3; + double standard_deviation = 18; + + double start_level = num_orientations / 2.0; + for (double level = start_level; level >= 0; level--) { + _gabor_filters.push_back(cv::getGaborKernel(size, standard_deviation, (level/num_orientations) * CV_PI, wavelength, + 1, 0, CV_32F)); + } + + double end_level = start_level; + start_level = num_orientations - 1; + + for (double level = start_level; level > end_level; level--) { + _gabor_filters.push_back(cv::getGaborKernel(size, standard_deviation, (level/num_orientations) * CV_PI, wavelength, + 1, 0, CV_32F)); + } +} + +void oriented_pyramid::compute_feature_maps() { + unsigned long num_orientations = _orientation_maps.size(); + for (unsigned long i = 0; i < num_orientations; i++) { + cv::Mat feature_map = (cv::Mat &&) _orientation_maps.at(i).front(); + cv::Size original_size = feature_map.size(); + unsigned long num_layers = _orientation_maps.at(i).size(); + for (unsigned long layer = 1; layer < num_layers; layer++) { + cv::Mat resized_image; + cv::resize(_orientation_maps.at(i).at(layer), resized_image, original_size, 0, 0, cv::INTER_CUBIC); + feature_map += resized_image; + } + _feature_maps.push_back(feature_map.clone()); + } +} + +cv::Mat oriented_pyramid::get_feature_map(int orientation) { + return _feature_maps.at((unsigned long) orientation); +} diff --git a/ccv/sheet5/oriented_pyramid.h b/ccv/sheet5/oriented_pyramid.h new file mode 100644 index 0000000..e94f25f --- /dev/null +++ b/ccv/sheet5/oriented_pyramid.h @@ -0,0 +1,43 @@ +#ifndef SHEET5_ORIENTED_PYRAMID_H +#define SHEET5_ORIENTED_PYRAMID_H + +#include +#include "laplacian_pyramid.h" + +class oriented_pyramid { +private: + std::vector> _orientation_maps; + std::vector _gabor_filters; + std::vector _feature_maps; + + /** + * Initializes the Gabor filters. + * @param num_orientations the number of orientations to use + */ + void initialize_gabor_filters(float num_orientations); +public: + /** + * Initializes the oriented pyramid. + * @param pyramid the laplacian pyramid + * @param num_orientations the number of Gabor filters to apply + */ + oriented_pyramid(const laplacian_pyramid& pyramid, int num_orientations); + + /** + * Computes the feature maps. + */ + void compute_feature_maps(); + + /** + * Returns the feature map for the nth orientation. + * + * compute_feature_maps must be called first. + * + * @param orientation the nth orientation + * @return the feature map + */ + cv::Mat get_feature_map(int orientation); +}; + + +#endif //SHEET5_ORIENTED_PYRAMID_H