1. Knowledge explanation
1、Cv2.ImWrite
is 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);
filename
is 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.img
is the image to save, typically a objectCv2.ImRead
loaded via or some other methodMat
.
2. The size of the color components can be expressed in different ways, the following are common ones:
-
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.
-
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.
-
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);
(float)mat.Cols - j
:mat.Cols
Converts the value of to a float and subtractsj
the 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.((float)mat.Cols - j) / (float)mat.Cols
: Divide the result obtained in step 1 bymat.Cols
the 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.((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.(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 asrgba.Item1
the 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.