前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >emgucv之Matrix操作

emgucv之Matrix操作

作者头像
zls365
发布2020-08-27 13:56:11
6730
发布2020-08-27 13:56:11
举报
文章被收录于专栏:CSharp编程大全

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

  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;
        }
    }
}

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-08-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 CSharp编程大全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档