利用EmguCV的函式將影像圖片轉成資料列,透過DataGridView顯示影像8位元(R,G,B)資料列。
引用類別
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using Emgu.CV.Util;
using System.Runtime.InteropServices;
using System.Diagnostics;
使用控制項
dataGridView
ImageBox(Emgu.CV)
程式碼
private void button1_Click(object sender, EventArgs e)
{
imgOpen();
}
private void imgOpen()
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Image<Bgr, byte> img;
img = new Image<Bgr, byte>(ofd.FileName);
imageBox1.Image = img;
DispMCvMatInfo(img.Mat, dataGridView1, img.Rows, img.Cols);
}
}
}
private void DispMCvMatInfo(Mat img, DataGridView dgv, int rows, int cols)
{
int i, j;
unsafe
{
MCvMat mImg = (MCvMat)Marshal.PtrToStructure(img.Ptr, typeof(MCvMat));
byte* ptr = (byte*)mImg.Data.ToPointer();
byte Red, Green, Blue;
for (i = 0; i < cols; i++)
{
dataGridView1.Columns.Add(i.ToString(), i.ToString());
}
for (i = 0; i < cols; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].HeaderCell.Value = i.ToString();
for (j =0; j < rows; j++)
{
Blue = ptr[0];
ptr++;
Green = ptr[0];
ptr++;
Red = ptr[0];
dgv[j,i].Value=string.Format("({0},{1},{2})", Red, Green, Blue);
ptr++;
}
}
}
}
使用unsafevm 需在[屬性]->[建置]裡面將"容許不安全的程式碼"打勾
留言列表