diff --git a/ccv/sheet3/CMakeLists.txt b/ccv/sheet3/CMakeLists.txt new file mode 100644 index 0000000..9d9ebe8 --- /dev/null +++ b/ccv/sheet3/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.5) +project(sheet3) + +set(CMAKE_CXX_STANDARD 11) + +find_package( OpenCV REQUIRED ) + +add_executable(sheet3 main.cpp) +add_executable(sheet3-exercise1 exercise1.cpp) +add_executable(sheet3-exercise3 exercise3.cpp) +target_link_libraries(sheet3 ${OpenCV_LIBS}) +target_link_libraries(sheet3-exercise1 ${OpenCV_LIBS}) +target_link_libraries(sheet3-exercise3 ${OpenCV_LIBS}) diff --git a/ccv/sheet3/DSC09506.JPG b/ccv/sheet3/DSC09506.JPG new file mode 100644 index 0000000..4dab65e Binary files /dev/null and b/ccv/sheet3/DSC09506.JPG differ diff --git a/ccv/sheet3/exercise1.cpp b/ccv/sheet3/exercise1.cpp new file mode 100644 index 0000000..ef91093 --- /dev/null +++ b/ccv/sheet3/exercise1.cpp @@ -0,0 +1,44 @@ +#include + +int main(int argc, char** argv ) { +// exercise 1 + +// matrix M + float m[] = {1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 1.0}; + cv::Mat M(3, 3, CV_32FC1, m); +// gaussian filter kernel + float g[] = {1, 2, 1, 2, 4, 2, 1, 2, 1}; + cv::Mat G(3, 3, CV_32FC1, g); + G = G * 0.0625; +// output G + std::cout << G << std::endl << std::endl; + +// convolve 1. + cv::Mat M_1; + cv::filter2D(M, M_1, -1, G, cv::Point(-1, -1), 0, cv::BORDER_CONSTANT); +// output M_1 + std::cout << M_1 << std::endl << std::endl; + +// convolve 2. + float g_1[] = {1, 2, 1}; + cv::Mat G_1(1, 3, CV_32FC1, g_1); + G_1 = G_1 * 0.25; + float g_2[] = {1, 2, 1}; + cv::Mat G_2(3, 1, CV_32FC1, g_2); + G_2 = G_2 * 0.25; +// output G_1, G_2 + std::cout << G_1 << std::endl << std::endl; + std::cout << G_2 << std::endl << std::endl; + + cv::Mat M_2; + cv::Mat M_3; + cv::filter2D(M, M_2, -1, G_1, cv::Point(-1, -1), 0, cv::BORDER_CONSTANT); + cv::filter2D(M_2, M_3, -1, G_2, cv::Point(-1, -1), 0, cv::BORDER_CONSTANT); +// output M_3 + std::cout << M_3 << std::endl << std::endl; + +// convolve 3. + cv::Mat M_4; + cv::GaussianBlur(M, M_4, cv::Size(3, 3), 0, 0, cv::BORDER_CONSTANT); + std::cout << M_4 << std::endl << std::endl; +} diff --git a/ccv/sheet3/exercise3.cpp b/ccv/sheet3/exercise3.cpp new file mode 100644 index 0000000..70bd9ca --- /dev/null +++ b/ccv/sheet3/exercise3.cpp @@ -0,0 +1,55 @@ +#include + +int main(int argc, char** argv ) { + if ( argc != 2 ) + { + printf("usage: \n"); + return -1; + } + + cv::Mat image; + image = cv::imread(argv[1], cv::ImreadModes::IMREAD_COLOR); + cv::Mat image_gray; + cv::cvtColor(image, image_gray, cv::COLOR_BGR2GRAY); + cv::Mat image_float; + image_gray.convertTo(image_float, CV_32F); + + cv::Mat G_1; + cv::Mat G_2; + cv::GaussianBlur(image_float, G_1, cv::Size(3, 3), 3, 3, cv::BORDER_CONSTANT); + cv::GaussianBlur(image_float, G_2, cv::Size(3, 3), 11, 11, cv::BORDER_CONSTANT); + cv::Mat image_filtered = G_1 - G_2; + + cv::imshow("Filtered (3 - 11)", image_filtered); + cv::waitKey(0); + + cv::GaussianBlur(image_float, G_1, cv::Size(3, 3), 3, 3, cv::BORDER_CONSTANT); + cv::GaussianBlur(image_float, G_2, cv::Size(3, 3), 7, 7, cv::BORDER_CONSTANT); + image_filtered = G_1 - G_2; + + cv::imshow("Filtered (3 - 7)", image_filtered); + cv::waitKey(0); + + cv::GaussianBlur(image_float, G_1, cv::Size(3, 3), 5, 5, cv::BORDER_CONSTANT); + cv::GaussianBlur(image_float, G_2, cv::Size(3, 3), 7, 7, cv::BORDER_CONSTANT); + image_filtered = G_1 - G_2; + + cv::imshow("Filtered (5 - 7)", image_filtered); + cv::waitKey(0); + + cv::GaussianBlur(image_float, G_1, cv::Size(3, 3), 11, 11, cv::BORDER_CONSTANT); + cv::GaussianBlur(image_float, G_2, cv::Size(3, 3), 3, 3, cv::BORDER_CONSTANT); + image_filtered = G_1 - G_2; + image_float.convertTo(image_gray, CV_8U); + + cv::imshow("Filtered (11 - 3)", image_filtered); + cv::waitKey(0); + + cv::GaussianBlur(image_float, G_1, cv::Size(3, 3), 9, 9, cv::BORDER_CONSTANT); + cv::GaussianBlur(image_float, G_2, cv::Size(3, 3), 5, 5, cv::BORDER_CONSTANT); + image_filtered = G_1 - G_2; + + cv::imshow("Filtered (9 - 5)", image_filtered); + cv::waitKey(0); +} + diff --git a/ccv/sheet3/main.cpp b/ccv/sheet3/main.cpp new file mode 100644 index 0000000..7b53758 --- /dev/null +++ b/ccv/sheet3/main.cpp @@ -0,0 +1,24 @@ +#include + +int main(int argc, char** argv ) { + if ( argc != 2 ) + { + printf("usage: main.out \n"); + return -1; + } + + // exercise 2 + cv::Mat image; + image = cv::imread(argv[1], cv::ImreadModes::IMREAD_COLOR); + int kmax = 50; + cv::Mat Matrix_convoluted; + for (int k = 3; k < kmax; k+=2) { + cv::GaussianBlur(image, Matrix_convoluted, cv::Size(k, k), 0, 0, cv::BORDER_CONSTANT); + std::string imageName = "Kernel ("; + imageName = imageName + std::to_string(k) + ", " + std::to_string(k) + ")"; + cv::imshow(imageName, Matrix_convoluted); + cv::waitKey(500); + } + + return 0; +}