Skip to content

Instantly share code, notes, and snippets.

@ldfallas
Created January 10, 2010 15:16
Show Gist options
  • Save ldfallas/273560 to your computer and use it in GitHub Desktop.
Save ldfallas/273560 to your computer and use it in GitHub Desktop.
namespace LangexplrExperiments
open System.Drawing
open System.Drawing.Imaging
open Microsoft.FSharp.Math
module ImageProcExperiments = begin
let Load24BitImageData (fileName:string) =
using(new Bitmap(fileName))
(fun bm ->
if bm.PixelFormat <> PixelFormat.Format24bppRgb then
raise (new System.NotSupportedException("Bitmap not supported"))
let rect = new Rectangle(0,0,bm.Width,bm.Height)
let bmpData = bm.LockBits(rect,System.Drawing.Imaging.ImageLockMode.ReadOnly,bm.PixelFormat)
let result : byte array = Array.create (bmpData.Height*bmpData.Stride) (byte(0))
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, result, 0, bmpData.Height*bmpData.Stride);
bm.UnlockBits(bmpData)
bmpData.Height,bmpData.Stride,result
)
let Save24BitImageData (width:int,height:int,data : byte array,fileName:string) =
using(new Bitmap(width,height,PixelFormat.Format24bppRgb))
(fun bm ->
let rect = new Rectangle(0,0,bm.Width,bm.Height)
let bmpData = bm.LockBits(rect,System.Drawing.Imaging.ImageLockMode.WriteOnly,bm.PixelFormat)
System.Runtime.InteropServices.Marshal.Copy(data,0,bmpData.Scan0, Array.length data);
bm.UnlockBits(bmpData)
bm.Save(fileName,ImageFormat.Jpeg)
)
let SeparateChannel( width,height,data : byte array,channel) =
let newWidth = (width/3)
let result = Array2D.create height newWidth (byte(0))
Array.iteri (fun i _ -> if ((i+3) % 3 = 0) then
result.[i/(3*newWidth),(i/3)%newWidth] <- data.[i + channel]
else
())
data
result
let MatrixToArray( width,height,data,channel) =
let newWidth = width*3
let result = Array.create (height*width*3) (byte(0))
Array2D.iteri (fun i j d ->
result.[(newWidth*i)+((j*3) + channel)] <- d) data
result
let FlattenUsingOneChannel( data : byte array,channel) =
Array.iteri (fun i _ -> if (i % 3 = 0) then
let newData = data.[i+channel]
data.[i] <- newData
data.[i+1] <- newData
data.[i+2] <- newData)
data
data
let DFT (matrix:byte[,]) (col:int) (row:int) =
let c = 1.0/float(Array2D.length1 matrix)
let mutable result = Complex.zero
for y = 0 to Array2D.length1 matrix do
for x = 0 to Array2D.length2 matrix do
let exponent = float(-1)* Complex.onei * float(2) * float(col*x + row*y)
result <- result + float(matrix.[row,col]) * Complex.Exp( (1.0/float(Array2D.length1 matrix) * exponent ))
c*result
let imageDFT matrix =
Array2D.mapi (fun i j _ -> DFT matrix j i) matrix
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment