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

[Sheet3] Finished exercises 1 through 3

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
2017-04-25 15:56:41 +02:00
parent cc1084ec77
commit 376aa0997c
5 changed files with 136 additions and 0 deletions

13
ccv/sheet3/CMakeLists.txt Normal file
View File

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

BIN
ccv/sheet3/DSC09506.JPG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 692 KiB

44
ccv/sheet3/exercise1.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <opencv2/opencv.hpp>
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;
}

55
ccv/sheet3/exercise3.cpp Normal file
View File

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

24
ccv/sheet3/main.cpp Normal file
View File

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