mirror of
https://github.com/2martens/uni.git
synced 2026-05-06 11:26:25 +02:00
[CVV] Added orientation pyramid
Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
@ -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})
|
||||
|
||||
@ -1,4 +1,9 @@
|
||||
#include <opencv2/opencv.hpp>
|
||||
#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<cv::Mat> gabor_filters = std::vector<cv::Mat>();
|
||||
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;
|
||||
}
|
||||
|
||||
59
ccv/sheet5/oriented_pyramid.cpp
Normal file
59
ccv/sheet5/oriented_pyramid.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
#include "oriented_pyramid.h"
|
||||
|
||||
oriented_pyramid::oriented_pyramid(const laplacian_pyramid &pyramid, int num_orientations) {
|
||||
_gabor_filters = std::vector<cv::Mat>();
|
||||
_orientation_maps = std::vector<std::vector<cv::Mat>>();
|
||||
_feature_maps = std::vector<cv::Mat>();
|
||||
|
||||
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<cv::Mat> orientation_vector = std::vector<cv::Mat>();
|
||||
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);
|
||||
}
|
||||
43
ccv/sheet5/oriented_pyramid.h
Normal file
43
ccv/sheet5/oriented_pyramid.h
Normal file
@ -0,0 +1,43 @@
|
||||
#ifndef SHEET5_ORIENTED_PYRAMID_H
|
||||
#define SHEET5_ORIENTED_PYRAMID_H
|
||||
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include "laplacian_pyramid.h"
|
||||
|
||||
class oriented_pyramid {
|
||||
private:
|
||||
std::vector<std::vector<cv::Mat>> _orientation_maps;
|
||||
std::vector<cv::Mat> _gabor_filters;
|
||||
std::vector<cv::Mat> _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
|
||||
Reference in New Issue
Block a user