用OpenCV 读取 png 中含有 alpha 的图像
2011-07-20 09:28
399 查看
OpenCV对图像的读取和保存支持非常方便。但也有非常不爽之处。
其中之一就是不能读写ALpha通道。今天在网上找到了解决的办法(http://blog.developer.stylight.de/2010/05/how-to-load-alpha-channel-pngs-with.html):
We are currently facing the situation that we need to process PNG files that contain an alpha channel. Unfortunately OpenCV curently does not support so (but there's a hack - but that didn't help in my situation). The documentation for cvLoadImage contains a sad little note:
Note that in the current implementation the alpha channel, if any, is stripped from the output image, e.g. 4-channel RGBA image will be loaded as RGB.
Diving into the code I figured alpha channels have been disabled on purpose. Around line 220 in grfmt_png.cpp (the respective PNG encoder / decoder) it reads: png_set_strip_alpha( png_ptr ).
So the hope would be, if one disables this line libpng should be able to read in the alpha channel. Indeed it does, if you specify a large enough buffer to write in (CV_8UC4 instead of CV_8UC3). So we need a few more patches to make it work throughout:
1. In grfmt_png.cpp, line 170 add a line that allows to push through the type (alpha channel) detection:
// Allow for PNG alpha channel here
if(color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
m_type = CV_8UC4;
}
2. In grfmt_png.cpp, around line 220 allow for an alpha channel by commenting the following statements:
//png_set_strip_alpha( png_ptr );
3. In loadsave.cpp , line 222 we finally tweak the parent routine to be more flexible towards the channels returned. Here we replace the "3" by CV_MAT_CN(type):
type = CV_MAKETYPE(CV_MAT_DEPTH(type), CV_MAT_CN(type));
(Note all changes were performed against OpenCV 2.0)
After a recompile you should be able to read alpha-channeled pngs as well!
过几天有空了仔细检查一下。测试没有问题把新的代码共享上来。再去掉8U*C的约束。就更好用了
转自 http://blog.sina.com.cn/s/blog_5119a7f90100renv.html
其中之一就是不能读写ALpha通道。今天在网上找到了解决的办法(http://blog.developer.stylight.de/2010/05/how-to-load-alpha-channel-pngs-with.html):
We are currently facing the situation that we need to process PNG files that contain an alpha channel. Unfortunately OpenCV curently does not support so (but there's a hack - but that didn't help in my situation). The documentation for cvLoadImage contains a sad little note:
Note that in the current implementation the alpha channel, if any, is stripped from the output image, e.g. 4-channel RGBA image will be loaded as RGB.
Diving into the code I figured alpha channels have been disabled on purpose. Around line 220 in grfmt_png.cpp (the respective PNG encoder / decoder) it reads: png_set_strip_alpha( png_ptr ).
So the hope would be, if one disables this line libpng should be able to read in the alpha channel. Indeed it does, if you specify a large enough buffer to write in (CV_8UC4 instead of CV_8UC3). So we need a few more patches to make it work throughout:
1. In grfmt_png.cpp, line 170 add a line that allows to push through the type (alpha channel) detection:
// Allow for PNG alpha channel here
if(color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
m_type = CV_8UC4;
}
2. In grfmt_png.cpp, around line 220 allow for an alpha channel by commenting the following statements:
//png_set_strip_alpha( png_ptr );
3. In loadsave.cpp , line 222 we finally tweak the parent routine to be more flexible towards the channels returned. Here we replace the "3" by CV_MAT_CN(type):
type = CV_MAKETYPE(CV_MAT_DEPTH(type), CV_MAT_CN(type));
(Note all changes were performed against OpenCV 2.0)
After a recompile you should be able to read alpha-channeled pngs as well!
过几天有空了仔细检查一下。测试没有问题把新的代码共享上来。再去掉8U*C的约束。就更好用了
转自 http://blog.sina.com.cn/s/blog_5119a7f90100renv.html
相关文章推荐
- Python-OpenCV读取png图像转成jpg图像存储
- OpenCV读取图像时Alpha通道的处理
- opencv读取图像的几个基本函数,imread、namedWindow、imshow、imwrite
- OpenCV的读取图像使用注意事项
- qt学习笔记(3)Qt、opencv、vs2010本地读取图像、反转图像并显示
- 使用OpenCV读取摄像头图像并显示
- opencv imread读取alpha通道
- 用OPencv读取一副图像的直方图
- opencv读取图像像素值
- opencv imread 函数读取CV_16UC1的png图片
- opencv读取视频保存成图像
- OPENCV对于有alpha通道的透明背景图片的读取和图片叠加
- OpenCV【2】---读取png图片显示到QT label上的问题
- 图像的读取显示以及保存----《opencv-python中文教程》
- opencv读取字符串图像
- opencv学习笔记之读取,修改,保存图像
- Opencv 图像读取与保存问题
- opencv的imread函数无法读取图像
- Opencv 图像读取与保存问题
- OpenCV中图像的读取与显示