• C# OpenCvSharp 颜色反转


    目录

    效果

    灰度图

    黑白色反转

    彩色反转

    项目

    代码

    下载 


    效果

    灰度图

    黑白色反转

    彩色反转

    项目

    代码

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. using OpenCvSharp;
    10. using OpenCvSharp.Extensions;
    11. namespace OpenCvSharp_颜色反转
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. }
    19. private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    20. Bitmap bmp;
    21. String imgPath = "";
    22. private void button2_Click(object sender, EventArgs e)
    23. {
    24. OpenFileDialog ofd = new OpenFileDialog();
    25. ofd.Filter = fileFilter;
    26. if (ofd.ShowDialog() != DialogResult.OK) return;
    27. imgPath = ofd.FileName;
    28. bmp = new Bitmap(imgPath);
    29. pictureBox1.Image = bmp;
    30. }
    31. private void button1_Click(object sender, EventArgs e)
    32. {
    33. if (imgPath == "")
    34. {
    35. return;
    36. }
    37. Mat mat = new Mat(imgPath);
    38. Cv2.CvtColor(mat, mat, ColorConversionCodes.BGR2GRAY);
    39. Mat dst = new Mat(mat.Height, mat.Width, mat.Type(), Scalar.White);
    40. byte grayPixel = 0;
    41. for (int r = 0; r < dst.Rows; r++)
    42. {
    43. for (int c = 0; c < dst.Cols; c++)
    44. {
    45. grayPixel = mat.At<byte>(r, c);
    46. dst.Set<byte>(r, c, (byte)(255 - grayPixel));
    47. }
    48. }
    49. if (pictureBox2.Image != null)
    50. {
    51. pictureBox2.Image.Dispose();
    52. }
    53. pictureBox2.Image = BitmapConverter.ToBitmap(dst);
    54. }
    55. private void button4_Click(object sender, EventArgs e)
    56. {
    57. if (imgPath == "")
    58. {
    59. return;
    60. }
    61. Mat mat = new Mat(imgPath);
    62. Cv2.CvtColor(mat, mat, ColorConversionCodes.BGR2GRAY);
    63. if (pictureBox2.Image != null)
    64. {
    65. pictureBox2.Image.Dispose();
    66. }
    67. pictureBox2.Image = BitmapConverter.ToBitmap(mat);
    68. }
    69. private void button3_Click(object sender, EventArgs e)
    70. {
    71. if (imgPath == "")
    72. {
    73. return;
    74. }
    75. Mat mat = new Mat(imgPath);
    76. Mat dst = new Mat(mat.Height, mat.Width, mat.Type(), Scalar.White);
    77. Vec3b vec3B;
    78. for (int r = 0; r < dst.Rows; r++)
    79. {
    80. for (int c = 0; c < dst.Cols; c++)
    81. {
    82. vec3B = mat.At(r, c);
    83. vec3B.Item0 = (byte)(255 - vec3B.Item0);
    84. vec3B.Item1 = (byte)(255 - vec3B.Item1);
    85. vec3B.Item2 = (byte)(255 - vec3B.Item2);
    86. dst.Set(r, c, vec3B);
    87. }
    88. }
    89. if (pictureBox2.Image != null)
    90. {
    91. pictureBox2.Image.Dispose();
    92. }
    93. pictureBox2.Image = BitmapConverter.ToBitmap(dst);
    94. }
    95. }
    96. }

    下载 

    Demo下载

  • 相关阅读:
    freeswitch的mod_curl模块
    SpringCloud与SpringBoot的版本对应
    基于javaweb谣言平台系统
    记src一处Juniper远程命令执行
    游戏复用列表实现思路
    [Full Clock 技术复盘] 一、浏览器前端如何实现百毫秒级时间校准?时间 API 推荐、模拟 NTP 算法原理及局限
    kotlin入门学习文档
    面试题 | 1. 说一下http和https
    爬虫基础入门
    线性回归之随机梯度下降法(Stochastic Gradient Descent,SGD)
  • 原文地址:https://blog.csdn.net/weixin_46771779/article/details/136246964