private static void SegmentImageParam(Bitmap src, string outputFile, double sigma, int k, StringBuilder sb) { BitmapData srcData = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadOnly, src.PixelFormat); int bytesPerPixel = 1; if (src.PixelFormat == PixelFormat.Format8bppIndexed) bytesPerPixel = 1; else if (src.PixelFormat == PixelFormat.Format24bppRgb) bytesPerPixel = 3; else if ((src.PixelFormat == PixelFormat.Format32bppRgb) || (src.PixelFormat == PixelFormat.Format32bppArgb)) bytesPerPixel = 4; Bitmap dst = new Bitmap(src.Width, src.Height, src.PixelFormat); BitmapData dstData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.WriteOnly, dst.PixelFormat); Stopwatch stopwatch = new Stopwatch(); Fhs.Fhs fhs = new Fhs.Fhs(); stopwatch.Start(); unsafe { fhs.fhs((byte*)srcData.Scan0, src.Width, src.Height, srcData.Stride, bytesPerPixel, (byte*)dstData.Scan0, sigma, k); } stopwatch.Stop(); src.UnlockBits(srcData); dst.UnlockBits(dstData); int[,] segments = ImageHelper.GetSegments(dst); int max = 0; foreach (int s in segments) if (s > max) max = s; sb.AppendFormat("sigma = {0:0.0}, k = {1}: segmentation time = {2}, segment count = {3}", sigma, k, stopwatch.Elapsed, max + 1); sb.AppendLine(); Bitmap seg = ImageHelper.GetBitmap(segments); seg.Save(outputFile); }
public void SegmentCppImage() { Task task = Task.Factory.StartNew(() => { App.Current.Dispatcher.Invoke(new Action(() => { ProcessRunning = true; })); try { Bitmap src = OriginImage.Bitmap; BitmapData srcData = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadOnly, src.PixelFormat); int bytesPerPixel = 1; if (src.PixelFormat == PixelFormat.Format8bppIndexed) bytesPerPixel = 1; else if (src.PixelFormat == PixelFormat.Format24bppRgb) bytesPerPixel = 3; else if ((src.PixelFormat == PixelFormat.Format32bppRgb) || (src.PixelFormat == PixelFormat.Format32bppArgb)) bytesPerPixel = 4; Bitmap dst = new Bitmap(src.Width, src.Height, src.PixelFormat); BitmapData dstData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.WriteOnly, dst.PixelFormat); Fhs.Fhs fhs = new Fhs.Fhs(); unsafe { fhs.fhs((byte*)srcData.Scan0, src.Width, src.Height, srcData.Stride, bytesPerPixel, (byte*)dstData.Scan0, 0.8, 300); } src.UnlockBits(srcData); dst.UnlockBits(dstData); int[,] segments = ImageHelper.GetSegments(dst); App.Current.Dispatcher.Invoke(new Action(() => { SegmentedImage = new ImageViewModel(ImageHelper.GetBitmap(segments)); })); } catch { } finally { App.Current.Dispatcher.Invoke(new Action(() => { ProcessRunning = false; })); } }); }