幾種常見方式:
1. 指標方式:IntPtr
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() == DialogResult.OK)
{
IntPtr inputImage = CvInvoke.cvLoadImage(ofd.FileName, LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_COLOR);
CvInvoke.cvShowImage("IntPtr", inputImage);
}
}
=======================================================================
2. EmguCV 影像格式 Image<Bgr, Byte>: a wrapper to IplImage of OpenCV
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() == DialogResult.OK)
{
Image<Bgr, Byte> inputImage = new Image<Bgr, byte>(ofd.FileName);
CvInvoke.cvShowImage("Image<Bgr, Byte>", inputImage);
}
}
=======================================================================
3. EmguCV 影像格式 Image<Gray, Byte>: 宣告一個EmguCV灰階影像格式
如果輸入是一張彩色照片, 但是卻用一塊灰階的EmguCV資料型態去接收會發生甚麼事情呢?
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() == DialogResult.OK)
{
Image<Gray, Byte> inputImage = new Image<Gray, byte>(ofd.FileName);
CvInvoke.cvShowImage("Image<Gray, Byte>", inputImage);
}
}
內建判斷, 直接自動將輸入彩色的照片轉成灰階, 酷吧!!!
=======================================================================
4. 輸入PictureBox 讀入影像, 將PictureBox.Image轉型Bitmpap丟給Image<Bgr, Byte>
語法: public Image(Bitmap bmp)
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() == DialogResult.OK)
{
pictureBox1.Load(ofd.FileName);
Image<Bgr, Byte> inputImage = new Image<Bgr, Byte>((Bitmap)(pictureBox1.Image));
CvInvoke.cvShowImage("Image<Bgr, Byte>", inputImage);
}
}
=======================================================================
5. 開啟一張空的影像(黑底), 大小為640X480
語法: Image(Size size)
Image<Gray, Byte> inputImage = new Image<Gray, byte>(new Size(640,480));
pictureBox1.Image = inputImage.ToBitmap();
=======================================================================
6. 自己定義一塊影像大小和像素值
語法:Image(int width, int height, TColor value)
Image<Bgr, Byte> inputImage = new Image<Bgr, byte>(640, 480, new Bgr(255,0,255));
pictureBox1.Image = inputImage.ToBitmap();
=======================================================================
7. 利用Bitmap讀入一張圖片, 並傳給Image<Bgr, Byte>影像格式
語法:Image(int width, int height, int stride, IntPtr scan0)
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() == DialogResult.OK)
{
Bitmap bmp = new Bitmap(ofd.FileName);
System.Drawing.Imaging.BitmapData bd;
bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Image<Bgr, Byte> inputImage = new Image<Bgr, Byte>(bd.Width, bd.Height, bd.Stride, bd.Scan0);
pictureBox1.Image = inputImage.ToBitmap();
bmp.UnlockBits(bd);
}
}
範例程式: EmguVS2008LoadImage
延伸閱讀:
Emgu CV: 基礎影像容器Mat
Emgu CV: Mask遮罩應用
Emgu CV: 影像形態學應用
Emgu CV: 如何有效取得像素值?
參考資料:
留言列表