.NET CORE ImageSharp 图像二值化

最近做到电子价签显示用的图像处理,电子价签的屏幕只能显示黑白红三色,这就需要对原始图像进行二值化,再取红色点,两层图层叠加后传输给电子价签显示。下图为处理效果,具体还可以通过代码调整阈值来调整效果。

.NET CORE ImageSharp 图像二值化_第1张图片

代码如下:

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.Primitives;
using System;

namespace ImageConvterTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int imgindex = 3;//图像编号
            var fImageTemp = Image.Load(System.IO.File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + $"/img/{imgindex}.jpg"));
            var fBImg = Binaryzation(fImageTemp);
            fBImg.Save(AppDomain.CurrentDomain.BaseDirectory + $"/img/{imgindex}Binaryzation.jpg");
            var fImageTemp2 = Image.Load(System.IO.File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + $"/img/{imgindex}.jpg"));
            //红色图像提取
            var fBImgColor = BinaryzationRed(fImageTemp2, 120, 70, 70);
            fBImgColor.Save(AppDomain.CurrentDomain.BaseDirectory + $"/img/{imgindex}Red.jpg");
            //合成图像
            fBImg.Mutate(p =>
            {
                p.DrawImage(fBImgColor, new Point(0, 0), 1);
            });
            fBImg.Save(AppDomain.CurrentDomain.BaseDirectory + $"/img/{imgindex}Final.jpg");
        }

        /// 
        /// 黑白二值化
        /// 
        /// 
        /// 
        public static Image Binaryzation(Image image)
        {
            image = ToGray(image);//先灰度处理
            int threshold = 180;//定义阈值
            for (int i = 0; i < image.Width; i++)
            {
                for (int j = 0; j < image.Height; j++)
                {
                    //获取该像素点的RGB的颜色
                    var color = image[i, j];
                    //计算颜色,大于平均值为黑,小于平均值为白
                    System.Drawing.Color newColor = color.B < threshold ? System.Drawing.Color.FromArgb(0, 0, 0) : System.Drawing.Color.FromArgb(255, 255, 255);
                    //修改该像素点的RGB的颜色
                    image[i, j] =new Rgba32(newColor.R, newColor.G, newColor.B, newColor.A);
                }
            }
            return image;
        }

        /// 
        /// 图像灰度处理
        /// 
        /// 
        /// 
        public static Image ToGray(Image img)
        {
            for (int i = 0; i < img.Width; i++)
            {
                for (int j = 0; j < img.Height; j++)
                {
                    var color = img[i, j];
                    //计算灰度值
                    int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
                    System.Drawing.Color newColor = System.Drawing.Color.FromArgb(gray, gray, gray);
                    //修改该像素点的RGB的颜色
                    img[i, j] = new Rgba32(newColor.R, newColor.G, newColor.B, newColor.A);
                }
            }
            return img;
        }

        /// 
        /// 图像红色处理
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static Image BinaryzationRed(Image image,int r,int g,int b)
        {
            for (int i = 0; i < image.Width; i++)
            {
                for (int j = 0; j < image.Height; j++)
                {
                    //获取该像素点的RGB的颜色
                    var color = image[i, j];
                    //计算颜色,大于平均值为黑,小于平均值为白
                    if (color.R >= r && color.G<=g&& color.B<=b)
                    { //修改该像素点的RGB的颜色
                        image[i, j] = Rgba32.Red;
                    }
                    else
                    {
                        image[i, j] = Rgba32.Transparent;
                    }
                }
            }
            return image;
        }
    }
}

 

你可能感兴趣的:(C#.NET,.net,core,ImageSharp)