C# OpenCvSharp uses Lab space to change the spring scene to autumn

Table of Contents

Effect

project

code

download


Effect

project

VS2022

.net framework 4.8

OpenCvSharp 4.8

code

Lab color space is a color model that contains three components: brightness (L), red and green channels (a), and blue and yellow channels (b). By converting the image from BGR to Lab, the color information can be separated into two independent channels of brightness and color, which facilitates some image processing tasks.

Core processing code

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);

//
// Summary:
// Converts image from one color space to another
//
// Parameters:
// src:
// The source image, 8-bit unsigned, 16-bit unsigned or single-precision floating-point
//
// dst:
// The destination image; will have the same size and the same depth as src
//
// code:
// The color space conversion code
//
//dstCn:
// The number of channels in the destination image; if the parameter is 0, the number
// of the channels will be derived automatically from src and the code
public static void CvtColor(InputArray src, OutputArray dst, ColorConversionCodes code, int dstCn = 0)

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 = "";

            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 = $"Time-consuming:{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 = "Save";
            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("Save successfully, location: " + sdf.FileName);
            }
        }

    }
}

Download

Source code download

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. OpenCV skill tree Home page Overview 23410 people are learning the system