当前位置:网站首页>[opencv] generate transparent PNG image
[opencv] generate transparent PNG image
2022-07-28 08:45:00 【sinat_ forty-one million seven hundred and fifty-two thousand t】
This article explains how to use opencv Of cv::imwrite() Function generates transparency / Opaque PNG Images :
1. PNG A brief introduction to the image
2. cv::imwrite() Function introduction
3. Sample code
1. PNG A brief introduction to the image
PNG yes 20 century 90 A replacement developed in the s GIF and TIFF Image file storage format , Portable image file format suitable for network transmission .
- PNG Images have streaming readability , Allows continuous reading and writing of image data , Therefore, it is suitable for network communication .
- PNG The image can be approximated and displayed step by step , This feature enables the image to be displayed on the terminal while transmitting the image file on the communication link , After displaying the whole outline, gradually display the details of the image .
- PNG Images can be transparent , Some parts of the image can not be displayed , Used to create some characteristic images .
- PNG Image is independent of computer hardware and software environment , Using lossless compression , You can store multiple images in one file .
So many features above , What is used in this article is PNG Transparency .
PNG File structure
PNG Image format consists of file signature and data block .
PNG The signature of the document is 8 byte , Used to identify PNG Format , be-all PNG There is a file signature at the beginning of the image file . this 8 Bytes are :89H50H 4EH 47H0DH 0AH1AH0AH
- 89H: Used to detect whether the transmission system supports 8 Bit character encoding , It is used to reduce the text file being incorrectly recognized as PNG Opportunities for documentation ;
- 50H 4EH 47H:PNG Each letter corresponds to ASCII, Recognize that it is PNG file ;
- 0DH 0AH:DOS Line breaks in style , be used for DOS-Unix Line break conversion of data ;
- 1AH: stay DOS At the command line , The end of file character used to block the display of files ;
- 0AH:Unix Line breaks in style , be used for Unix-DOS Line feed conversion ;
Use the file header flag to determine the file type (mythsman.com) Lists the header flags of more file formats .
PNG Data blocks define two types of data blocks , One is called critical data block , Is a required data block , The other is called auxiliary data block , Is an optional data block . Each data block contains a length 、 Block type 、 Block data 、 Cyclic redundancy detection has four parts .
For more detailed information, please refer to :PNG File format details - maxiongying - Blog Garden (cnblogs.com)
2. cv::imwrite() Function introduction
cv::imwrite() The function is Opencv Functions provided to save pictures , The function prototype :
bool cv::imwrite(
const string& filename,
cv::InputArray image,
const vector<int>& params =vector<int>()); Parameters params Is the parameter code saved for a specific format , yes STL vector type , The specific content is : A series of parameters ID And the parameter value corresponding to this parameter . For this article , preservation PNG Format , So parameters params It can be defined as :
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);CV_IMWRITE_PNG_COMPRESSION Means compression PNG;9 Indicates the strength of compression , A higher value means more compression , The scope is 0~9, The default is 3.
3. Sample code
#include <iostream>
#include <opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
// Create with alpha( transparent ) The tunnel Mat
cv::Mat pngAlphaMat(480, 640, CV_8UC4);
cv::Mat pngMat(480, 640, CV_8UC3);
for (int i = 0; i < pngAlphaMat.rows; i++)
{
for (int j = 0; j < pngAlphaMat.cols; j++)
{
Vec4b &rgba = pngAlphaMat.at<Vec4b>(i, j);
Vec3b &rgb = pngMat.at<Vec3b>(i, j);
rgba[0] = UCHAR_MAX;
rgba[1] = saturate_cast<uchar>((float(pngAlphaMat.cols - j) / ((float)pngAlphaMat.cols))*UCHAR_MAX);
rgba[2] = saturate_cast<uchar>((float(pngAlphaMat.rows - i) / ((float)pngAlphaMat.rows))*UCHAR_MAX);
rgba[3] = saturate_cast<uchar>(0.5 * (rgba[1] + rgba[2]));
rgb[0] = saturate_cast<uchar>((float(pngMat.cols - j) / ((float)pngMat.cols))*UCHAR_MAX);
rgb[1] = saturate_cast<uchar>((float(pngMat.rows - i) / ((float)pngMat.rows))*UCHAR_MAX);
rgb[2] = saturate_cast<uchar>(0.5 * (rgb[1] + rgb[2]));
}
}
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
cv::imwrite(" transparent Alpha chart .png", pngAlphaMat, compression_params);
cv::imwrite(" Opaque chart .png", pngMat, compression_params);
cv::imshow(" transparent PNG chart ", pngAlphaMat);
cv::imshow(" Opaque PNG chart ", pngMat);
cv::waitKey(0);
return 0;
}Saved pictures :

边栏推荐
猜你喜欢

1w5字详细介绍分布式系统的那些技术方案

When will brain like intelligence, which is popular in academia, land? Let's listen to what the industry masters say - qubits, colliders, x-knowledge Technology

uniapp---- 获取当前位置的经纬度等信息的详细步骤(包含小程序)

思迈特软件Smartbi完成C轮融资,推动国产BI加速进入智能化时代

The five pictures tell you: why is there such a big gap between people in the workplace?

中标捷报!南大通用GBase 8s中标南瑞集团2022年数据库框架项目
![[soft test software evaluator] 2013 comprehensive knowledge over the years](/img/c5/183acabd7015a5e515b7d83c127b2c.jpg)
[soft test software evaluator] 2013 comprehensive knowledge over the years
![第2章-2 计算分段函数[1]](/img/40/cad6bf92849624199af0fd1ba1d433.jpg)
第2章-2 计算分段函数[1]

49-OpenCv深入分析轮廓

C轮融资已完成!思迈特软件领跑国内BI生态赋能,产品、服务竿头一步
随机推荐
NDK 系列(6):说一下注册 JNI 函数的方式和时机
An article to understand data warehouse: metadata classification, metadata management
Opencv+paddle Orc recognize pictures and extract table information
2022牛客多校第二场解题报告
2022牛客多校第二场解题报告
The five pictures tell you: why is there such a big gap between people in the workplace?
PHP基础知识 - PHP 使用 PDO
Usage of constructors
【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样
leetcode/数组中和为0的三个不同数
Can‘t connect to server on ‘IP‘ (60)
博客搭建九:hugo添加搜索功能
Unity中队列(Queue)的简单使用
2022 Niuke multi school second problem solving Report
Mysql-怎么添加用户和设置权限?
Three different numbers with 0 in leetcode/ array
Round C financing has been completed! Smart software leads domestic Bi ecological empowerment, and products and services are a step forward
模型预测控制(MPC)解析(九):二次规划的数值解(下)
Redis basic knowledge, let's review it
GB/T 41479-2022信息安全技术 网络数据处理安全要求 导图概览