public override ulong Create(BitmapData image) { var data = new DenseMatrix(32, 32); using (var unmanaged = new UnmanagedImage(image)) { using (var filtered = ExtractChannel.Apply(unmanaged)) { Filter.ApplyInPlace(filtered); using (var imgdata = Resize.Apply(filtered)) { unsafe { var src = (byte *)imgdata.ImageData.ToPointer(); var offset = imgdata.Stride - imgdata.Width; var width = imgdata.Width; var height = imgdata.Height; for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++, src++) { data.At(y, x, (float)*src / 255); } src += offset; } } } } } var dct = DctMatrix.FastDCT(data); var vals = new double[dctsize * dctsize]; var valscount = 0; for (var r = 1; r <= dctsize; r++) { for (var c = 1; c <= dctsize; c++) { vals[valscount] = dct.At(r, c); ++valscount; } } var sorted = new double[dctsize * dctsize]; Array.Copy(vals, sorted, vals.Length); Array.Sort(sorted); var mid = dctsize * dctsize / 2; var median = (sorted[mid - 1] + sorted[mid]) / 2d; ulong index = 1; ulong result = 0; for (var i = 0; i < dctsize * dctsize; i++) { if (vals[i] > median) { result |= index; } index = index << 1; } return(result); }
public override ulong Create(BitmapData image) { var data = new DenseMatrix(32, 32); using (var unmanaged = UnmanagedImage.FromManagedImage(image)) using (var filtered = ExtractChannel.Apply(unmanaged)) { new Convolution(Filter, 9).ApplyInPlace(filtered); using (var imgdata = new ResizeNearestNeighbor(32, 32).Apply(filtered)) { unsafe { byte *src = (byte *)imgdata.ImageData.ToPointer(); int offset = imgdata.Stride - imgdata.Width; int width = imgdata.Width; int height = imgdata.Height; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, src++) { data.At(y, x, (float)*src / 255); } src += offset; } } } } var dct = DctMatrix.FastDCT(data); int dctsize = 8; var vals = new List <double>(); for (int r = 1; r <= dctsize; r++) { for (int c = 1; c <= dctsize; c++) { vals.Add(dct[r, c]); } } var sorted = new List <double>(vals); sorted.Sort(); var mid = dctsize * dctsize / 2; double median = (sorted[mid - 1] + sorted[mid]) / 2d; ulong index = 1; ulong result = 0; for (int i = 0; i < dctsize * dctsize; i++) { if (vals[i] > median) { result |= index; } index = index << 1; } return(result); }