当前位置:网站首页>OpenCV中保存不同深度图像的技巧
OpenCV中保存不同深度图像的技巧
2022-04-22 23:59:00 【liiiiiiiiiiiiike】
什么是图像深度?
很多人开始学习OpenCV之后,接触的几个方法就包括imwrite函数,而且很快知道需要传入一个ndarray类型的mat对象作为实参,常规代码如下:
imwrite("D:/result.png ", dst);#dst为mat对象,前面的实参为保存的地址
这样保存的图像默认是每个通道8位的字节图像(也就是每个通道像素点的大小为0~255),常见的RGB图像是图像深度为24,这个可以通过windows下查看图像属性获得,截图如下:

如果每个通道占16位的话,RGB图像深度就会变成48,如果每个通道占32位的话,深度就会变成96,显然图像深度越大,图像文件也会越多,加载时候消耗的内存也会越多,所以OpenCV中默认读写图像都是每个通道8位(单字节)图像。
各种不同深度图像的保存
转换之后,如果直接保存,代码如下:
#加载图像
Mat src = imread("D:/flower.png", IMREAD_UNCHANGED);
printf("depth %d \n", src.depth());
#转为为16位图像
Mat dst;
src.convertTo(dst, CV_16U);
imshow("flower16", dst);
imwrite("D:/flower-16.png", dst);
转换之后,归一化之后再保存,代码如下:
# 加载图像
Mat src = imread("D:/flower.png", IMREAD_UNCHANGED);
printf("depth %d \n", src.depth());
# 转为为16位图像
Mat dst;
src.convertTo(dst, CV_16U);
# 归一化再保存
normalize(dst, dst, 0, 256 * 256, NORM_MINMAX);
imwrite("D:/flower-16.png", dst);
imshow("flower-16", dst);
两者效果对比如下:

# 加载图像
Mat src = imread("D:/flower.png", IMREAD_UNCHANGED);
printf("depth %d \n", src.depth());
# 转为为32位图像
Mat dst;
src.convertTo(dst, CV_32F);
# 归一化再保存
normalize(dst, dst, 0, 1.0, NORM_MINMAX);
imwrite("D:/flower-32.png", dst);
imshow("flower-32", dst);
对上述各种不同深度的图像,必须通过下面的方式才可以正确读取
Mat src = imread("D:/flower.png", IMREAD_UNCHANGED);
版权声明
本文为[liiiiiiiiiiiiike]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45074568/article/details/123084669
边栏推荐
- AcWing 1842. Milk bucket traverses all situations
- LabVIEW implements application stop or exit
- [Objective-C advanced programming] - GCD
- Vs write assembly
- Rotate according to angle
- SystemVerilog verification - Test Platform preparation guide learning notes (1): data types
- [leetcode] binary tree, maximum depth of 104 binary tree, diameter of 543 binary tree, maximum path and sum of 124 binary tree
- L1-075 强迫症 (10 分)
- 用C语言编写一个俄罗斯方块小游戏
- 基于.NetCore开发博客项目 StarBlog - (3) 模型设计
猜你喜欢

Interviewer: how big is the performance difference between ArrayList and LinkedList?

YASKAWA motor servo software sigmawin + cannot be connected to the servo driver

Fundamentals of programming language (1)

Visual studio always conflicts with Sogou input method

【leetcode】二叉树,226翻转二叉树,116填充二叉树节点的右侧指针,114将二叉树展开为链表

AcWing 1842. Milk bucket traverses all situations
![[* ctf2022] Web Topic reproduction and WP](/img/b2/baaa8a0170d787a7585e7463c9a4f8.png)
[* ctf2022] Web Topic reproduction and WP
![FileNotFoundError: [Errno 2] No such file or directory: 'image/1. Jpg 'problem solving](/img/72/dca1064713646f909c4a7a8567a742.png)
FileNotFoundError: [Errno 2] No such file or directory: 'image/1. Jpg 'problem solving
![[turtle confession collection]](/img/81/b4bacc23691e58e403f1330d0ca7cf.jpg)
[turtle confession collection] "the moon at the bottom of the sea is the moon in the sky, and the person in front of us is the sweetheart." More joy and peace for the rest of your life ~ (with 3 sourc

FPGA (VI) RTL code II (complex circuit design 1)
随机推荐
Fundamentals of programming language (1)
Solve the error reporting problem of require is not defined
简便工作时间的法宝:以密钥验证的方式来登录服务器
AcWing 1842. Milk bucket traverses all situations
两种颜色渐变
Font adaptation
Redis的基本使用
The parameter configuration of websoket package is out of the box
根据宽度计算文字大小
Conjugate gradients (4)
Gets the range of the currently selected string
Thought of reducing Governance -- detailed summary of binary search
Wechat applet page Jump
Hypermesh-laminate复合材料案例学习笔记
Learn rust
Gets the color of a point
L1-075 强迫症 (10 分)
选中所有的文字
Calculate text size based on width
Detailed explanation of seven common query connections in MySQL