opencv——PNG格式图像读取转换函数
2015-08-03 19:42
253 查看
暑假培训学习了opencv,放图像时,PNG格式的Alpha通道没用啊
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/angry.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/angry.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/angry.gif)
图都自己去p颜色,用来加载时变颜色
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif)
工作量太大了,,于是花了一下午弄了个简易功能的PNG格式文件读取转化加载函数。
注:这里主要是用opencv 3.0主要用Mat类来存储图片,用Ipl指针的可以类似的转换下。
首先查了网上资料才知道在imread后加-1值才可以读取alpha通道,这时为4通道,而正常显示3通道就可以了,第4通道就是alpha通道,
所以只要根据alpha值简单的转换到3通道再操作就行了!!
//欢迎讨论-,-作为一只啥都半懂的大学狗,希望有大神指点
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/angry.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/angry.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/angry.gif)
图都自己去p颜色,用来加载时变颜色
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif)
工作量太大了,,于是花了一下午弄了个简易功能的PNG格式文件读取转化加载函数。
注:这里主要是用opencv 3.0主要用Mat类来存储图片,用Ipl指针的可以类似的转换下。
首先查了网上资料才知道在imread后加-1值才可以读取alpha通道,这时为4通道,而正常显示3通道就可以了,第4通道就是alpha通道,
所以只要根据alpha值简单的转换到3通道再操作就行了!!
</pre><pre name="code" class="cpp">#include <stdio.h> #include <core.hpp> #include <highgui.hpp> using namespace std; using namespace cv; int main() { Mat h_forpng(Mat picture); int i, j; Mat M = imread("图片相对路径", -1);//这里加-1是为了读取PNG格式的alpha通道,不加的话无法读取,那么后面的操作也就无进行 Mat N; N = h_forpng(M); imshow("qwe",M); imshow("zzz", N);//用来看效果的,当然-,-最后都是拿了核心代码就行了 waitKey(); return 0; } Mat h_forpng(Mat picture) { Mat After(picture.rows, picture.cols, CV_8UC3, Scalar(100,100,100));//这里是更改图片背景,统一颜色,以后的图片叠加好去色 int i, j; //当然这里可以直接改为背景 for (i = 0; i < picture.rows; i++) { for (j = 0; j < picture.cols; j++) { After.at<Vec3b>(i, j)[0] = picture.at<Vec4b>(i, j)[0] * float(picture.at<Vec4b>(i, j)[3]) / 255 + After.at<Vec3b>(i, j)[0] * float(255 - picture.at<Vec4b>(i, j)[3])/255; After.at<Vec3b>(i, j)[1] = picture.at<Vec4b>(i, j)[1] * float(picture.at<Vec4b>(i, j)[3]) / 255 + After.at<Vec3b>(i, j)[1] * float(255 - picture.at<Vec4b>(i, j)[3])/255; After.at<Vec3b>(i, j)[2] = picture.at<Vec4b>(i, j)[2] * float(picture.at<Vec4b>(i, j)[3]) / 255 + After.at<Vec3b>(i, j)[2] * float(255 - picture.at<Vec4b>(i, j)[3])/255; } } return After;//这里如果直接对图片操作,不需要存储,建议用指针带入,改为void,避免了读入大图片带来的资源浪费。 }
//欢迎讨论-,-作为一只啥都半懂的大学狗,希望有大神指点
相关文章推荐
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua实现split函数
- Lua常用时间函数使用实例
- Lua函数与字符串处理简明总结
- Lua学习笔记之表和函数
- Lua中实现sleep函数功能的4种方法
- Lua函数用法研究
- Lua基础教程之赋值语句、表达式、流程控制、函数学习笔记
- PowerShell函数简明教程
- 探索PowerShell(十一)函数介绍