diff --git a/ccv/sheet3/CMakeLists.txt b/ccv/sheet3/CMakeLists.txt index 9d9ebe8..1f9690b 100644 --- a/ccv/sheet3/CMakeLists.txt +++ b/ccv/sheet3/CMakeLists.txt @@ -8,6 +8,8 @@ find_package( OpenCV REQUIRED ) add_executable(sheet3 main.cpp) add_executable(sheet3-exercise1 exercise1.cpp) add_executable(sheet3-exercise3 exercise3.cpp) +add_executable(sheet3-exercise4 gauss_pyramid.cpp lab_pyramid.cpp exercise4.cpp) target_link_libraries(sheet3 ${OpenCV_LIBS}) target_link_libraries(sheet3-exercise1 ${OpenCV_LIBS}) target_link_libraries(sheet3-exercise3 ${OpenCV_LIBS}) +target_link_libraries(sheet3-exercise4 ${OpenCV_LIBS}) diff --git a/ccv/sheet3/exercise4.cpp b/ccv/sheet3/exercise4.cpp new file mode 100644 index 0000000..5c3ae9c --- /dev/null +++ b/ccv/sheet3/exercise4.cpp @@ -0,0 +1,35 @@ +#include "lab_pyramid.h" + +int main(int argc, char** argv ) { + if ( argc != 2 ) + { + printf("usage: \n"); + return -1; + } + + lab_pyramid pyramid = lab_pyramid(argv[1]); + const int number_of_layers = 5; + pyramid.create_pyramids(number_of_layers); + gauss_pyramid l_pyr = pyramid.get_pyramid(lab_pyramid::COLOR_L); + gauss_pyramid a_pyr = pyramid.get_pyramid(lab_pyramid::COLOR_A); + gauss_pyramid b_pyr = pyramid.get_pyramid(lab_pyramid::COLOR_B); + + for (int i = 0; i < number_of_layers; i++) + { + cv::imshow("Channel: L, Layer:" + std::to_string(i), l_pyr.get(i)); + cv::waitKey(0); + } + + for (int i = 0; i < number_of_layers; i++) + { + cv::imshow("Channel: A, Layer:" + std::to_string(i), a_pyr.get(i)); + cv::waitKey(0); + } + + for (int i = 0; i < number_of_layers; i++) + { + cv::imshow("Channel: B, Layer:" + std::to_string(i), b_pyr.get(i)); + cv::waitKey(0); + } + return 0; +} diff --git a/ccv/sheet3/gauss_pyramid.cpp b/ccv/sheet3/gauss_pyramid.cpp new file mode 100644 index 0000000..b433ef6 --- /dev/null +++ b/ccv/sheet3/gauss_pyramid.cpp @@ -0,0 +1,21 @@ +#include "gauss_pyramid.h" + +gauss_pyramid::gauss_pyramid() {} + +gauss_pyramid::gauss_pyramid(cv::Mat img, int number_of_layers) +{ + _layers.push_back(img); + cv::Mat blurredImage; + cv::Mat resizedImage = img; + for (int i = 1; i < number_of_layers; i++) + { + cv::GaussianBlur(resizedImage, blurredImage, cv::Size(5, 5), 0, 0, cv::BORDER_CONSTANT); + cv::resize(blurredImage, resizedImage, cv::Size(), 0.5, 0.5, cv::INTER_NEAREST); + _layers.push_back(resizedImage); + } +} + +cv::Mat gauss_pyramid::get(int layer) +{ + return _layers.at((unsigned long) layer); +} diff --git a/ccv/sheet3/gauss_pyramid.h b/ccv/sheet3/gauss_pyramid.h new file mode 100644 index 0000000..5be6a18 --- /dev/null +++ b/ccv/sheet3/gauss_pyramid.h @@ -0,0 +1,17 @@ +#ifndef SHEET3_GAUSS_PYRAMID_H +#define SHEET3_GAUSS_PYRAMID_H + +#include + +class gauss_pyramid +{ +private: + std::vector _layers; +public: + gauss_pyramid(); + gauss_pyramid(cv::Mat img, int number_of_layers); + cv::Mat get(int layer); +}; + + +#endif //SHEET3_GAUSS_PYRAMID_H diff --git a/ccv/sheet3/lab_pyramid.cpp b/ccv/sheet3/lab_pyramid.cpp new file mode 100644 index 0000000..19a31b6 --- /dev/null +++ b/ccv/sheet3/lab_pyramid.cpp @@ -0,0 +1,29 @@ +#include "lab_pyramid.h" + +lab_pyramid::lab_pyramid(cv::String image_filename) { + cv::Mat image_rgb = cv::imread(image_filename, cv::IMREAD_COLOR); + cv::cvtColor(image_rgb, _inputImage_lab, cv::COLOR_RGB2Lab); + cv::split(_inputImage_lab ,_imageChannels); +}; + +void lab_pyramid::create_pyramids(int number_of_layers) +{ + _pyramids[COLOR_L] = gauss_pyramid(_imageChannels[COLOR_L], number_of_layers); + _pyramids[COLOR_A] = gauss_pyramid(_imageChannels[COLOR_A], number_of_layers); + _pyramids[COLOR_B] = gauss_pyramid(_imageChannels[COLOR_B], number_of_layers); +} + +gauss_pyramid lab_pyramid::get_pyramid(int channel) +{ + switch (channel) + { + case COLOR_L: + return _pyramids[COLOR_L]; + case COLOR_A: + return _pyramids[COLOR_A]; + case COLOR_B: + return _pyramids[COLOR_B]; + default: + throw std::invalid_argument( "received invalid channel value, use COLOR_L, COLOR_A or COLOR_B" ); + } +} diff --git a/ccv/sheet3/lab_pyramid.h b/ccv/sheet3/lab_pyramid.h new file mode 100644 index 0000000..fe4b74b --- /dev/null +++ b/ccv/sheet3/lab_pyramid.h @@ -0,0 +1,41 @@ +#ifndef SHEET3_LAB_PYRAMID_H +#define SHEET3_LAB_PYRAMID_H + +#include +#include "gauss_pyramid.h" + +class lab_pyramid { +private: + cv::Mat _inputImage_lab; + cv::Mat _imageChannels[3]; + gauss_pyramid _pyramids[3]; +public: + const static int COLOR_L = 0; + const static int COLOR_A = 1; + const static int COLOR_B = 2; + + /** + * Initializes a LAB pyramid. + * + * @param image_filename the filename of the image that should be used + */ + lab_pyramid(cv::String image_filename); + + /** + * Creates the gaussian pyramids for all channels with the given number of layers each. + * + * @param number_of_layers number of layers for gaussian pyramid + */ + void create_pyramids(int number_of_layers); + + /** + * Before this method can be called, pyramids have to be created via create_pyramids. + * + * @param channel the channel you want to get (COLOR_L, COLOR_A or COLOR_B) + * @return the gaussian_pyramid for the given channel + */ + gauss_pyramid get_pyramid(int channel); +}; + + +#endif //SHEET3_LAB_PYRAMID_H