当前位置:网站首页>Saving images of different depths in opencv
Saving images of different depths in opencv
2022-07-01 02:46:00 【Love CV】
What is image depth
A lot of people start to learn OpenCV after , Touch the front few API Include imwrite function , And soon learned how to save Mat Object is image , The general code is as follows :
imwrite("D:/result.png ", dst);
among dst yes Mat object .
The image saved in this way is each channel by default 8 Bit byte image , common RGB The image is an image with a depth of 24, This can be done through windows View the image properties to get , The screenshot is as follows :

If each channel occupies 16 A word of ,RGB The image depth becomes 48, If each channel occupies 32 A word of , The depth becomes 96, Obviously, the deeper the image , There will also be more image files , The more memory will be consumed when loading , therefore OpenCV The default read / write image in is each channel 8 position ( Single byte ) Images .
To relive imwrite function
Suppose we want to save the image as 16 Bits or 32 Bit floating point number image , What should we do , Before we begin, let's review again OpenCV Save image in API function -imwrite
bool cv::imwrite(
const String & filename,
InputArray img,
const std::vector< int > & params = std::vector< int >()
)
Filename The parameter is the path to save the declared file
Img The parameter represents the... To be saved Mat Image object
Params Represents the options when saving an image ,
These options include PNG/JPG/WEBP/TIFF The compression quality 、 Format selection, etc , It can be divided into the following four categories
ImwriteEXRTypeFlags
ImwriteFlags
ImwritePAMFlags
ImwritePNGFlags,
I wrote an article before about how to use these options to compress the image properly when maintaining , Here's about Params The use of parameters will not be repeated , Mainly based on key-value Method added to vector Just go to the middle .
imwrite The image type support of the function when saving to different depth formats is described as follows :
8 Bitwise image (CV_8U), Support png/jpg/bmp/webp And other common image formats
16 Bitwise image (CV_16U), Support png/jpeg2000/TIFF Format
32 Bitwise image (CV_32F), Support PFM/TIFF/OpenEXR/TIFF/HDR
Before saving to the specified format , Can pass convertTo perhaps cvtCOLOR After image type or channel conversion , Call again imwrite Preservation .
Various depths of preservation
16 Bit image saving
After the transformation , If you save directly , The code is as follows :
// Load image
Mat src = imread("D:/flower.png", IMREAD_UNCHANGED);
printf("depth %d \n", src.depth());
// Convert to 16 Bit image
Mat dst;
src.convertTo(dst, CV_16U);
imshow("flower16", dst);
imwrite("D:/flower-16.png", dst);
After the transformation , Normalize and save , The code is as follows :
// Load image
Mat src = imread("D:/flower.png", IMREAD_UNCHANGED);
printf("depth %d \n", src.depth());
// Convert to 16 Bit image
Mat dst;
src.convertTo(dst, CV_16U);
// Normalize and save
normalize(dst, dst, 0, 256 * 256, NORM_MINMAX);
imwrite("D:/flower-16.png", dst);
imshow("flower-16", dst);
The comparison of the two effects is as follows :

32 Bit image saving
// Load image
Mat src = imread("D:/flower.png", IMREAD_UNCHANGED);
printf("depth %d \n", src.depth());
// Convert to 16 Bit image
Mat dst;
src.convertTo(dst, CV_32F);
// Normalize and save
normalize(dst, dst, 0, 1.0, NORM_MINMAX);
imwrite("D:/flower-32.png", dst);
imshow("flower-32", dst);
For the above images with different depths , It must be read correctly in the following way
Mat src = imread("D:/flower.png", IMREAD_UNCHANGED);
among IMREAD_UNCHANGED Indicates that no changes will be made to the original image .
边栏推荐
- Focusing on green and low carbon, data center cooling has entered a new era of "intelligent cooling"
- Mouse over effect 7
- servlet【初识】
- [wechat applet development] style summary
- SAP ALV汇总跟导出Excel 汇总数据不一致
- Proxy support and SNI routing of pulsar
- Mouse over effect V
- Mouse over effect II
- Pulsar Geo Replication/灾备/地域复制
- PTA 1016
猜你喜欢

Optimal Transport系列1

Optimal transport Series 1

Gartner研究:在中国,混合云的采用已成为主流趋势

Voici le programme de formation des talents de SHARE Creators!

Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?

Visual effects, picture to cartoon function

Restcloud ETL WebService data synchronization to local

AI edge computing platform - beaglebone AI 64 introduction

MCU firmware packaging Script Software

小程序自定义顶部导航栏,uni-app微信小程序自定义顶部导航栏
随机推荐
Viewing JVM parameters
5款主流智能音箱入门款测评:苹果小米华为天猫小度,谁的表现更胜一筹?
Restcloud ETL practice data row column conversion
Pychart software deployment gray unable to point
鼠标悬停效果四
Visual effects, picture to cartoon function
Restcloud ETl数据通过时间戳实现增量数据同步
How to open a stock account? Also, is it safe to open an account online?
详解数据治理知识体系
Find the length of the common part of two line segments
How to add a condition for an associated table in an SQL statement [null value required or not required]
A small document of JS method Encyclopedia
go: finding module for package
Mouse over effect 7
鼠标悬停效果十
import tensorflow.contrib.slim as slim报错
集群方法同步执行框架 Suona
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
js中的图片预加载
如果我在北京,到哪里开户比较好?另外,手机开户安全么?