1
0
mirror of https://github.com/2martens/uni.git synced 2026-05-06 19:36:26 +02:00

Finished exercise 4 on sheet3

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
2017-04-28 15:19:10 +02:00
parent 376aa0997c
commit 65e01e1008
6 changed files with 145 additions and 0 deletions

View File

@ -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})

35
ccv/sheet3/exercise4.cpp Normal file
View File

@ -0,0 +1,35 @@
#include "lab_pyramid.h"
int main(int argc, char** argv ) {
if ( argc != 2 )
{
printf("usage: <Image_Path>\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;
}

View File

@ -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);
}

View File

@ -0,0 +1,17 @@
#ifndef SHEET3_GAUSS_PYRAMID_H
#define SHEET3_GAUSS_PYRAMID_H
#include <opencv2/opencv.hpp>
class gauss_pyramid
{
private:
std::vector<cv::Mat> _layers;
public:
gauss_pyramid();
gauss_pyramid(cv::Mat img, int number_of_layers);
cv::Mat get(int layer);
};
#endif //SHEET3_GAUSS_PYRAMID_H

View File

@ -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" );
}
}

41
ccv/sheet3/lab_pyramid.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef SHEET3_LAB_PYRAMID_H
#define SHEET3_LAB_PYRAMID_H
#include <opencv2/opencv.hpp>
#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