当前位置:网站首页>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 .
边栏推荐
- Mouse over effect 7
- C language a little bit (may increase in the future)
- robots.txt限制搜索引擎收录
- 【微信小程序开发】样式汇总
- Gartner研究:在中国,混合云的采用已成为主流趋势
- 7_ Openresty installation
- 鼠标悬停效果四
- If I am in Beijing, where is a better place to open an account? In addition, is it safe to open a mobile account?
- Xception learning notes
- 鼠标悬停效果九
猜你喜欢
随机推荐
Pulsar Geo Replication/灾备/地域复制
Sampling Area Lights
In the industrial Internet, "small" programs have "big" effects
股票开账户如何优惠开户?还有,在线开户安全么?
How to open a stock account? Also, is it safe to open an account online?
鼠标悬停效果十
pycharm 软件deployment 灰色 无法点
Restcloud ETL实践之数据行列转换
Mouse over effect V
[graduation season · advanced technology Er] - summary from graduation to work
How do I hide div on Google maps- How to float a div over Google Maps?
基于Pytorch完整的训练一个神经网络并进行验证
Analysis and solution of anr problems
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
Zero foundation self-study SQL course | window function
AI 边缘计算平台 - BeagleBone AI 64 简介
ipmitool下载地址和编译安装时可能出现的问题
Restcloud ETl数据通过时间戳实现增量数据同步
I want to know how to open a stock account? Is it safe to open an account online?
How to use Jieba participle in unity