emgucv之Matrix操作

2020-08-27 13:56:11 浏览数 (1)

主要是解答群友的两个小问题

  1. 获取datagridview的行号和列号
代码语言:javascript复制
 private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            MessageBox.Show("当前行号"   e.RowIndex.ToString()   "当前列号:"  e.ColumnIndex.ToString());
        }

2. 将数组赋值给Matrix并转换为Mat

代码语言:javascript复制
using Emgu.CV;
using System;
using System.Windows.Forms;

namespace WindowsFormsApp7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
           // MessageBox.Show("当前行号"   e.RowIndex.ToString()   "当前列号:"  e.ColumnIndex.ToString());

        }
        public Mat CreateMatFromArr(double[,] arr)
        {
            int height = arr.GetLength(0);
            int width = arr.GetLength(1);
            Matrix<double> mask = new Matrix<double>(height,width);
            mask.Data = arr;
            return mask.Mat;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            double[,] dataArr = { { 100,100,100,100,100,100,100,100,100,100},{100,100,100,100,100,100,100,100,100,100} };
            Mat m = CreateMatFromArr(dataArr);
            pictureBox1.Image = m.Bitmap;
        }
    }
}

运行结果:

函数的参数需要泛型。。。。,修改如下:

代码语言:javascript复制
using Emgu.CV;
using System;
using System.Windows.Forms;

namespace WindowsFormsApp7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
           // MessageBox.Show("当前行号"   e.RowIndex.ToString()   "当前列号:"  e.ColumnIndex.ToString());

        }
        public Mat CreateMatFromArr<T>(T[,] arr)
        {
            int height = arr.GetLength(0);
            int width = arr.GetLength(1);
            Matrix<double> mask = new Matrix<Double>(height, width);
            for (int row = 0;row < height;row   )
            {
                for (int col = 0; col < width; col  )
                mask.Data[row, col] = Convert.ToDouble(arr[row, col]);
            }
            return mask.Mat;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int[,] dataArr = { { 100,100,100,100,100,100,100,100,100,100},{100,100,100,100,100,100,100,100,100,100} };// int,double,byte均可
            Mat m = CreateMatFromArr(dataArr);
            pictureBox1.Image = m.Bitmap;
        }
    }
}

0 人点赞