diff --git a/1706-4/README.md b/1706-4/README.md deleted file mode 100644 index 57f45fa..0000000 --- a/1706-4/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# 1706-4 - - - . - : -`<>_<>`, , `ivanov_ii`. \ No newline at end of file diff --git a/1706-4/nikonorov_id/Gauss_OpenMP/Gauss.sln b/1706-4/nikonorov_id/Gauss_OpenMP/Gauss.sln new file mode 100644 index 0000000..1f9dff8 --- /dev/null +++ b/1706-4/nikonorov_id/Gauss_OpenMP/Gauss.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29519.181 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Gauss", "Gauss\Gauss.vcxproj", "{C38E8791-06A2-4F85-9235-D8DE1C082B2F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C38E8791-06A2-4F85-9235-D8DE1C082B2F}.Debug|x64.ActiveCfg = Debug|x64 + {C38E8791-06A2-4F85-9235-D8DE1C082B2F}.Debug|x64.Build.0 = Debug|x64 + {C38E8791-06A2-4F85-9235-D8DE1C082B2F}.Debug|x86.ActiveCfg = Debug|Win32 + {C38E8791-06A2-4F85-9235-D8DE1C082B2F}.Debug|x86.Build.0 = Debug|Win32 + {C38E8791-06A2-4F85-9235-D8DE1C082B2F}.Release|x64.ActiveCfg = Release|x64 + {C38E8791-06A2-4F85-9235-D8DE1C082B2F}.Release|x64.Build.0 = Release|x64 + {C38E8791-06A2-4F85-9235-D8DE1C082B2F}.Release|x86.ActiveCfg = Release|Win32 + {C38E8791-06A2-4F85-9235-D8DE1C082B2F}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D399271D-CECE-4B46-BA1E-1EBB982D743B} + EndGlobalSection +EndGlobal diff --git a/1706-4/nikonorov_id/Gauss_OpenMP/Gauss/Gauss.vcxproj b/1706-4/nikonorov_id/Gauss_OpenMP/Gauss/Gauss.vcxproj new file mode 100644 index 0000000..f379d48 --- /dev/null +++ b/1706-4/nikonorov_id/Gauss_OpenMP/Gauss/Gauss.vcxproj @@ -0,0 +1,151 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {C38E8791-06A2-4F85-9235-D8DE1C082B2F} + Gauss + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + false + C:\OpenCV\opencv\build\include + true + /Zc:twoPhase %(AdditionalOptions) + + + Console + true + true + true + C:\OpenCV\opencv\build\x64\vc15\lib + opencv_world420.lib;%(AdditionalDependencies) + + + + + + + + + \ No newline at end of file diff --git a/1706-4/nikonorov_id/Gauss_OpenMP/Gauss/Gauss.vcxproj.filters b/1706-4/nikonorov_id/Gauss_OpenMP/Gauss/Gauss.vcxproj.filters new file mode 100644 index 0000000..30fee6f --- /dev/null +++ b/1706-4/nikonorov_id/Gauss_OpenMP/Gauss/Gauss.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + \ No newline at end of file diff --git a/1706-4/nikonorov_id/Gauss_OpenMP/Gauss/Source.cpp b/1706-4/nikonorov_id/Gauss_OpenMP/Gauss/Source.cpp new file mode 100644 index 0000000..6af7ce5 --- /dev/null +++ b/1706-4/nikonorov_id/Gauss_OpenMP/Gauss/Source.cpp @@ -0,0 +1,176 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace cv; +using namespace std; +#define M_PI 3.14159265358979323846 + +int Clamp(int value, int min, int max) +{ + if (value < min) + return min; + + if (value > max) + return max; + + return value; +} + +void deletekernel(double* kernel, int k) { + + delete [] kernel; +} + +//-------------------------------------------------------------// +//-------------------------------------------------------------// +//-------------------------------------------------------------// +//-------------------------------------------------------------// +//-------------------------------------------------------------// + +double* createKernel(int rows, int cols) { + int tmp = rows * cols; + double* mas; + mas = new double[tmp]; + return mas; +} +void InitKern(double* kernel, int k, double sigma) +{ + int radius = (k / 2); + double sum = 0.0; + double tmp = 1 / (2 * M_PI * sigma * sigma); + int step = radius; + + for (int i = -radius; i <= radius; i++) + { + for (int j = -radius; j <= radius; j++) + { + kernel[j + step] = tmp * exp(-(i * i + j * j) / (2 * sigma * sigma)); + sum += kernel[j + step]; + } + step += k; + } + + for (int i = 0; i < k * k; i++) + { + kernel[i] /= sum; + } +} + +void OpenMP_Gauss(const Mat& input, Mat& output, double* kernel, int kern_size, int thread_nums) +{ + int radius = int(kern_size / 2); +#pragma omp parallel for num_threads(thread_nums) + for (int i = 1; i < input.rows; i++) { + for (int j = 1; j < input.cols; j++) { + double tmp = 0.0; + for (int l = -radius; l <= radius; l++) { + for (int w = -radius; w <= radius; w++) { + int idX = Clamp(i + l, 0, input.rows - 1); + int idY = Clamp(j + w, 0, input.cols - 1); + tmp += double(input.at(idX, idY)) * kernel[l + w + 2 * (l + 2)]; + } + } + output.at(i, j) = int(tmp); + + } + } +} + +void Sequential_Gauss(const Mat& input, Mat& output, double* kernel, int kern_size) +{ + int radius = int(kern_size / 2); + for (int i = 1; i < input.rows; i++) { + for (int j = 1; j < input.cols; j++) { + double tmp = 0.0; + for (int l = -radius; l <= radius; l++) { + for (int w = -radius; w <= radius; w++) { + int idX = Clamp(i + l, 0, input.rows - 1); + int idY = Clamp(j + w, 0, input.cols - 1); + tmp += double(input.at(idX, idY)) * kernel[l + w + 2 * (l + 2)]; + } + } + output.at(i, j) = int(tmp); + + } + } +} + +bool check(const Mat& image1, const Mat& image2) +{ + Mat res; + bitwise_xor(image1, image2, res); + if (countNonZero(res) > 0) + return false; + + else + return true; + +} + +int main(int argc, char** argv) { + string orig_image_path; + string result_image_path; + int thread_nums; + if (argc < 4) + { + orig_image_path += "pic.jpg"; + result_image_path += "result.jpg"; + thread_nums = 6; + } + else + { + orig_image_path += argv[1]; + result_image_path += argv[2]; + } + + Mat original_image; + original_image = imread(orig_image_path, IMREAD_COLOR); + + if (original_image.empty()) + { + cout << "No image data" << endl; + return -1; + } + Mat gray_image; + Mat seq_result; + Mat OpenMP_result; + cvtColor(original_image, gray_image, COLOR_BGR2GRAY); + cvtColor(original_image, seq_result, COLOR_BGR2GRAY); + cvtColor(original_image, OpenMP_result, COLOR_BGR2GRAY); + + int k = 3; + + double* kernel; + kernel = createKernel(k, k); + InitKern(kernel, k, 7); + + auto begin = std::chrono::steady_clock::now(); + OpenMP_Gauss(gray_image, seq_result, kernel, k, thread_nums); + auto end = std::chrono::steady_clock::now(); + auto elapsed_ms = std::chrono::duration_cast(end - begin); + cout << "The OpenMP time is: " << elapsed_ms.count() << " ms" << endl; + + auto s_begin = std::chrono::steady_clock::now(); + Sequential_Gauss(gray_image, OpenMP_result, kernel, k); + auto s_end = std::chrono::steady_clock::now(); + auto s_elapsed_ms = std::chrono::duration_cast(s_end - s_begin); + cout << "The sequential time is: " << s_elapsed_ms.count() << " ms" << endl; + + if (check(seq_result, OpenMP_result)) + { + imwrite(result_image_path, OpenMP_result); + namedWindow("my_result", WINDOW_AUTOSIZE); + namedWindow("Gray image", WINDOW_AUTOSIZE); + imshow("my_result", OpenMP_result); + imshow("Gray image", gray_image); + waitKey(0); + } + deletekernel(kernel, k); + return 0; +} \ No newline at end of file