C#图像处理1------二值化

本文介绍了如何使用C#的Bitmap类实现图像二值化算法,通过THRESH_BINARY方法将图像转换为黑白,着重于基础操作和阈值处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近用到的C#比较多,所以将以前自己写的一些图像算法写成个教程系列。。这里主要使用Bitmap进行图像算法编写,主要加深自己对算法的理解。实际用到工业中 大家还是基于opencv或者其他一些视觉库。

图像二值化的概念就不细说了,现在使用C#实现THRESH_BINARY二值化算法。原理就是大于设定得值就设置为最大值。

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;

namespace 图像_二值化
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Bitmap bt;
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                bt = new Bitmap(ofd.FileName);
                pictureBox1.Image = bt;
            }

            Color p1;
            Bitmap bt1 = bt.Clone() as Bitmap;
            for (int x1 = 0; x1 < bt.Width; x1++)
            {
                for (int x2 = 0; x2 < bt.Height; x2++)
                {
                    p1 = bt.GetPixel(x1, x2);
                    int temp = (p1.R + p1.B + p1.G) / 3;
                    bt1.SetPixel(x1, x2, Color.FromArgb(temp, temp, temp));
                }
            }

            Color p2;
            for (int x1 = 0; x1 < bt1.Width; x1++)
            {
                for (int x2 = 0; x2 < bt1.Height; x2++)
                {
                    p2 = bt1.GetPixel(x1, x2);
                    if (p2.G > 150)
                    {
                        bt1.SetPixel(x1, x2, Color.FromArgb(255, 255, 255));
                    }
                    else
                    {
                        bt1.SetPixel(x1, x2, Color.FromArgb(0, 0, 0));
                    }
                }
            }


            pictureBox2.Image = bt1;


        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mrs.Gril

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值