Browse Source

Edge detection experiment.

Former-commit-id: 6770319d4943080addcf9a4d6128477963a6bb7a
pull/17/head
James South 12 years ago
parent
commit
47ceb15606
  1. 54
      build/Build.ImageProcessor.Plugins.Cair.proj
  2. 4
      src/ImageProcessor.Web/Properties/AssemblyInfo.cs
  3. 12
      src/ImageProcessor/ImageFactory.cs
  4. 6
      src/ImageProcessor/ImageProcessor.csproj
  5. 2
      src/ImageProcessor/Imaging/Convolution.cs
  6. 66
      src/ImageProcessor/Imaging/EdgeDetection/ConvolutionFilter.cs
  7. 9
      src/ImageProcessor/Imaging/EdgeDetection/IEdgeFilter.cs
  8. 27
      src/ImageProcessor/Imaging/EdgeDetection/SobelEdgeFilter.cs
  9. 78
      src/ImageProcessor/Processors/DetectEdges.cs
  10. 4
      src/ImageProcessor/Properties/AssemblyInfo.cs
  11. 6
      src/ImageProcessorConsole/Program.cs
  12. 6
      src/Plugins/ImageProcessor/ImageProcessor.Plugins.Cair/Properties/AssemblyInfo.cs
  13. 6
      src/Plugins/ImageProcessor/ImageProcessor.Plugins.WebP/Properties/AssemblyInfo.cs

54
build/Build.ImageProcessor.Plugins.Cair.proj

@ -1,54 +0,0 @@
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildCommunityTasksPath>.\</MSBuildCommunityTasksPath>
</PropertyGroup>
<Import Project=".\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
<!--
****************************************************
VARIABLES
*****************************************************
-->
<PropertyGroup>
<BuildConfiguration>Release</BuildConfiguration>
<BuildFolder>_BuildOutput\</BuildFolder>
<IncludeSymbols>False</IncludeSymbols>
<BuildFolderAbsolutePath>$(MSBuildProjectDirectory)\$(BuildFolder)</BuildFolderAbsolutePath>
<SolutionBinFolderAbsolutePath>$(BuildFolderAbsolutePath)ImageProcessor.Plugins.Cair\lib\net45</SolutionBinFolderAbsolutePath>
<BuildInputDir>..\src\Plugins\ImageProcessor\ImageProcessor.Plugins.Cair\</BuildInputDir>
</PropertyGroup>
<!--
****************************************************
TARGETS
*****************************************************
-->
<Target Name="Build" DependsOnTargets="BuildImageProcessorPluginsCair">
<Message Text="Build finished" />
</Target>
<Target Name="BuildImageProcessorPluginsCair" DependsOnTargets="SetVersionNumber">
<Message Text="Compiling ImageProcessor.Plugins.Cair project to build\$(BuildFolder)" Importance="High" />
<MSBuild Projects="$(BuildInputDir)\ImageProcessor.Plugins.Cair.csproj" Properties="WarningLevel=0;Configuration=$(BuildConfiguration);PipelineDependsOnBuild=False;OutDir=$(SolutionBinFolderAbsolutePath);" Targets="Clean;Rebuild;" BuildInParallel="False" ToolsVersion="4.0" UnloadProjectsOnCompletion="False" />
<Message Text="Finished compiling project" Importance="High" />
</Target>
<Target Name="SetVersionNumber" Condition="'$(BUILD_RELEASE)'!=''">
<Message Text="Creating Version File: $(BUILD_RELEASE)"/>
<ItemGroup>
<AssemblyFiles Include="$(BuildInputDir)Properties\AssemblyInfo.cs;" />
</ItemGroup>
<FileUpdate Files="@(AssemblyFiles)"
Multiline="true"
Singleline="false"
Regex="(AssemblyVersion|AssemblyFileVersionAttribute|AssemblyFileVersion)\(&quot;([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)?&quot;\)"
ReplacementText="$1(&quot;$(BUILD_RELEASE)&quot;)" />
</Target>
</Project>

4
src/ImageProcessor.Web/Properties/AssemblyInfo.cs

@ -1,4 +1,4 @@
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AssemblyInfo.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
@ -19,7 +19,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyConfiguration("James South")]
[assembly: AssemblyCompany("James South")]
[assembly: AssemblyProduct("ImageProcessor.Web")]
[assembly: AssemblyCopyright("Copyright © James South")]
[assembly: AssemblyCopyright("Copyright © James South")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

12
src/ImageProcessor/ImageFactory.cs

@ -20,6 +20,7 @@ namespace ImageProcessor
using ImageProcessor.Common.Exceptions;
using ImageProcessor.Imaging;
using ImageProcessor.Imaging.EdgeDetection;
using ImageProcessor.Imaging.Filters;
using ImageProcessor.Imaging.Formats;
using ImageProcessor.Processors;
@ -440,6 +441,17 @@ namespace ImageProcessor
return this;
}
public ImageFactory DetectEdges(IEdgeFilter filter)
{
if (this.ShouldProcess)
{
DetectEdges detectEdges = new DetectEdges { DynamicParameter = filter };
this.CurrentImageFormat.ApplyProcessor(detectEdges.ProcessImage, this);
}
return this;
}
/// <summary>
/// Applies a filter to the current image. Use the <see cref="MatrixFilters"/> class to
/// assign the correct filter.

6
src/ImageProcessor/ImageProcessor.csproj

@ -37,6 +37,7 @@
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\ImageProcessor.XML</DocumentationFile>
<Prefer32Bit>false</Prefer32Bit>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'All|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
@ -51,6 +52,7 @@
<WarningLevel>4</WarningLevel>
<Optimize>false</Optimize>
<Prefer32Bit>false</Prefer32Bit>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
@ -74,6 +76,9 @@
<Compile Include="Imaging\Colors\HslaColor.cs" />
<Compile Include="Imaging\Colors\RgbaColor.cs" />
<Compile Include="Imaging\Colors\YCbCrColor.cs" />
<Compile Include="Imaging\EdgeDetection\ConvolutionFilter.cs" />
<Compile Include="Imaging\EdgeDetection\IEdgeFilter.cs" />
<Compile Include="Imaging\EdgeDetection\SobelEdgeFilter.cs" />
<Compile Include="Imaging\FastBitmap.cs">
<SubType>Code</SubType>
</Compile>
@ -121,6 +126,7 @@
<Compile Include="Processors\Alpha.cs" />
<Compile Include="Processors\BackgroundColor.cs" />
<Compile Include="Processors\AutoRotate.cs" />
<Compile Include="Processors\DetectEdges.cs" />
<Compile Include="Processors\GaussianBlur.cs" />
<Compile Include="Processors\Brightness.cs" />
<Compile Include="Processors\Contrast.cs" />

2
src/ImageProcessor/Imaging/Convolution.cs

@ -12,8 +12,6 @@ namespace ImageProcessor.Imaging
{
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using ImageProcessor.Common.Extensions;

66
src/ImageProcessor/Imaging/EdgeDetection/ConvolutionFilter.cs

@ -0,0 +1,66 @@

namespace ImageProcessor.Imaging.EdgeDetection
{
using System;
using System.Drawing;
using System.Drawing.Imaging;
using ImageProcessor.Common.Extensions;
/// <summary>
/// http://pastebin.com/xHHD3pXi
/// </summary>
public class ConvolutionFilter
{
private readonly IEdgeFilter edgeFilter;
public ConvolutionFilter(IEdgeFilter edgeFilter)
{
this.edgeFilter = edgeFilter;
}
public Bitmap ProcessFilter(Bitmap source)
{
double[,] horizontalFilter = this.edgeFilter.HorizontalMatrix;
double[,] verticallFilter = this.edgeFilter.VerticalMatrix;
int width = source.Width;
int height = source.Height;
int maxWidth = width - 1;
int maxHeight = height - 1;
Bitmap destination = new Bitmap(width, height, PixelFormat.Format32bppArgb);
using (FastBitmap sourceBitmap = new FastBitmap(source))
{
using (FastBitmap destinationBitmap = new FastBitmap(destination))
{
for (int y = 1; y < maxHeight; y++)
{
for (int x = 1; x < maxWidth; x++)
{
double newX = 0;
double newY = 0;
for (int hw = -1; hw < 2; hw++)
{
for (int wi = -1; wi < 2; wi++)
{
double component = sourceBitmap.GetPixel(x + wi, y + hw).B;
newX += horizontalFilter[hw + 1, wi + 1] * component;
newY += verticallFilter[hw + 1, wi + 1] * component;
}
}
byte value = Math.Sqrt((newX * newX) + (newY * newY)).ToByte();
Color tempcolor = Color.FromArgb(value, value, value);
destinationBitmap.SetPixel(x, y, tempcolor);
}
}
}
}
return destination;
}
}
}

9
src/ImageProcessor/Imaging/EdgeDetection/IEdgeFilter.cs

@ -0,0 +1,9 @@
namespace ImageProcessor.Imaging.EdgeDetection
{
public interface IEdgeFilter
{
double[,] HorizontalMatrix { get; }
double[,] VerticalMatrix { get; }
}
}

27
src/ImageProcessor/Imaging/EdgeDetection/SobelEdgeFilter.cs

@ -0,0 +1,27 @@
namespace ImageProcessor.Imaging.EdgeDetection
{
public class SobelEdgeFilter : IEdgeFilter
{
public double[,] HorizontalMatrix
{
get
{
return new double[,]
{ { -1, 0, 1, },
{ -2, 0, 2, },
{ -1, 0, 1, }, };
}
}
public double[,] VerticalMatrix
{
get
{
return new double[,]
{ { 1, 2, 1, },
{ 0, 0, 0, },
{ -1, -2, -1, }, };
}
}
}
}

78
src/ImageProcessor/Processors/DetectEdges.cs

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ImageProcessor.Processors
{
using System.Drawing;
using ImageProcessor.Common.Exceptions;
using ImageProcessor.Imaging.EdgeDetection;
public class DetectEdges : IGraphicsProcessor
{
/// <summary>
/// Initializes a new instance of the <see cref="DetectEdges"/> class.
/// </summary>
public DetectEdges()
{
this.Settings = new Dictionary<string, string>();
}
/// <summary>
/// Gets or sets the dynamic parameter.
/// </summary>
public dynamic DynamicParameter
{
get;
set;
}
/// <summary>
/// Gets or sets any additional settings required by the processor.
/// </summary>
public Dictionary<string, string> Settings
{
get;
set;
}
/// <summary>
/// Processes the image.
/// </summary>
/// <param name="factory">
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
/// the image to process.
/// </param>
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory)
{
Bitmap newImage = null;
Image image = factory.Image;
IEdgeFilter filter = this.DynamicParameter;
try
{
ConvolutionFilter convolutionFilter = new ConvolutionFilter(filter);
newImage = convolutionFilter.ProcessFilter((Bitmap)image);
image.Dispose();
image = newImage;
}
catch (Exception ex)
{
if (newImage != null)
{
newImage.Dispose();
}
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
return image;
}
}
}

4
src/ImageProcessor/Properties/AssemblyInfo.cs

@ -1,4 +1,4 @@
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AssemblyInfo.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
@ -19,7 +19,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("James South")]
[assembly: AssemblyProduct("ImageProcessor")]
[assembly: AssemblyCopyright("Copyright © James South")]
[assembly: AssemblyCopyright("Copyright © James South")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

6
src/ImageProcessorConsole/Program.cs

@ -18,6 +18,7 @@ namespace ImageProcessorConsole
using System.Linq;
using ImageProcessor;
using ImageProcessor.Imaging;
using ImageProcessor.Imaging.EdgeDetection;
using ImageProcessor.Imaging.Filters;
using ImageProcessor.Plugins.Cair;
using ImageProcessor.Plugins.Cair.Imaging;
@ -45,8 +46,8 @@ namespace ImageProcessorConsole
di.Create();
}
//IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".jpg");
IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".gif", ".webp", ".bmp", ".jpg", ".png", ".tif");
IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".jpg");
//IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".gif", ".webp", ".bmp", ".jpg", ".png", ".tif");
foreach (FileInfo fileInfo in files)
{
@ -76,6 +77,7 @@ namespace ImageProcessorConsole
//.Constrain(size)
//.ReplaceColor(Color.FromArgb(255, 1, 107, 165), Color.FromArgb(255, 1, 165, 13), 80)
.Resize(layer)
.DetectEdges(new SobelEdgeFilter())
//.Filter(MatrixFilters.Comic)
//.Filter(MatrixFilters.HiSatch)
//.Pixelate(8)

6
src/Plugins/ImageProcessor/ImageProcessor.Plugins.Cair/Properties/AssemblyInfo.cs

@ -1,4 +1,4 @@
using System.Reflection;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@ -10,7 +10,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ImageProcessor.Plugins.Cair")]
[assembly: AssemblyCopyright("Copyright James South © 2014")]
[assembly: AssemblyCopyright("Copyright James South © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@ -31,6 +31,6 @@ using System.Runtime.InteropServices;
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
// [assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

6
src/Plugins/ImageProcessor/ImageProcessor.Plugins.WebP/Properties/AssemblyInfo.cs

@ -1,4 +1,4 @@
using System.Reflection;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@ -10,7 +10,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("James South")]
[assembly: AssemblyProduct("ImageProcessor.Plugins.WebP")]
[assembly: AssemblyCopyright("Copyright © James South")]
[assembly: AssemblyCopyright("Copyright © James South")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@ -31,6 +31,6 @@ using System.Runtime.InteropServices;
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
// [assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]

Loading…
Cancel
Save