• 使用OpenCvSharp实现图片从春天转变为秋天效果
  • 发布于 1个月前
  • 79 热度
    0 评论
  • 久拥我i
  • 0 粉丝 43 篇博客
  •   
效果
效果1

效果2

说明
Lab颜色空间是一种颜色模型。它包含了亮度(L)、红绿通道(a)和蓝黄通道(b)三个分量。通过将图像从BGR到Lab的转换,可以将颜色信息分离为亮度和颜色两个独立的通道,方便进行一些图像处理任务。

核心处理代码
Cv2.CvtColor(result_image, result_image, ColorConversionCodes.BGR2Lab);
Mat[] mats = Cv2.Split(result_image);
mats[1] = mats[1] * 0 + 127;
Cv2.Merge(mats, result_image);
Cv2.CvtColor(result_image, result_image, ColorConversionCodes.Lab2BGR);
完整代码
using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
 
namespace OpenCvSharp_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string startupPath;
        string image_path;
 
        Stopwatch stopwatch = new Stopwatch();
 
        Mat image;
        Mat result_image;
 
        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
 
            pictureBox1.Image = null;
            pictureBox2.Image = null;
            textBox1.Text = "";
            // 堆代码 duidaima.com
            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }
 
            stopwatch.Restart();
 
            result_image = image.Clone();
            Cv2.CvtColor(result_image, result_image, ColorConversionCodes.BGR2Lab);
            Mat[] mats = Cv2.Split(result_image);
            mats[1] = mats[1] * 0 + 127;
            Cv2.Merge(mats, result_image);
            Cv2.CvtColor(result_image, result_image, ColorConversionCodes.Lab2BGR);
 
            double costTime = stopwatch.Elapsed.TotalMilliseconds;
 
            textBox1.Text = $"耗时:{costTime:F2}ms";
            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
 
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            if (pictureBox2.Image == null)
            {
                return;
            }
            Bitmap output = new Bitmap(pictureBox2.Image);
            var sdf = new SaveFileDialog();
            sdf.Title = "保存";
            sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";
            if (sdf.ShowDialog() == DialogResult.OK)
            {
                switch (sdf.FilterIndex)
                {
                    case 1:
                        {
                            output.Save(sdf.FileName, ImageFormat.Jpeg);
                            break;
                        }
                    case 2:
                        {
                            output.Save(sdf.FileName, ImageFormat.Png);
                            break;
                        }
                    case 3:
                        {
                            output.Save(sdf.FileName, ImageFormat.Bmp);
                            break;
                        }
                    case 4:
                        {
                            output.Save(sdf.FileName, ImageFormat.Emf);
                            break;
                        }
                    case 5:
                        {
                            output.Save(sdf.FileName, ImageFormat.Exif);
                            break;
                        }
                    case 6:
                        {
                            output.Save(sdf.FileName, ImageFormat.Gif);
                            break;
                        }
                    case 7:
                        {
                            output.Save(sdf.FileName, ImageFormat.Icon);
                            break;
                        }
                    case 8:
                        {
                            output.Save(sdf.FileName, ImageFormat.Tiff);
                            break;
                        }
                    case 9:
                        {
                            output.Save(sdf.FileName, ImageFormat.Wmf);
                            break;
                        }
                }
                MessageBox.Show("保存成功,位置:" + sdf.FileName);
            }
        }
 
    }
}

用户评论