当前位置:网站首页>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 .
边栏推荐
- Applet custom top navigation bar, uni app wechat applet custom top navigation bar
- Pychar open remote directory remote host
- Optimal transport Series 1
- Ipmitool download address and possible problems during compilation and installation
- go: finding module for package
- Prototype and prototype chain in JS
- 详解数据治理知识体系
- pycharm 软件deployment 灰色 无法点
- Restcloud ETL practice data row column conversion
- 鼠标悬停效果七
猜你喜欢

Desai wisdom number - other charts (parallel coordinate chart): employment of fresh majors in 2021

Share Creators萌芽人才培养计划来了!

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

Detailed data governance knowledge system

Optimal Transport系列1

RestCloud ETL实践之无标识位实现增量数据同步

nacos配置中心使用教程

Machine learning 9-universal approximator radial basis function neural network, examining PDA and SVM from a new perspective

Share Creators萌芽人才培養計劃來了!

Record a service deployment failure troubleshooting
随机推荐
股票开账户如何优惠开户?还有,在线开户安全么?
robots.txt限制搜索引擎收录
SWT / anr problem - binder stuck
VirtualBox installation enhancements
如果在小券商办理网上开户安全吗?我的资金会不会不安全?
鼠标悬停效果七
Mouse over effect II
pycharm 软件deployment 灰色 无法点
[graduation season · advanced technology Er] - summary from graduation to work
鼠标悬停效果六
How to open a stock account? Also, is it safe to open an account online?
PCB defect detection based on OpenCV and image subtraction
鼠标悬停效果八
[PR # 5 A] two way running (state pressure DP)
go: finding module for package
如果我在北京,到哪里开户比较好?另外,手机开户安全么?
鼠标悬停效果九
Mouse over effect I
鼠标悬停效果五
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip