My favorites | Sign in
Project Logo
                
Search
for
Updated Aug 31, 2009 by schimatk
Labels: Featured, Phase-Support
Samples  
Sample codes

Samples

IplImage ⇔ Bitmap

IplImage ipl = new IplImage("foo.png", LoadMode.Color);

Bitmap bitmap = BitmapConverter.ToBitmap(ipl);
// Bitmap bitmap = ipl.ToBitmap();

IplImage ipl2 = BitmapConverter.ToIplImage(bitmap);

IplImage ⇔ WriteableImage

using OpenCvSharp.Extensions;


IplImage ipl = new IplImage("foo.png", LoadMode.Color);

WriteableBitmap wb = WriteableBitmapConverter.ToWriteableBitmap(ipl, PixelFormats.Bgr24);
//WriteableBitmap wb = ipl.ToWriteableBitmap(PixelFormats.Bgr24);

IplImage ipl2 = WriteableBitmapConverter.ToIplImage(wb);
//IplImage ipl2 = wb.ToIplImage();

Display IplImage in PictureBox

Bitmap bitmap = null;
using(IplImage ipl = new IplImage("bar.png", LoadMode.Color)){
    bitmap = ipl.ToBitmap();
}

Form form = new Form();
form.Text = "Display IplImage in PictureBox";
form.ClientSize = bitmap.Size;

PictureBox pictureBox = new PictureBox();
pictureBox.Dock = DockStyle.Fill;
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox.Image = bitmap;

form.Controls.Add(pictureBox);

form.ShowDialog();

For this purpose, you can also use OpenCvSharp.UserInterface.PictureBoxIpl;

var pictureBox = new OpenCvSharp.UserInterface.PictureBoxIpl();

pictureBox.ImageIpl = new IplImage("bar.png", LoadMode.Color);

How to Access Pixel

slow

IplImage img = new IplImage("baz.png", LoadMode.Color);

for (int y = 0; y < img.Height; y++) {
    for (int x = 0; x < img.Width; x++) {
        CvColor c = img[y, x];
        img[y, x] = new CvColor() {
            B = (byte)Math.Round(c.B * 0.7 + 10),
            G = (byte)Math.Round(c.G * 1.0),
            R = (byte)Math.Round(c.R * 0.0),
        };
    }
}

unsafe

IplImage img = new IplImage("baz.png", LoadMode.Color);

unsafe {
    byte* ptr = (byte*)img.ImageData;
    for (int y = 0; y < img.Height; y++) {
        for (int x = 0; x < img.Width; x++) {
            int offset = (img.WidthStep * y) + (x * 3);
            byte b = ptr[offset + 0];    // B
            byte g = ptr[offset + 1];    // G
            byte r = ptr[offset + 2];    // R
            ptr[offset + 0] = r;
            ptr[offset + 1] = g;
            ptr[offset + 2] = b;
        }
    }
}

Marshal.ReadByte/WriteByte

IplImage img = new IplImage("baz.png", LoadMode.Color);
IntPtr ptr = img.ImageData;

for (int x = 0; x < image.Width; x++) {
    for (int y = 0; y < image.Height; y++) {
        int offset = (image.WidthStep * y) + (x * 3);
        byte b = Marshal.ReadByte(ptr, offset + 0);    // B
        byte g = Marshal.ReadByte(ptr, offset + 1);    // G
        byte r = Marshal.ReadByte(ptr, offset + 2);    // R
        Marshal.WriteByte(ptr, offset, r);
        Marshal.WriteByte(ptr, offset, g);
        Marshal.WriteByte(ptr, offset, b);
    }
}

Comment by olof.bjarnason, Nov 27, 2008

IplImage? ipl = new IplImage?("foo.png", LoadMode?.Color);

Why do you have to specify a file name "foo.png"?


Sign in to add a comment
Hosted by Google Code