diff --git a/task1/convolution.hpp b/task1/convolution.hpp new file mode 100644 index 0000000..0b506e5 --- /dev/null +++ b/task1/convolution.hpp @@ -0,0 +1,32 @@ +/* +MIT License + +Copyright (c) 2023 Society of Robotics and Automation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +#ifndef CONVOLUTION_HPP +#define CONVOLUTION_HPP + +#include + +cv::Mat convolve(cv::Mat, cv::Mat); // Declaring which function to be accessed while linking the two cpp files + +#endif + diff --git a/task1/include/convolution.hpp b/task1/include/convolution.hpp new file mode 100644 index 0000000..0b506e5 --- /dev/null +++ b/task1/include/convolution.hpp @@ -0,0 +1,32 @@ +/* +MIT License + +Copyright (c) 2023 Society of Robotics and Automation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +#ifndef CONVOLUTION_HPP +#define CONVOLUTION_HPP + +#include + +cv::Mat convolve(cv::Mat, cv::Mat); // Declaring which function to be accessed while linking the two cpp files + +#endif + diff --git a/task1/sol1.cpp b/task1/sol1.cpp new file mode 100644 index 0000000..c7f1714 --- /dev/null +++ b/task1/sol1.cpp @@ -0,0 +1,54 @@ + +#include +#include +#include +#include +#include +#include "convolution.hpp" + + +using namespace std; +cv::Mat convolve(cv::Mat, cv::Mat); +int main() +{ + cv::Mat img = cv::imread("./assets/hogwarts.png", cv::IMREAD_GRAYSCALE); + if (img.empty()) { + cerr << "Could not load image at ./assets/hogwarts.png\n"; + return -1; + } + cv::resize(img, img, cv::Size(img.cols*0.5, img.rows*0.5)); + + // The two kernels — do not change these + cv::Mat K1 = (cv::Mat_(3, 3) << + 1, 2, 1, + 0, 0, 0, + -1, -2, -1 + ); + + cv::Mat K2 = (cv::Mat_(3, 3) << + 1, 0, -1, + 2, 0, -2, + 1, 0, -1 + ); + cv::Mat gaussian_v1 = (cv::Mat_(3, 1) << 1., 0. , 1.); + cv::Mat gaussian_h1 = (cv::Mat_(1, 3) << 1., 2. ,1.); + cv::Mat gaussian_h2 = (cv::Mat_(1, 3) << 1., 0. , 1.); + cv::Mat gaussian_v2 = (cv::Mat_(3, 1) << 1., 2. ,1.); + + cv::Mat intermediate1 = convolve(img, gaussian_v1); // Convolve Vertically + cv::Mat output1 = convolve(intermediate1, gaussian_h1); + cv::Mat intermediate2 = convolve(output1, gaussian_v2); + cv::Mat img_f; // Convolve Vertically + img_f= convolve(intermediate2, gaussian_h2); + + + img.convertTo(img_f, CV_64FC1); + + cv::namedWindow("Output by Naive Seperable Convolution", cv::WINDOW_NORMAL); + cv::imshow("Output by Naive Seperable Convolution",img_f); + cv::waitKey(0); + // TODO: apply K1 and K2 to img_f with as few multiplications per pixel as possible. + // Print the number of multiplications your approach uses per pixel. + + return 0; +} \ No newline at end of file diff --git a/task1/sol2 b/task1/sol2 new file mode 100755 index 0000000..c63f624 Binary files /dev/null and b/task1/sol2 differ diff --git a/task1/sol2.cpp b/task1/sol2.cpp new file mode 100644 index 0000000..1a1c09b --- /dev/null +++ b/task1/sol2.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +int main() +{ + cv::Mat img = cv::imread("./assets/hogwarts.png", cv::IMREAD_GRAYSCALE); + if (img.empty()) { + cerr << "Could not load image at ./assets/hogwarts.png\n"; + return -1; + } + cv::resize(img, img, cv::Size(img.cols*0.5, img.rows*0.5)); + + // The two kernels — do not change these + cv::Mat K1 = (cv::Mat_(3, 3) << + 1, 2, 1, + 0, 0, 0, + -1, -2, -1 + ); + + cv::Mat K2 = (cv::Mat_(3, 3) << + 1, 0, -1, + 2, 0, -2, + 1, 0, -1 + ); + cv::Mat gaussian_v1 = (cv::Mat_(3, 1) << 1., 0. , 1.); + cv::Mat gaussian_h1 = (cv::Mat_(1, 3) << 1., 2. ,1.); + cv::Mat gaussian_h2 = (cv::Mat_(1, 3) << 1., 0. , 1.); + cv::Mat gaussian_v2 = (cv::Mat_(3, 1) << 1., 2. ,1.); + cv::Mat inter1; + cv::Mat output1; + cv::Mat inter2; + cv::Mat intermediate1 + + + + // using the built-in function + cv::filter2D(img, inter1, -1,gaussian_v1 , cv::Point(-1, -1), 0.0, cv::BorderTypes::BORDER_REPLICATE); + cv::filter2D(inter1, output1, -1,gaussian_h1 , cv::Point(-1, -1), 0.0, cv::BorderTypes::BORDER_REPLICATE); + cv::filter2D(output1, inter2, -1,gaussian_v2 , cv::Point(-1, -1), 0.0, cv::BorderTypes::BORDER_REPLICATE); + cv::filter2D(img, inter2, -1,gaussian_h2 , cv::Point(-1, -1), 0.0, cv::BorderTypes::BORDER_REPLICATE); + img.convertTo(img_f, CV_64FC1); + cv::imshow("filter 2D output", img_f); + cv::waitKey(0); + return 0; +} \ No newline at end of file