static void Main(string[] args) { var img = new Bgr<byte>[480, 640]; //*********************************************************************************************************************************************************************** Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("********* TColor[,] <=> Image<> conversions (built-in) ****************"); Console.ResetColor(); //to Image<> Image<Bgr<byte>> lockedImg = img.Lock(); //from Image<> var arr = lockedImg.Clone(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("********* Image<,> <=> OpenCV conversions (built-in) ****************"); Console.ResetColor(); //to IplImage IplImage iplImage; using (var uImg = img.Lock()) { iplImage = uImg.AsOpenCvImage(); //data is shared } //from IplImage var imgFromIpl = iplImage.AsImage(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("*********** Image<,> <=> Bitmap conversions (BitmapInterop) ****************"); Console.ResetColor(); //to Bitmap var bmp = img.ToBitmap(); //from Bitmap var imgFromBmp = bmp.ToArray(); }
static void Main(string[] args) { var img = new Bgr <byte> [480, 640]; //*********************************************************************************************************************************************************************** Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("********* TColor[,] <=> Image<> conversions (built-in) ****************"); Console.ResetColor(); //to Image<> Image <Bgr <byte> > lockedImg = img.Lock(); //from Image<> var arr = lockedImg.Clone(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("********* Image<,> <=> OpenCV conversions (built-in) ****************"); Console.ResetColor(); //to IplImage IplImage iplImage; using (var uImg = img.Lock()) { iplImage = uImg.AsCvIplImage(); //data is shared } //from IplImage var imgFromIpl = iplImage.AsImage(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("*********** Image<,> <=> Bitmap conversions (BitmapInterop) ****************"); Console.ResetColor(); //to Bitmap var bmp = img.ToBitmap(); //from Bitmap var imgFromBmp = bmp.ToArray(); }
static void Main(string[] args) { Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + ";runtimes/win10-x64/"); //only needed if projects are directly referenced var img = new Bgr <byte> [480, 640]; //*********************************************************************************************************************************************************************** Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("********* TColor[,] <=> Image<> conversions (built-in) ****************"); Console.ResetColor(); //to Image<> Image <Bgr <byte> > lockedImg = img.Lock(); //from Image<> var arr = lockedImg.Clone(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("********* Image<,> <=> OpenCV conversions (built-in) ****************"); Console.ResetColor(); //to IplImage IplImage iplImage; using (var uImg = img.Lock()) { iplImage = uImg.AsCvIplImage(); //data is shared } //from IplImage var imgFromIpl = iplImage.AsImage(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("*********** Image<,> <=> Bitmap conversions (BitmapInterop) ****************"); Console.ResetColor(); //to Bitmap var bmp = img.ToBitmap(); //from Bitmap var imgFromBmp = bmp.ToImage <Bgr <byte> >(); }
static void Main() { //var reader = new CameraCapture(0); //capture from camera var reader = new FileCapture(Path.Combine(getResourceDir(), "Welcome.mp4")); reader.Open(); var writer = new VideoWriter(@"output.avi", reader.FrameSize, /*reader.FrameRate does not work Cameras*/ 30); //TODO: bug: FPS does not work for cameras writer.Open(); Bgr <byte>[,] frame = null; do { reader.ReadTo(ref frame); if (frame == null) { break; } using (var uFrame = frame.Lock()) { writer.Write(uFrame); } frame.Show(scaleForm: true); }while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape)); reader.Dispose(); writer.Dispose(); UI.CloseAll(); }
/// <summary> /// Save a stream of image frames as an AVI video file. /// </summary> /// <param name="frames">Ordered list of frames at 30 fps.</param> /// <param name="width">Frame width.</param> /// <param name="height">Frame height.</param> /// <param name="file">Output filename.</param> public static void SaveAsAvi(List <Color[]> frames, int width, int height, string file) { if (frames.Count == 0) { return; } using (VideoWriter writer = new VideoWriter(file, new Size(width, height), 30, true, VideoCodec.FromName("MJPG"))) { writer.Open(); foreach (Color[] frame in frames) { Bgr <byte>[,] bitmap = new Bgr <byte> [height, width]; int h = 0; for (int k = 0; k < height; k++) { for (int i = 0; i < width; i++) { bitmap[k, i] = new Bgr <byte>(frame[h].B, frame[h].G, frame[h].R); h++; } } writer.Write(bitmap.Lock()); } writer.Close(); } }
/// <summary> /// Evaluates the function using the provided image. /// </summary> /// <param name="image">Color image.</param> /// <returns>Entropy value. Smaller entropy is preferred.</returns> public unsafe float Evaluate(Bgr <byte>[,] image) { var histogram = new int[HISTOGRAM_SIZE]; using (var uImg = image.Lock()) { Bgr8 *imData = (Bgr8 *)uImg.ImageData; for (int r = 0; r < uImg.Height; r++) { for (int c = 0; c < uImg.Width; c++) { Bgr8 bgr = imData[c]; float intensity = 0.2126f * bgr.R + 0.7152f * bgr.G + 0.0722f * bgr.B; float gain = getGainAt(this, c, r, uImg.Width, uImg.Height); int correction = (int)(gain * intensity); updateHistogram(histogram, correction); } imData = (Bgr8 *)((byte *)imData + uImg.Stride); } } float entropy = calculateEntropy(histogram); return(entropy); }
static void Main() { Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + ";runtimes/win10-x64/"); //only needed if projects are directly referenced //var reader = new CameraCapture(0); //capture from camera var reader = new FileCapture(Path.Combine(getResourceDir(), "Welcome.mp4")); reader.Open(); var writer = new VideoWriter(@"output.avi", reader.FrameSize, /*reader.FrameRate does not work Cameras*/ 30); //TODO: bug: FPS does not work for cameras writer.Open(); Bgr <byte>[,] frame = null; do { reader.ReadTo(ref frame); if (frame == null) { break; } using (var uFrame = frame.Lock()) { writer.Write(uFrame); } frame.Show(autoSize: true); }while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape)); reader.Dispose(); writer.Dispose(); ImageUI.CloseAll(); }
/// <summary> /// Sets the specified image. /// </summary> /// <param name="image">Image to display.</param> public void SetImage(Bgr<byte>[,] image) { if (bmp == null || bmp.Width != image.Width() || bmp.Height != image.Height()) { bmp = new Bitmap(image.Width(), image.Height(), PixelFormat.Format24bppRgb); } BitmapData bmpData = bmp.Lock(); if (bmpData.BytesPerPixel != image.ColorInfo().Size) { bmpData.Dispose(); bmpData = null; bmp = new Bitmap(image.Width(), image.Height(), PixelFormat.Format24bppRgb); } bmpData = bmpData ?? bmp.Lock(); using (var uIm = image.Lock()) { Copy.UnsafeCopy2D(uIm.ImageData, bmpData.Data, uIm.Stride, bmpData.ScanWidth, uIm.Height); } bmpData.Dispose(); imageView.Image = bmp; if (ScaleForm) ClientSize = new Size(image.Width(), image.Height()); }
static void Main(string[] args) { Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + ";runtimes/win10-x64/"); //only needed if projects are directly referenced var img = new Bgr <byte> [480, 640]; //*********************************************************************************************************************************************************************** Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("********* TColor[,] <=> Image<> conversions (built-in) ****************"); Console.ResetColor(); //to Image<> Image <Bgr <byte> > lockedImg = img.Lock(); //from Image<> var arr = lockedImg.Clone(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("********* Image<,> <=> OpenCV conversions (built-in) ****************"); Console.ResetColor(); //to IplImage IplImage iplImage; using (var uImg = img.Lock()) { iplImage = uImg.AsCvIplImage(); //data is shared } //from IplImage var imgFromIpl = iplImage.AsImage(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("*********** Image<,> <=> Bitmap conversions (BitmapInterop) ****************"); Console.ResetColor(); //to Bitmap var bmp = img.ToBitmap(); //from Bitmap var imgFromBmp = bmp.ToImage <Bgr <byte> >(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("*********** Image<,> <=> BitmapSource conversions (BitmapSourceInterop) ****************"); Console.ResetColor(); //to generic-image var colorBitmap = new BitmapImage(new Uri("http://www.online-image-editor.com//styles/2014/images/example_image.png")); Bgra <byte>[,] colorImg = colorBitmap.ToArray <Bgra <byte> >(); //to BitmapSource Gray <byte>[,] grayImg = img.ToGray(); BitmapSource grayBitmap = grayImg.ToBitmapSource(); }
static void Main(string[] args) { var img = new Bgr <byte> [480, 640]; //*********************************************************************************************************************************************************************** Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("********* TColor[,] <=> Image<> conversions (built-in) ****************"); Console.ResetColor(); //to Image<> Image <Bgr <byte> > lockedImg = img.Lock(); //from Image<> var arr = lockedImg.Clone(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("********* Image<,> <=> OpenCV conversions (built-in) ****************"); Console.ResetColor(); //to IplImage IplImage iplImage; using (var uImg = img.Lock()) { iplImage = uImg.AsOpenCvImage(); //data is shared } //from IplImage var imgFromIpl = iplImage.AsImage(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("*********** Image<,> <=> Bitmap conversions (BitmapInterop) ****************"); Console.ResetColor(); //to Bitmap var bmp = img.ToBitmap(); //from Bitmap var imgFromBmp = bmp.ToArray(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("****** TColor[,] <=> AForge UnmanagedImage conversions (AForgeInterop) *******"); Console.ResetColor(); //to AForge image using (var uImg = img.Lock()) { UnmanagedImage uIm = uImg.AsAForgeImage(); //data is shared } //from AForge image UnmanagedImage aforgeIm = UnmanagedImage.Create(640, 480, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var imgFromAForge = aforgeIm.AsImage(); //TODO: extensions directly to array }
static void Main(string[] args) { var img = new Bgr <byte> [480, 640]; //*********************************************************************************************************************************************************************** Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("********* TColor[,] <=> Image<> conversions (built-in) ****************"); Console.ResetColor(); //to Image<> Image <Bgr <byte> > lockedImg = img.Lock(); //from Image<> var arr = lockedImg.Clone(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("********* Image<,> <=> OpenCV conversions (built-in) ****************"); Console.ResetColor(); //to IplImage IplImage iplImage; using (var uImg = img.Lock()) { iplImage = uImg.AsCvIplImage(); //data is shared } //from IplImage var imgFromIpl = iplImage.AsImage(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("*********** Image<,> <=> Bitmap conversions (BitmapInterop) ****************"); Console.ResetColor(); //to Bitmap var bmp = img.ToBitmap(); //from Bitmap var imgFromBmp = bmp.ToArray(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("*********** Image<,> <=> BitmapSource conversions (BitmapSourceInterop) ****************"); Console.ResetColor(); //to generic-image var colorBitmap = new BitmapImage(new Uri("http://www.online-image-editor.com//styles/2014/images/example_image.png")); Bgra <byte>[,] colorImg = colorBitmap.ToArray <Bgra <byte> >(); //to BitmapSource Gray <byte>[,] grayImg = img.ToGray(); BitmapSource grayBitmap = grayImg.ToBitmapSource(); }
static void Main(string[] args) { var img = new Bgr<byte>[480, 640]; //*********************************************************************************************************************************************************************** Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("********* TColor[,] <=> Image<> conversions (built-in) ****************"); Console.ResetColor(); //to Image<> Image<Bgr<byte>> lockedImg = img.Lock(); //from Image<> var arr = lockedImg.Clone(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("********* Image<,> <=> OpenCV conversions (built-in) ****************"); Console.ResetColor(); //to IplImage IplImage iplImage; using (var uImg = img.Lock()) { iplImage = uImg.AsCvIplImage(); //data is shared } //from IplImage var imgFromIpl = iplImage.AsImage(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("*********** Image<,> <=> Bitmap conversions (BitmapInterop) ****************"); Console.ResetColor(); //to Bitmap var bmp = img.ToBitmap(); //from Bitmap var imgFromBmp = bmp.ToArray(); //*********************************************************************************************************************************************************************** Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("*********** Image<,> <=> BitmapSource conversions (BitmapSourceInterop) ****************"); Console.ResetColor(); //to generic-image var colorBitmap = new BitmapImage(new Uri("http://www.online-image-editor.com//styles/2014/images/example_image.png")); Bgra<byte>[,] colorImg = colorBitmap.ToArray<Bgra<byte>>(); //to BitmapSource Gray<byte>[,] grayImg = img.ToGray(); BitmapSource grayBitmap = grayImg.ToBitmapSource(); }
/// <summary> /// Sets the specified image. /// </summary> /// <param name="image">Image to display.</param> public void SetImage(Bgr<byte>[,] image) { if (bmp == null || bmp.Width != image.Width() || bmp.Height != image.Height()) { bmp = new Bitmap(image.Width(), image.Height(), PixelFormat.Format24bppRgb); } using (BitmapData bmpData = bmp.Lock()) using (var uIm = image.Lock()) { Copy.UnsafeCopy2D(uIm.ImageData, bmpData.Data, uIm.Stride, bmpData.ScanWidth, uIm.Height); } PictureBox.Image = bmp; if (ScaleForm) ClientSize = new Size(image.Width(), image.Height()); }
/// <summary> /// Computes gradient orientations from the color image. Orientation from the channel which has the maximum gradient magnitude is taken as the orientation for a location. /// </summary> /// <param name="frame">Image.</param> /// <param name="magnitudeSqrImage">Squared magnitude image.</param> /// <param name="minValidMagnitude">Minimal valid magnitude.</param> /// <returns>Orientation image (angles are in degrees).</returns> public unsafe static Gray <int>[,] Compute(Bgr <byte>[,] frame, out Gray <int>[,] magnitudeSqrImage, int minValidMagnitude) { var minSqrMagnitude = minValidMagnitude * minValidMagnitude; var orientationImage = new Gray <int> [frame.Height(), frame.Width()]; var _magnitudeSqrImage = orientationImage.CopyBlank(); using (var uFrame = frame.Lock()) { ParallelLauncher.Launch(thread => { computeColor(thread, (byte *)uFrame.ImageData, uFrame.Stride, orientationImage, _magnitudeSqrImage, minSqrMagnitude); }, frame.Width() - 2 * kernelRadius, frame.Height() - 2 * kernelRadius); } magnitudeSqrImage = _magnitudeSqrImage; return(orientationImage); }
void capture_NewFrame(object sender, EventArgs e) { reader.ReadTo(ref frame); if (frame == null) { Application.Idle -= capture_NewFrame; return; } this.pictureBox.Image = frame.ToBitmap(); bool isFrameWritten = writer.Write(frame.Lock()); //TODO: revise if (!isFrameWritten) { MessageBox.Show("Frame is not written!", "Frame writing error", MessageBoxButtons.OK, MessageBoxIcon.Error); } GC.Collect(); }
/// <summary> /// Applies the de-vignetting function to the specified color image. /// </summary> /// <param name="image">Image to correct.</param> public unsafe void Apply(Bgr <byte>[,] image) { using (var uImg = image.Lock()) { Bgr8 *imData = (Bgr8 *)uImg.ImageData; for (int r = 0; r < uImg.Height; r++) { for (int c = 0; c < uImg.Width; c++) { float gain = getGainAt(this, c, r, uImg.Width, uImg.Height); Bgr8 *bgr = &imData[c]; bgr->B = clampToByte((int)(bgr->B * gain)); bgr->G = clampToByte((int)(bgr->G * gain)); bgr->R = clampToByte((int)(bgr->R * gain)); } imData = (Bgr8 *)((byte *)imData + uImg.Stride); } } }