• C# OpenCvSharp 利用Lab空间把春天的场景改为秋天


    目录

    效果

    项目

    代码

    下载


    效果

    项目

    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)

    1. using OpenCvSharp;
    2. using System;
    3. using System.Diagnostics;
    4. using System.Drawing;
    5. using System.Drawing.Imaging;
    6. using System.Windows.Forms;
    7. namespace OpenCvSharp_Demo
    8. {
    9. public partial class Form1 : Form
    10. {
    11. public Form1()
    12. {
    13. InitializeComponent();
    14. }
    15. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    16. string startupPath;
    17. string image_path;
    18. Stopwatch stopwatch = new Stopwatch();
    19. Mat image;
    20. Mat result_image;
    21. private void Form1_Load(object sender, EventArgs e)
    22. {
    23. startupPath = System.Windows.Forms.Application.StartupPath;
    24. }
    25. private void button1_Click(object sender, EventArgs e)
    26. {
    27. OpenFileDialog ofd = new OpenFileDialog();
    28. ofd.Filter = fileFilter;
    29. if (ofd.ShowDialog() != DialogResult.OK) return;
    30. pictureBox1.Image = null;
    31. pictureBox2.Image = null;
    32. textBox1.Text = "";
    33. image_path = ofd.FileName;
    34. pictureBox1.Image = new Bitmap(image_path);
    35. image = new Mat(image_path);
    36. }
    37. private void button2_Click(object sender, EventArgs e)
    38. {
    39. if (image_path == "")
    40. {
    41. return;
    42. }
    43. stopwatch.Restart();
    44. result_image = image.Clone();
    45. Cv2.CvtColor(result_image, result_image, ColorConversionCodes.BGR2Lab);
    46. Mat[] mats = Cv2.Split(result_image);
    47. mats[1] = mats[1] * 0 + 127;
    48. Cv2.Merge(mats, result_image);
    49. Cv2.CvtColor(result_image, result_image, ColorConversionCodes.Lab2BGR);
    50. double costTime = stopwatch.Elapsed.TotalMilliseconds;
    51. textBox1.Text = $"耗时:{costTime:F2}ms";
    52. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
    53. }
    54. private void button3_Click(object sender, EventArgs e)
    55. {
    56. if (pictureBox2.Image == null)
    57. {
    58. return;
    59. }
    60. Bitmap output = new Bitmap(pictureBox2.Image);
    61. var sdf = new SaveFileDialog();
    62. sdf.Title = "保存";
    63. 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";
    64. if (sdf.ShowDialog() == DialogResult.OK)
    65. {
    66. switch (sdf.FilterIndex)
    67. {
    68. case 1:
    69. {
    70. output.Save(sdf.FileName, ImageFormat.Jpeg);
    71. break;
    72. }
    73. case 2:
    74. {
    75. output.Save(sdf.FileName, ImageFormat.Png);
    76. break;
    77. }
    78. case 3:
    79. {
    80. output.Save(sdf.FileName, ImageFormat.Bmp);
    81. break;
    82. }
    83. case 4:
    84. {
    85. output.Save(sdf.FileName, ImageFormat.Emf);
    86. break;
    87. }
    88. case 5:
    89. {
    90. output.Save(sdf.FileName, ImageFormat.Exif);
    91. break;
    92. }
    93. case 6:
    94. {
    95. output.Save(sdf.FileName, ImageFormat.Gif);
    96. break;
    97. }
    98. case 7:
    99. {
    100. output.Save(sdf.FileName, ImageFormat.Icon);
    101. break;
    102. }
    103. case 8:
    104. {
    105. output.Save(sdf.FileName, ImageFormat.Tiff);
    106. break;
    107. }
    108. case 9:
    109. {
    110. output.Save(sdf.FileName, ImageFormat.Wmf);
    111. break;
    112. }
    113. }
    114. MessageBox.Show("保存成功,位置:" + sdf.FileName);
    115. }
    116. }
    117. }
    118. }

    下载

    源码下载

  • 相关阅读:
    远程linux机器中使用camera
    LeetCode --- 1523. Count Odd Numbers in an Interval Range 解题报告
    tcpdump进行ARP抓包
    【求助】基于Ascend910的MindSpore训练无法复现GPU上的模型效果
    批量提取图片文件名使用Python中os.listdir( )函数时出现文件名乱序,非预想排序的解决方法
    如果再爱一次 你还会选我吗
    uniapp da-tree插件 代码和结构分析
    Rust6.1 Writing Automated Tests
    vscode ssh远程连接服务器的重置以及openssh
    JavaScript 基础 - 第1天笔记
  • 原文地址:https://blog.csdn.net/lw112190/article/details/133857366