From 163dd24dddaa11348e7f466c28ade4b5fa378f01 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sat, 9 May 2015 09:43:42 +0200 Subject: [PATCH] Add a little project in F# using XAML + Emgu (OpenCV). --- Parasitemia/Parasitemia.sln | 22 ++++ Parasitemia/Parasitemia/App.config | 14 +++ Parasitemia/Parasitemia/AssemblyInfo.fs | 41 +++++++ Parasitemia/Parasitemia/MainWindow.xaml | 11 ++ Parasitemia/Parasitemia/MainWindow.xaml.fs | 6 + Parasitemia/Parasitemia/Parasitemia.fsproj | 122 +++++++++++++++++++++ Parasitemia/Parasitemia/Program.fs | 68 ++++++++++++ Parasitemia/Parasitemia/packages.config | 6 + 8 files changed, 290 insertions(+) create mode 100644 Parasitemia/Parasitemia.sln create mode 100644 Parasitemia/Parasitemia/App.config create mode 100644 Parasitemia/Parasitemia/AssemblyInfo.fs create mode 100644 Parasitemia/Parasitemia/MainWindow.xaml create mode 100644 Parasitemia/Parasitemia/MainWindow.xaml.fs create mode 100644 Parasitemia/Parasitemia/Parasitemia.fsproj create mode 100644 Parasitemia/Parasitemia/Program.fs create mode 100644 Parasitemia/Parasitemia/packages.config diff --git a/Parasitemia/Parasitemia.sln b/Parasitemia/Parasitemia.sln new file mode 100644 index 0000000..983f997 --- /dev/null +++ b/Parasitemia/Parasitemia.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.22823.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Parasitemia", "Parasitemia\Parasitemia.fsproj", "{70838E65-F211-44FC-B28F-0ED1CA6E850F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {70838E65-F211-44FC-B28F-0ED1CA6E850F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {70838E65-F211-44FC-B28F-0ED1CA6E850F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {70838E65-F211-44FC-B28F-0ED1CA6E850F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {70838E65-F211-44FC-B28F-0ED1CA6E850F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Parasitemia/Parasitemia/App.config b/Parasitemia/Parasitemia/App.config new file mode 100644 index 0000000..e6cb850 --- /dev/null +++ b/Parasitemia/Parasitemia/App.config @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Parasitemia/Parasitemia/AssemblyInfo.fs b/Parasitemia/Parasitemia/AssemblyInfo.fs new file mode 100644 index 0000000..269e427 --- /dev/null +++ b/Parasitemia/Parasitemia/AssemblyInfo.fs @@ -0,0 +1,41 @@ +namespace Parasitemia.AssemblyInfo + +open System.Reflection +open System.Runtime.CompilerServices +open System.Runtime.InteropServices + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[] +[] +[] +[] +[] +[] +[] +[] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [] +[] +[] + +do + () \ No newline at end of file diff --git a/Parasitemia/Parasitemia/MainWindow.xaml b/Parasitemia/Parasitemia/MainWindow.xaml new file mode 100644 index 0000000..8578d8c --- /dev/null +++ b/Parasitemia/Parasitemia/MainWindow.xaml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Parasitemia/Parasitemia/MainWindow.xaml.fs b/Parasitemia/Parasitemia/MainWindow.xaml.fs new file mode 100644 index 0000000..26d30c9 --- /dev/null +++ b/Parasitemia/Parasitemia/MainWindow.xaml.fs @@ -0,0 +1,6 @@ +namespace Parasitemia.Views + +open FsXaml + +type MainWindow = XAML<"MainWindow.xaml"> + diff --git a/Parasitemia/Parasitemia/Parasitemia.fsproj b/Parasitemia/Parasitemia/Parasitemia.fsproj new file mode 100644 index 0000000..c071e66 --- /dev/null +++ b/Parasitemia/Parasitemia/Parasitemia.fsproj @@ -0,0 +1,122 @@ + + + + + + Debug + AnyCPU + 2.0 + 70838e65-f211-44fc-b28f-0ed1ca6e850f + WinExe + Parasitemia + Parasitemia + v4.6 + true + 4.4.0.0 + Parasitemia + + + + + + true + full + false + false + bin\Debug\ + DEBUG;TRACE + 3 + x64 + bin\Debug\Parasitemia.XML + false + + + pdbonly + true + true + bin\Release\ + TRACE + 3 + x64 + bin\Release\Parasitemia.XML + false + + + 11 + + + + + $(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets + + + + + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + + + + + + + + + + ..\..\..\Programs\Emgu\emgucv-windows-universal 3.0.0.2032\bin\Emgu.CV.dll + + + ..\..\..\Programs\Emgu\emgucv-windows-universal 3.0.0.2032\bin\Emgu.CV.WPF.exe + + + ..\..\..\Programs\Emgu\emgucv-windows-universal 3.0.0.2032\bin\Emgu.Util.dll + + + ..\packages\FsXaml.Wpf.0.9.9\lib\net45\FsXaml.Wpf.dll + True + + + ..\packages\FsXaml.Wpf.0.9.9\lib\net45\FsXaml.Wpf.TypeProvider.dll + True + + + + True + + + + + + + + + ..\packages\Expression.Blend.Sdk.1.0.2\lib\net45\System.Windows.Interactivity.dll + True + + + + + + + xcopy "D:\Programs\Emgu\emgucv-windows-universal 3.0.0.2032\bin\x64\*" $(TargetDir) /Y /D + + + \ No newline at end of file diff --git a/Parasitemia/Parasitemia/Program.fs b/Parasitemia/Parasitemia/Program.fs new file mode 100644 index 0000000..e9ae0d4 --- /dev/null +++ b/Parasitemia/Parasitemia/Program.fs @@ -0,0 +1,68 @@ +module Parasitemia.Main + +open System.IO +open System.Windows +open System.Windows.Media +open System.Windows.Markup +open System.Windows.Shapes +open System.Windows.Controls +open System.Drawing +open System.Diagnostics + +open Emgu.CV +open Emgu.CV.Structure +open Emgu.CV.WPF + +let display (window : Views.MainWindow) (img : IImage) = + let imgControl = window.Root.FindName("img") :?> Controls.Image + imgControl.Source <- BitmapSourceConvert.ToBitmapSource(img) + +let log (window : Views.MainWindow) (mess : string) = + let txtLog = window.Root.FindName("txtLog") :?> Controls.TextBlock + txtLog.Text <- txtLog.Text + mess + "\n" + +let inline dprintfn fmt = + Printf.ksprintf System.Diagnostics.Debug.WriteLine fmt + +let measureTime<'a> (print : string -> unit) (f : unit -> 'a) = + let sw = Stopwatch() + sw.Start() + let res = f () + sw.Stop() + print <| sprintf "time: %A ms" sw.ElapsedMilliseconds + res + +[] +do + dprintfn "OpenCV test" + + let app = new Application() + + let mainWindow = Views.MainWindow() + + let timer = measureTime (log mainWindow) + + use image = new Image("../../../../imgs_corrMK/1412151257/1412151257-Gamma-0.8-0002.png") + + use scaledImage = image.Resize(0.6, CvEnum.Inter.Area) + + use imageHSV = scaledImage.Convert() + let H, S = match imageHSV.Split() with // Warning: H is from 0 to 179°. + | [| H; S; _|] -> H, S + | _ -> failwith "unable to split the HSV channels" + + let hueShiftValue = 175 + // Modulo operator doesn't exist on matrix thus we have to apply a function to every pixels. + let correctedH : Image = H.Convert(fun b _ _ -> + (255 - int(b) * 255 / 179 + hueShiftValue) % 256 |> byte + ) + + let correctedS : Image = S.Not() + + let filteredH = correctedH.SmoothMedian(5) + let filteredS = correctedS.SmoothMedian(5) + + display mainWindow filteredS + mainWindow.Root.Show() + + app.Run() |> ignore diff --git a/Parasitemia/Parasitemia/packages.config b/Parasitemia/Parasitemia/packages.config new file mode 100644 index 0000000..9ac3914 --- /dev/null +++ b/Parasitemia/Parasitemia/packages.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file -- 2.45.2