用Mat及Image<Gray, byte>同时打开图片

该篇博客介绍了如何在C#的Windows Forms应用中利用Emgu CV库,分别通过Image<Gray, byte>和Mat数据结构来打开并显示图像文件。在代码示例中,展示了当用户选择图片文件后,如何将图片加载到Image<Gray, byte>对象中并在imageBox1中显示,以及如何直接使用Mat对象加载并显示名为'ball.png'的图片到imageBox2中。" 132804368,11421163,Python网络爬虫实战指南:从基础到高级,"['python', '爬虫', '开发语言', '数据抓取', '网络自动化']
摘要由CSDN通过智能技术生成

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.ML;

 

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

        private void button1_Click(object sender, EventArgs e)
        {

            OpenFileDialog op = new OpenFileDialog();
            if (op.ShowDialog() == DialogResult.OK)
            {
                Image<Gray, byte> image = new Image<Gray, byte>(op.FileName);//图片生成
               
                imageBox1.Image = image;//图片显示

            }
            Mat mt = new Mat("ball.png", Emgu.CV.CvEnum.LoadImageType.AnyColor);
             //图片文件应该放于bin目录下
            imageBox2.Image = mt;//图片显示
          
        }
    }
}

public Point2d RefineSubPixel(Mat image, Point2d lower, Point2d upper) { // 提取感兴趣区域 Rect roiRect = new Rect((int)lower.X, (int)lower.Y, (int)(upper.X - lower.X), (int)(upper.Y - lower.Y)); Mat roi = new Mat(image, roiRect); // 初始化初始点 Point2d refinedPoint = new Point2d(roi.Cols / 2.0, roi.Rows / 2.0); // 定义优化终止标准 var termCriteria = new TermCriteria(CriteriaTypes.MaxIter | CriteriaTypes.Eps, 20, 0.03); // 执行优化迭代 if (roi.Width > 1 && roi.Height > 1) { // 预处理 var grayRoi = new Mat(); Cv2.PyrMeanShiftFiltering(roi, roi, 2, 2); Cv2.CvtColor(roi, grayRoi, ColorConversionCodes.BGR2GRAY); Cv2.Threshold(grayRoi, grayRoi, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu); // 迭代更新点坐标 var delta = new Point2d(); var point = new Point2d(refinedPoint.X, refinedPoint.Y); var bestPoint = new Point2d(refinedPoint.X, refinedPoint.Y); var width = image.Cols; var height = image.Rows; var targetGray = grayRoi.At<byte>((int)point.Y, (int)point.X); var minError = double.MaxValue; var precision = 1e-6; for (int i = 0; i < termCriteria.MaxCount; i++) { int x = (int)Math.Round(point.X); int y = (int)Math.Round(point.Y); if (x <= 0 || y <= 0 || x >= grayRoi.Cols - 1 || y >= grayRoi.Rows - 1) { break; } // 计算当前点周围的梯度信息 var derivX = (grayRoi.At<byte>(y, x + 1) - grayRoi.At<byte>(y, x - 1)) / 2.0; var derivY = (grayRoi.At<byte>(y + 1, x) - grayRoi.At<byte>(y - 1, x)) / 2.0; var hessian = new Mat(2, 2, MatType.CV_64F); hessian.Set<double>(0, 0, grayRoi.At<byte>(y, x + 1) + grayRoi.At<byte>(y, x - 1) - 2 * grayRoi.At<byte>(y, x)); hessian.Set<double>(0, 1, (grayRoi.At<byte>(y + 1, x + 1) - grayRoi.At<byte>(y + 1, x - 1) - grayRoi.At<byte>(y - 1, x + 1) + grayRoi.At<byte>(y - 1, x - 1)) / 4.0); hessian.Set<double>(1, 0, hessian.At<double>(0, 1)); hessian.Set<double>(1, 1, grayRoi.At<byte请完善代码
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值