OpenCvSharp_use imwrite to generate transparent png image (with source code)

1. Knowledge explanation

1、Cv2.ImWriteis a function in the OpenCV library used to save an image as a file. It accepts two parameters: the file path and the image to save.

Cv2.ImWrite(string filename, Mat img);
  • filenameis the file path to save the image, it can be a relative path or an absolute path. Filename extensions can be specified for common image formats such as ".jpg", ".png", etc.
  • imgis the image to save, typically a object Cv2.ImReadloaded via or some other method Mat.

2. The size of the color components can be expressed in different ways, the following are common ones:

  1. 8-bit integer representation (0-255): In 8-bit integer representation, the value range of color components is 0 to 255. Among them, 0 means minimum intensity or no color, and 255 means maximum intensity or full intensity color. This is the most common representation and the one used in most image processing libraries and software.

  2. Hexadecimal representation (0x00-0xFF): In hexadecimal representation, the value range of color components is from 00 to FF. Each hexadecimal number corresponds to an 8-bit binary number (00000000 to 11111111), thus representing the same range of values. This representation is often used in programming languages, for example to represent color values ​​in code.

  3. Floating point number representation (0.0-1.0): In floating point number representation, the color component ranges from 0.0 to 1.0. 0.0 represents minimum intensity or no color, and 1.0 represents maximum intensity or full intensity color. This representation is commonly used in some image processing libraries and software, especially in computer graphics.

 2. Calculation explanation

rgba.Item1 = (byte)(((float)mat.Cols - j) / (float)mat.Cols * 0xff);
  1. (float)mat.Cols - j: mat.ColsConverts the value of to a float and subtracts jthe value from it. The result calculated in this step is the position or distance of the current column relative to the width of the image.
  2. ((float)mat.Cols - j) / (float)mat.Cols: Divide the result obtained in step 1 by mat.Colsthe value of . The result calculated in this step represents the relative position or distance of the current column relative to the entire image width. The result range is between 0 and 1.
  3. ((float)mat.Cols - j) / (float)mat.Cols * 0xff: Multiply the result obtained in step 2 by 0xff, which is 255 in hexadecimal. This step maps the relative position or distance to the range 0 to 255 to get the value of the color component.
  4. (byte)(((float)mat.Cols - j) / (float)mat.Cols * 0xff): Convert the floating-point result obtained in step 3 to a byte type, that is, truncate it to an integer part and convert it to a byte. The final value will be used as rgba.Item1the value of .

To sum up, this calculation process is to calculate the value of the color component according to the position or distance of the current column relative to the entire image width. The resulting value is converted to a byte type to meet the storage requirements of the image data and maintain data consistency.

process:

1. Create a form project and add a button button to the form

The source code is as follows

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Mat createAlphaMat()
        {
            Mat mat = new Mat(480, 640, MatType.CV_8UC4);
            for (int i = 0; i < mat.Rows; ++i)
            {
                for (int j = 0; j < mat.Cols; ++j)
                {
                     //创建一个 Vec4b 对象,用于存储每个像素的颜色和透明度值。
                    var rgba = new Vec4b();
                    // 蓝色,颜色分量的取值范围为00到ff
                    rgba.Item0 = 0xff;
                    // 绿色
                    rgba.Item1 = (byte)(((float)mat.Cols - j) / (float)mat.Cols * 0xff);
                    // 红色
                    rgba.Item2 = (byte)(((float)mat.Rows - i) / (float)mat.Rows * 0xff);
                    // 透明度
                    rgba.Item3 = (byte)((float)0.5 * (float)(rgba[1] + rgba[2]));
                    // 设置
                    mat.Set(i, j, rgba);
                }
            }
            return mat;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Mat srcImage = createAlphaMat();
            Bitmap map = BitmapConverter.ToBitmap(srcImage);
            pictureBox1.Image = map;

            Cv2.ImWrite("透明Alpha值图.png", srcImage);
        }
    }
}

This article only focuses on the button that generates png images.

 Reference article: (Lecture 2 Generate PNG images Xiaozhai Blog Network (bilibili996.com) http://www.bilibili996.com/Course?id=4324175000003http://www.bilibili996.com/Course?id=4324175000003 http:/ /www.bilibili996.com/Course?id=4324175000003

Guess you like

Origin blog.csdn.net/m0_55074196/article/details/131680015