目录
VS2022
.net framework 4.8
OpenCvSharp 4.8
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);
//
// 摘要:
// Converts image from one color space to another
//
// 参数:
// 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 = $"耗时:{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);
- }
- }
-
- }
- }