Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions task1/convolution.hpp
Original file line number Diff line number Diff line change
@@ -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 <opencv2/opencv.hpp>

cv::Mat convolve(cv::Mat, cv::Mat); // Declaring which function to be accessed while linking the two cpp files

#endif

32 changes: 32 additions & 0 deletions task1/include/convolution.hpp
Original file line number Diff line number Diff line change
@@ -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 <opencv2/opencv.hpp>

cv::Mat convolve(cv::Mat, cv::Mat); // Declaring which function to be accessed while linking the two cpp files

#endif

54 changes: 54 additions & 0 deletions task1/sol1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

#include <iostream>
#include <opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgcodecs.hpp>
#include<opencv2/imgproc.hpp>
#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_<double>(3, 3) <<
1, 2, 1,
0, 0, 0,
-1, -2, -1
);

cv::Mat K2 = (cv::Mat_<double>(3, 3) <<
1, 0, -1,
2, 0, -2,
1, 0, -1
);
cv::Mat gaussian_v1 = (cv::Mat_<double>(3, 1) << 1., 0. , 1.);
cv::Mat gaussian_h1 = (cv::Mat_<double>(1, 3) << 1., 2. ,1.);
cv::Mat gaussian_h2 = (cv::Mat_<double>(1, 3) << 1., 0. , 1.);
cv::Mat gaussian_v2 = (cv::Mat_<double>(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;
}
Binary file added task1/sol2
Binary file not shown.
51 changes: 51 additions & 0 deletions task1/sol2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
#include <opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgcodecs.hpp>
#include<opencv2/imgproc.hpp>


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_<double>(3, 3) <<
1, 2, 1,
0, 0, 0,
-1, -2, -1
);

cv::Mat K2 = (cv::Mat_<double>(3, 3) <<
1, 0, -1,
2, 0, -2,
1, 0, -1
);
cv::Mat gaussian_v1 = (cv::Mat_<double>(3, 1) << 1., 0. , 1.);
cv::Mat gaussian_h1 = (cv::Mat_<double>(1, 3) << 1., 2. ,1.);
cv::Mat gaussian_h2 = (cv::Mat_<double>(1, 3) << 1., 0. , 1.);
cv::Mat gaussian_v2 = (cv::Mat_<double>(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;
}