Advertisement
Guest User

Untitled

a guest
Nov 6th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.68 KB | None | 0 0
  1. open System.Drawing
  2. open System.Drawing.Imaging
  3. open System.IO
  4. open System
  5.  
  6. type Sampler = int * int -> Color
  7. type PixelFunction = Sampler -> int * int -> Color
  8.  
  9. let applyFunction (source: Bitmap) (target: Bitmap) (getPixel: PixelFunction) =
  10.  
  11.     let w = source.Width
  12.     let h = source.Height
  13.  
  14.     let sampler (u: int, v: int) =
  15.         source.GetPixel(
  16.             (u + w) % w,
  17.             (v + h) % h
  18.         )
  19.  
  20.     for v in 0..(target.Height - 1) do
  21.         for u in 0..(target.Width - 1) do
  22.             target.SetPixel(u, v, getPixel sampler (u, v))
  23.  
  24. let convertImage sourcePath transformation =
  25.     let targetPath = Path.GetFileNameWithoutExtension(sourcePath) + "_output.jpg"
  26.     use source = new Bitmap(sourcePath)
  27.     use target: Bitmap = transformation source
  28.     target.Save(targetPath, ImageFormat.Jpeg)
  29.  
  30. let grayScale sourcePath =
  31.     let rand = new Random()
  32.  
  33.     let transformation (source: Bitmap) =
  34.         let target = new Bitmap(source.Width, source.Height)
  35.         let getPixel samp (u, v) =
  36.             let getIntensity u v =
  37.                 let (color: Color) = samp (u, v)
  38.                 (float color.R * 0.2126 + float color.G * 0.7152 + float color.B * 0.0722) / 255.0
  39.  
  40.             let sd = 0.5
  41.             let range = 5
  42.  
  43.             let gaussianFunction x y =
  44.                 exp(-(float x ** 2.0 + float y ** 2.0) / (2.0 * sd ** 2.0)) / (2.0 * Math.PI * sd ** 2.0)
  45.  
  46.             let normalization =
  47.                 { -range .. range }
  48.                 |> Seq.sumBy (fun dx ->
  49.                     { -range .. range }
  50.                     |> Seq.sumBy (gaussianFunction dx))
  51.  
  52.             let pixel dx dy =
  53.                 (gaussianFunction dx dy) * getIntensity (u + dx) (v + dy) / normalization
  54.  
  55.             let intensity =
  56.                 { -range .. range }
  57.                 |> Seq.sumBy (fun dx ->
  58.                     { -range .. range }
  59.                     |> Seq.sumBy (pixel dx)
  60.                 )
  61.  
  62.             //if rand.NextDouble() > intensity
  63.             //then Color.Black
  64.             //else Color.White
  65.  
  66.             let x = int (intensity * 255.0)
  67.             Color.FromArgb(x, x, x)
  68.  
  69.         applyFunction source target getPixel
  70.         target
  71.  
  72.     convertImage sourcePath transformation
  73.  
  74. let batchConvert (dir: string) (convertor: string -> unit) =
  75.     Directory.EnumerateFiles(dir) |> Seq.iter convertor
  76.  
  77. let batchConvertAsync (dir: string) (convertor: string -> unit) =
  78.     let asyncConvertor file = async { convertor file }
  79.     Directory.EnumerateFiles(dir)
  80.     |> Seq.map asyncConvertor
  81.     |> Async.Parallel
  82.     |> Async.RunSynchronously
  83.     |> ignore
  84.  
  85. //batchConvertAsync "input" grayScale
  86. grayScale @"7_output.jpg"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement