Emgu 学习笔记(二)---图像二值化,自适应阈值化,Otsu二值化

本文介绍了在Emgu库中进行图像二值化和自适应阈值化的操作,包括Threshold()函数的使用以及AdaptiveThreshold()函数的参数解释。通过代码示例展示了如何实现不同类型的阈值处理,如MeanC和GaussianC方法,以适应图像不同区域的阈值需求。最终呈现了处理效果。

图像二值化,自适应阈值化,Otsu二值化

Emgu灰度化、二值化操作方法和OpenCV中区别不大,Threshold()来实现的。

自适应阈值是整幅图像使用一个阈值,自适应阈值是图像的不同区域使用不同的阈值,而这个阈值是对整个区域计算出来的。在Emgu中也是调用函数AdaptiveThreshold()来实现的。

public static AdaptiveThreshold(IInputArray src,IOutputArray dst,double maxValue,AdaptiveThresholdType adativeType,ThresholdType thresholdType,int blockSize,double param1)

AdaptiveThresholdType adativeType:自适应阈值计算方法:MeanC和GaussianC

ThresholdType thresholdType:必须为二值化阈值化或者是反二值化阈值化(Binary/BinaryInv)

int blockSize:计算使用的区域矩阵大小:3,5,7,9


来看代码实现:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.CvEnum;
namespace 全局阈值
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Image<Bgr, Byte> image = new Image<Bgr, Byte>(Properties.Resources._2);
            pictureBox1.Image = image.ToBitmap();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            using (var image = new Image<Bgr, Byte>(Properties.Resources._2))
            {
                var grayImage = image.Convert<Gray, Byte>();
               
                pictureBox1.Image = grayImage.ToBitmap();
               

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Image<Bgr, Byte> image = new Image<Bgr, Byte>(Properties.Resources._2);
            var grayImage = image.Convert<Gray, Byte>();
            var threshImage = grayImage.CopyBlank();
            CvInvoke.Threshold(grayImage, threshImage, 150, 255, ThresholdType.Binary);
            pictureBox2.Image = threshImage.ToBitmap();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var grayImage = new Image<Gray, Byte>(Properties.Resources._2);
            var threshImage = grayImage.ThresholdAdaptive(new Gray(255), AdaptiveThresholdType.MeanC, ThresholdType.Binary, 9, new Gray(5));
            pictureBox3.Image = threshImage.ToBitmap();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var grayImage = new Image<Gray, Byte>(Properties.Resources._2);
            var threshImge = grayImage.CopyBlank();
            CvInvoke.Threshold(grayImage, threshImge, 0, 255, ThresholdType.Otsu);
            pictureBox4.Image = threshImge.ToBitmap();
        }
    }
}


效果图:

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值