Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Никоноров И.Д. ЛР№2 Линейная фильтрация изображений (горизонтальное разбиение). Ядро Гаусса 3x3. OpenMP #152

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
6 changes: 0 additions & 6 deletions 1706-4/README.md

This file was deleted.

31 changes: 31 additions & 0 deletions 1706-4/nikonorov_id/Gauss_OpenMP/Gauss.sln
Original file line number Diff line number Diff line change
@@ -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
151 changes: 151 additions & 0 deletions 1706-4/nikonorov_id/Gauss_OpenMP/Gauss/Gauss.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<ProjectGuid>{C38E8791-06A2-4F85-9235-D8DE1C082B2F}</ProjectGuid>
<RootNamespace>Gauss</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>false</ConformanceMode>
<AdditionalIncludeDirectories>C:\OpenCV\opencv\build\include</AdditionalIncludeDirectories>
<OpenMPSupport>true</OpenMPSupport>
<AdditionalOptions>/Zc:twoPhase %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>C:\OpenCV\opencv\build\x64\vc15\lib</AdditionalLibraryDirectories>
<AdditionalDependencies>opencv_world420.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Source.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
22 changes: 22 additions & 0 deletions 1706-4/nikonorov_id/Gauss_OpenMP/Gauss/Gauss.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Исходные файлы">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Файлы заголовков">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Файлы ресурсов">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Source.cpp">
<Filter>Исходные файлы</Filter>
</ClCompile>
</ItemGroup>
</Project>
176 changes: 176 additions & 0 deletions 1706-4/nikonorov_id/Gauss_OpenMP/Gauss/Source.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#include <iostream>
#include <ctime>
#include <iomanip>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <omp.h>
#include <chrono>

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<uchar>(idX, idY)) * kernel[l + w + 2 * (l + 2)];
}
}
output.at<uchar>(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<uchar>(idX, idY)) * kernel[l + w + 2 * (l + 2)];
}
}
output.at<uchar>(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<std::chrono::milliseconds>(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<std::chrono::milliseconds>(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;
}