C#&图像:1.图像区域分割与提取

(1)创建一个名为SplitImage的窗体的应用程序,将窗体改名为FormSplitImage。


(2)创建一个名为ImageProcessingLibrary的类库程序,为该工程添加名为ImageProcessing的静态类

(3)为ImageProcessing类添加统计直方图的静态函数

(4)在ImageProcessing类中添加二值化处理函数BinaryImage

(5)在SplitImage工程中引用ImageProcessingLibrary工程,并添加ImageProcessingLibrary, System.Drawing命名空间。
(6)在窗体中重写OnPaint事件函数,并在函数中添加绘制原始图像、显示直方图和图像分割与提取后的图像

程序框架 :

 被窗体的应用程序引用的类库代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ImageProcessingLibrary
{
    public static class ImageProcessing
    {/// 获取直方图数组,并绘制直方图
     /// <param name="image">需要处理的图像</param>
     /// <param name="indexColor">处理的颜色索引值,Blue:0,Green:1,Red:2</param>
     /// <param name="histogram">直方图统计数组</param>
     /// <returns>绘制好的直方图</returns>
        public static Bitmap GetHistogram(Bitmap image, int indexColor, out int[] histogram)
        {
            histogram = new int[256];                               //直方图统计数组
            BitmapData data = image.LockBits(new Rectangle(new Point(), image.Size),
             ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);  //将图像锁定到内存中
            byte[] datas = new byte[data.Stride * image.Height];   //图像数组
            Marshal.Copy(data.Scan0, datas, 0, datas.Length);     //将图像在内存中的数据复制到图像数组中
            for (int y = 0; y < image.Height * data.Stride; y += data.Stride) //data.Stride代表图像一行数据的字节总数/步长为data.Stride
            {//外层循环是遍历行

                   for (int x = 0; x < image.Width * 3; x += 3)//遍历当前行中的每个像素/每个像素由三个字节(RGB)组成
                           //每个颜色分量(红色、绿色或蓝色)可以有的不同强度级别就是2^8,即256个级别

                {
                    int index = y + x;                     //颜色在内存中的索引/每个索引偏移量3字节(对应R,G,B)
                    histogram[datas[index + indexColor]]++;//增加直方图中对应颜色分量出现的次数
                }
            }
            image.UnlockBits(data);
            byte maxValue = 0;                             //直方图中的最大值
            for (int value = 1; value < 256; value++)
            {
                if (histogram[value] > histogram[maxValue]) maxValue = (byte)value;
            }
        
        Bitmap imageHistogram = new Bitmap(256, 256);
        Graphics GHistogram = Graphics.FromImage(imageHistogram);
        GHistogram.Clear(Color.Blue);
         for (int value = 1; value< 256; value++)
         {
             int length = byte.MaxValue * histogram[value] / histogram[maxValue];
        GHistogram.DrawLine(new Pen(Color.FromArgb(value, value, value), 1f), value,
                  256, value, 256 - length);                            //绘制直方图
         }
    Font font = new Font("宋体", 9f);
            //绘制统计标识
            for (int value = 32; value < 256; value += 32)
            {
                int count = histogram[maxValue] / 8 * value / 32;
                Pen pen = new Pen(Color.Lime);
                pen.DashStyle = DashStyle.DashDot;
                SizeF sizeCount = GHistogram.MeasureString(count.ToString(), font);
                GHistogram.DrawLine(pen, 0, 255 - value, 255, 255 - value);//绘制数量等级线
                GHistogram.DrawString(count.ToString(), font, Brushes.Red, 5, 255 - value - sizeCount.Height / 2);

               SizeF sizeValue = GHistogram.MeasureString(value.ToString(), font);
                GHistogram.DrawLine(Pens.Red, value, 250, value, 255);//绘制颜色值等级线
                GHistogram.DrawString(value.ToString(), font, Brushes.Red, value - sizeValue.Width / 2, 240);
            }
            font.Dispose();
            return imageHistogram;
        }

        /// 将图像进行二值化处理
        /// <param name="image">需要处理的图像</param>
        /// <param name="indexColor">处理的颜色索引值,Blue:0,Green:1,Red:2</param>
        /// <param name="thresholdMin">阈值下限</param>
        /// <param name="thresholdMax">阈值上限</param>
        public static void BinaryImage(Bitmap image, int indexColor, int thresholdMin, int thresholdMax)
        {
            //将图像锁定到内存中
            BitmapData data = image.LockBits(new Rectangle(new Point(), image.Size), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            byte[] datas = new byte[data.Stride * image.Height];           //图像数组
            Marshal.Copy(data.Scan0, datas, 0, datas.Length);              //将图像在内存中的数据复制到图像数组中
            for (int y = 0; y < image.Height * data.Stride; y += data.Stride)
            {
                for (int x = 0; x < image.Width * 3; x += 3)
                {
                    int index = y + x;
                    //根据阈值将图像分成黑色和白色,其中阈值内的为黑色,阈值外的为白色
                    if (datas[index + indexColor] >= thresholdMin && datas[index + indexColor] <= thresholdMax)
                        datas[index] = datas[index + 1] = datas[index + 2] = 0;
                    else
                        datas[index] = datas[index + 1] = datas[index + 2] = 255;
                }
            }
            Marshal.Copy(datas, 0, data.Scan0, datas.Length);      //将图像数组复制到内存中
            image.UnlockBits(data);                                //将图像从内存中解锁
        }

    }
            }
/*假设颜色分量是8位的,那么每个颜色分量(红色、绿色或蓝色)可以有的不同强度级别就是2^8,即256个级别。
 * 这是因为8位可以表示从0到255的整数,总共256个不同的数值。在数字图像处理中,8位颜色深度是常见的,
 * 因为它提供了足够的动态范围来表示大多数自然和人工颜色的细微差别,同时保持数据量相对较小。

当你说“直方图大小为256”时,你指的是直方图的横坐标(即颜色强度的可能值)有256个不同的条目,
每个条目对应一个特定的颜色强度值(从0到255)。直方图的纵坐标通常表示该颜色强度值在图像中出现的频率或像素数量。

因此,如果我们想为8位颜色分量的图像构建直方图,我们将创建一个大小为256的数组,数组的每个元素初始化为0。
然后,我们遍历图像的每个像素,对于每个像素的特定颜色分量(如红色、绿色或蓝色),我们增加直方图中对应颜色强度值的计数。
这个过程最终会给我们一个表示图像中每个颜色强度出现频率的直方图。

*/

窗体的应用程序,重写OnPaint事件函数代码:

using ImageProcessingLibrary;
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;

namespace SplitImage
{
    public partial class FormSplitImage : Form
    {
        public FormSplitImage()
        {
            InitializeComponent();
        }
        protected override void OnPaint(PaintEventArgs e)//重写OnPaint事件函数
        {
            Graphics G = e.Graphics;

            Bitmap image = new Bitmap("123456.jpg");                       //加载图像
            Rectangle rectImage = new Rectangle(new Point(), image.Size);
            G.DrawImage(image, rectImage);                                   //绘制原始图像
            int[] histogram;                                                   //直方图统计数组
            Rectangle rectHistogram = new Rectangle(rectImage.Width, 0, 256, 256); //获取图像的灰度直方图(起始点X,Y,像素大小x,y)

            Bitmap imageHistogram = ImageProcessing.GetHistogram(image, 0, out histogram);//这里out返回了直方图数组histogram
            G.DrawImage(imageHistogram, rectHistogram);                        //绘制直方图
            rectImage.Offset(0, image.Height);//矩形位置调整指定的量,即往下(y)移一个图片高度,定义了绘制分割后的图像的rectImage
            ImageProcessing.BinaryImage(image, 1, 0, 150);                     //通过二值化将目标分割出来()
            G.DrawImage(image, rectImage);                                     //绘制分割后的图像
            image.Dispose();                                                   //释放图像
            imageHistogram.Dispose();                                          //释放直方图图像


        }

       
    }
}

     在程序路径下准备图片:123456.jpg 

 运行SplitImage窗体的应用程序:

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值