Skip to content

Commit 295dde6

Browse files
committedFeb 27, 2018
Implement image processing filters
1 parent 7144988 commit 295dde6

20 files changed

+2452
-0
lines changed
 

‎im-proc-filters.sln

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26430.6
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "im-proc-filters", "im-proc-filters.vcxproj", "{DCB70548-9C18-4D66-B4F4-9710F0ABCB20}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{DCB70548-9C18-4D66-B4F4-9710F0ABCB20}.Debug|x64.ActiveCfg = Debug|x64
17+
{DCB70548-9C18-4D66-B4F4-9710F0ABCB20}.Debug|x64.Build.0 = Debug|x64
18+
{DCB70548-9C18-4D66-B4F4-9710F0ABCB20}.Debug|x86.ActiveCfg = Debug|Win32
19+
{DCB70548-9C18-4D66-B4F4-9710F0ABCB20}.Debug|x86.Build.0 = Debug|Win32
20+
{DCB70548-9C18-4D66-B4F4-9710F0ABCB20}.Release|x64.ActiveCfg = Release|x64
21+
{DCB70548-9C18-4D66-B4F4-9710F0ABCB20}.Release|x64.Build.0 = Release|x64
22+
{DCB70548-9C18-4D66-B4F4-9710F0ABCB20}.Release|x86.ActiveCfg = Release|Win32
23+
{DCB70548-9C18-4D66-B4F4-9710F0ABCB20}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
EndGlobal

‎im-proc-filters.vcxproj

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<ItemGroup>
22+
<ClInclude Include="inc\bilateral.h" />
23+
<ClInclude Include="inc\canny.h" />
24+
<ClInclude Include="inc\gabor.h" />
25+
<ClInclude Include="inc\gauss.h" />
26+
<ClInclude Include="inc\gradient.h" />
27+
<ClInclude Include="inc\imageformats.h" />
28+
<ClInclude Include="inc\imageio.h" />
29+
<ClInclude Include="inc\median.h" />
30+
<ClInclude Include="inc\mirror.h" />
31+
<ClInclude Include="inc\mse_psnr.h" />
32+
<ClInclude Include="inc\pixelformats.h" />
33+
<ClInclude Include="inc\rotate.h" />
34+
<ClInclude Include="inc\sobel.h" />
35+
<ClInclude Include="inc\ssim.h" />
36+
<ClInclude Include="inc\vessels.h" />
37+
</ItemGroup>
38+
<ItemGroup>
39+
<ClCompile Include="src\imageio.cpp" />
40+
<ClCompile Include="src\main.cpp" />
41+
</ItemGroup>
42+
<PropertyGroup Label="Globals">
43+
<VCProjectVersion>15.0</VCProjectVersion>
44+
<ProjectGuid>{DCB70548-9C18-4D66-B4F4-9710F0ABCB20}</ProjectGuid>
45+
<RootNamespace>improcfilters</RootNamespace>
46+
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
47+
</PropertyGroup>
48+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
49+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
50+
<ConfigurationType>Application</ConfigurationType>
51+
<UseDebugLibraries>true</UseDebugLibraries>
52+
<PlatformToolset>v141</PlatformToolset>
53+
<CharacterSet>MultiByte</CharacterSet>
54+
</PropertyGroup>
55+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
56+
<ConfigurationType>Application</ConfigurationType>
57+
<UseDebugLibraries>false</UseDebugLibraries>
58+
<PlatformToolset>v141</PlatformToolset>
59+
<WholeProgramOptimization>true</WholeProgramOptimization>
60+
<CharacterSet>MultiByte</CharacterSet>
61+
</PropertyGroup>
62+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
63+
<ConfigurationType>Application</ConfigurationType>
64+
<UseDebugLibraries>true</UseDebugLibraries>
65+
<PlatformToolset>v141</PlatformToolset>
66+
<CharacterSet>MultiByte</CharacterSet>
67+
</PropertyGroup>
68+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
69+
<ConfigurationType>Application</ConfigurationType>
70+
<UseDebugLibraries>false</UseDebugLibraries>
71+
<PlatformToolset>v141</PlatformToolset>
72+
<WholeProgramOptimization>true</WholeProgramOptimization>
73+
<CharacterSet>MultiByte</CharacterSet>
74+
</PropertyGroup>
75+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
76+
<ImportGroup Label="ExtensionSettings">
77+
</ImportGroup>
78+
<ImportGroup Label="Shared">
79+
</ImportGroup>
80+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
81+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
82+
</ImportGroup>
83+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
84+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
85+
</ImportGroup>
86+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
87+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
88+
</ImportGroup>
89+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
90+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
91+
</ImportGroup>
92+
<PropertyGroup Label="UserMacros" />
93+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
94+
<IncludePath>C:\Users\rkazants\Desktop\MSU\winter\smoi\image-processing-filters\im-proc-filters\inc;$(IncludePath)</IncludePath>
95+
</PropertyGroup>
96+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
97+
<IncludePath>C:\Users\rkazants\Desktop\MSU\winter\smoi\image-processing-filters\im-proc-filters\inc;$(IncludePath)</IncludePath>
98+
</PropertyGroup>
99+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
100+
<IncludePath>C:\Users\rkazants\Desktop\MSU\winter\smoi\image-processing-filters\im-proc-filters\inc;$(IncludePath)</IncludePath>
101+
</PropertyGroup>
102+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
103+
<IncludePath>C:\Users\rkazants\Desktop\MSU\winter\smoi\image-processing-filters\im-proc-filters\inc;$(IncludePath)</IncludePath>
104+
</PropertyGroup>
105+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
106+
<ClCompile>
107+
<WarningLevel>Level3</WarningLevel>
108+
<Optimization>Disabled</Optimization>
109+
<SDLCheck>true</SDLCheck>
110+
</ClCompile>
111+
</ItemDefinitionGroup>
112+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
113+
<ClCompile>
114+
<WarningLevel>Level3</WarningLevel>
115+
<Optimization>Disabled</Optimization>
116+
<SDLCheck>true</SDLCheck>
117+
</ClCompile>
118+
</ItemDefinitionGroup>
119+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
120+
<ClCompile>
121+
<WarningLevel>Level3</WarningLevel>
122+
<Optimization>MaxSpeed</Optimization>
123+
<FunctionLevelLinking>true</FunctionLevelLinking>
124+
<IntrinsicFunctions>true</IntrinsicFunctions>
125+
<SDLCheck>true</SDLCheck>
126+
</ClCompile>
127+
<Link>
128+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
129+
<OptimizeReferences>true</OptimizeReferences>
130+
</Link>
131+
</ItemDefinitionGroup>
132+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
133+
<ClCompile>
134+
<WarningLevel>Level3</WarningLevel>
135+
<Optimization>MaxSpeed</Optimization>
136+
<FunctionLevelLinking>true</FunctionLevelLinking>
137+
<IntrinsicFunctions>true</IntrinsicFunctions>
138+
<SDLCheck>true</SDLCheck>
139+
</ClCompile>
140+
<Link>
141+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
142+
<OptimizeReferences>true</OptimizeReferences>
143+
</Link>
144+
</ItemDefinitionGroup>
145+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
146+
<ImportGroup Label="ExtensionTargets">
147+
</ImportGroup>
148+
</Project>

‎im-proc-filters.vcxproj.filters

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="inc">
5+
<UniqueIdentifier>{1a195bd9-4153-4578-815d-41c24479807a}</UniqueIdentifier>
6+
</Filter>
7+
<Filter Include="src">
8+
<UniqueIdentifier>{6d13590a-9bf0-4807-94bb-850bd13e4964}</UniqueIdentifier>
9+
</Filter>
10+
</ItemGroup>
11+
<ItemGroup>
12+
<ClInclude Include="inc\bilateral.h">
13+
<Filter>inc</Filter>
14+
</ClInclude>
15+
<ClInclude Include="inc\canny.h">
16+
<Filter>inc</Filter>
17+
</ClInclude>
18+
<ClInclude Include="inc\gabor.h">
19+
<Filter>inc</Filter>
20+
</ClInclude>
21+
<ClInclude Include="inc\gauss.h">
22+
<Filter>inc</Filter>
23+
</ClInclude>
24+
<ClInclude Include="inc\gradient.h">
25+
<Filter>inc</Filter>
26+
</ClInclude>
27+
<ClInclude Include="inc\imageformats.h">
28+
<Filter>inc</Filter>
29+
</ClInclude>
30+
<ClInclude Include="inc\imageio.h">
31+
<Filter>inc</Filter>
32+
</ClInclude>
33+
<ClInclude Include="inc\median.h">
34+
<Filter>inc</Filter>
35+
</ClInclude>
36+
<ClInclude Include="inc\mirror.h">
37+
<Filter>inc</Filter>
38+
</ClInclude>
39+
<ClInclude Include="inc\mse_psnr.h">
40+
<Filter>inc</Filter>
41+
</ClInclude>
42+
<ClInclude Include="inc\pixelformats.h">
43+
<Filter>inc</Filter>
44+
</ClInclude>
45+
<ClInclude Include="inc\rotate.h">
46+
<Filter>inc</Filter>
47+
</ClInclude>
48+
<ClInclude Include="inc\sobel.h">
49+
<Filter>inc</Filter>
50+
</ClInclude>
51+
<ClInclude Include="inc\ssim.h">
52+
<Filter>inc</Filter>
53+
</ClInclude>
54+
<ClInclude Include="inc\vessels.h">
55+
<Filter>inc</Filter>
56+
</ClInclude>
57+
</ItemGroup>
58+
<ItemGroup>
59+
<ClCompile Include="src\imageio.cpp">
60+
<Filter>src</Filter>
61+
</ClCompile>
62+
<ClCompile Include="src\main.cpp">
63+
<Filter>src</Filter>
64+
</ClCompile>
65+
</ItemGroup>
66+
</Project>

‎inc/bilateral.h

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
3+
Copyright (c) 2017 Roman Kazantsev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
22+
23+
*/
24+
25+
#pragma once
26+
#define _USE_MATH_DEFINES
27+
28+
#include <vector>
29+
#include <math.h>
30+
#include "imageio.h"
31+
32+
static int ComputeKernelRadius(float sigma_d, float sigma_r) {
33+
int rad = int(std::max<float>(std::ceil(sigma_d), std::ceil(3 * sigma_r)));
34+
return rad;
35+
}
36+
37+
static int CreateGaussianKernel(float sigma_d, int rad, std::vector<float> &kernel) {
38+
kernel.clear();
39+
40+
float coeff = 1.0f / (2.0f * sigma_d * sigma_d * float(M_PI));
41+
42+
for (int l = -rad; l <= rad; l++) {
43+
for (int k = -rad; k <= rad; k++) {
44+
float fd = std::expf(-(k * k + l * l) / (2 * sigma_d * sigma_d)) * coeff;
45+
46+
kernel.push_back(fd);
47+
}
48+
}
49+
return 0;
50+
}
51+
52+
static int CreateBilateralKernel(float sigma_d, float sigma_r, ColorFloatImage const &tmp_image,
53+
std::vector<float> const &gaussian_kernel,
54+
int i, int j, std::vector<float> &kernel) {
55+
56+
int rad = ComputeKernelRadius(sigma_d, sigma_r);
57+
ColorFloatPixel p_base = tmp_image(i, j);
58+
59+
float sum = 0.0f;
60+
float coeff = 1.0f / (2.0f * sigma_r * sigma_r * float(M_PI));
61+
for (int l = -rad; l <= rad; l++) {
62+
for (int k = -rad; k <= rad; k++) {
63+
float fd = 0.0f;
64+
float fr = 0.0f;
65+
ColorFloatPixel p_curr = tmp_image(i + k, j + l);
66+
67+
fd = gaussian_kernel[(l + rad) * (2 * rad + 1) + (k + rad)];
68+
fr = std::expf(-((p_curr.r - p_base.r) * (p_curr.r - p_base.r) +
69+
(p_curr.g - p_base.g) * (p_curr.g - p_base.g) +
70+
(p_curr.b - p_base.b) * (p_curr.b - p_base.b)) / (2 * sigma_r * sigma_r)) * coeff;
71+
kernel.push_back(fd * fr);
72+
sum += (fd * fr);
73+
}
74+
}
75+
76+
// normalize coefficients of the kernel
77+
for (int k = 0; k < kernel.size(); k++) {
78+
kernel[k] /= sum;
79+
}
80+
81+
return 0;
82+
}
83+
84+
int ApplyBilateralFilter(ColorFloatImage const &input_image, ColorFloatImage &output_image,
85+
float sigma_d, float sigma_r) {
86+
87+
int rad = ComputeKernelRadius(sigma_d, sigma_r);
88+
89+
// Compute gaussian kernel
90+
std::vector<float> gaussian_kernel;
91+
CreateGaussianKernel(sigma_d, rad, gaussian_kernel);
92+
93+
// prepare extrapolated image
94+
ColorFloatImage tmp_image(input_image.Width() + 2 * rad, input_image.Height() + 2 * rad);
95+
ExtrapolateImage(input_image, tmp_image, rad);
96+
97+
// process image with bilateral filter
98+
for (int j = 0; j < output_image.Height(); j++) {
99+
for (int i = 0; i < output_image.Width(); i++) {
100+
// compute gabor kernel
101+
std::vector<float> kernel;
102+
kernel.clear();
103+
CreateBilateralKernel(sigma_d, sigma_r, tmp_image,
104+
gaussian_kernel, i + rad, j + rad, kernel);
105+
106+
float sum_r = 0.0f;
107+
float sum_g = 0.0f;
108+
float sum_b = 0.0f;
109+
for (int k = 0; k < kernel.size(); k++) {
110+
int jj = k / (2 * rad + 1);
111+
int ii = k % (2 * rad + 1);
112+
sum_r += kernel[k] * tmp_image(i + ii, j + jj).r;
113+
sum_g += kernel[k] * tmp_image(i + ii, j + jj).g;
114+
sum_b += kernel[k] * tmp_image(i + ii, j + jj).b;
115+
}
116+
output_image(i, j).r = sum_r;
117+
output_image(i, j).g = sum_g;
118+
output_image(i, j).b = sum_b;
119+
}
120+
}
121+
122+
return 0;
123+
}

0 commit comments

Comments
 (0)
Please sign in to comment.