OpenCV学习:将图像转为二值图像(函数cvtColor和函数threshold)
2015-07-20 11:31
621 查看
想换一下CSDN账户的头像,换成自己的真实的头像,但是又不想那么直接,干脆就把头像转换成二值图得了,因为从二值图像是推不出来原图的
。这个过程需要OpenCV的两个函数,第一个函数是彩色图像转化为灰度图像:cvtColor函数;下一个函数是由灰度图转化为二值图像函数:threshold函数。用法很简单,代码如下:
[cpp] view plaincopyprint?
cvtColor(img_origin,img_gray,CV_BGR2GRAY);
threshold(img_gray,img_binary,145,255,THRESH_BINARY);
imwrite("/home/hon/result.jpg",img_binary);
imshow("binary image",img_binary);
既然说到了这两个函数,就说说这两个函数的用法吧。这两个函数都是OpenCV中C++系列的函数,函数没有前缀cv(大部分参考书籍上介绍的OpenCV函数是c系列的,有前缀cv)。
cvtColor函数:
原型:
[cpp] view plaincopyprint?
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )
src和dst分别是待转的图像(src)和待转图像转换后的图像(dst);code是一个掩码,表示由src到dst之间是怎么转的,比如是彩色转为灰度,还是彩色转为HSI模式;最后的dstCn表示dst图像的波段数,这个值默认是0,它可以从参数code中推断。
code的模式包括:
CV_RGB2GRAY:<彩色图像---灰度图像>
CV_BGR2YCrCb, CV_RGB2YCrCb, CV_YCrCb2BGR, CV_YCrCb2RGB
CV_BGR2HSV, CV_RGB2HSV, CV_HSV2BGR, CV_HSV2RGB
更多的变换信息可以参考 OpenCV 2.4.5 documentation:http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#void
cvtColor(InputArray src, OutputArray dst, int code, int dstCn)
threshold函数:
原型:
[cpp] view plaincopyprint?
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
src和dst分别是待处理的图像(src)和由src生成的二值图像(dst);thresh是阈值,所谓的阈值函数就肯定要有个阈值;maxval在某些模式使用,type就是模式了。
code的模式包括:
参考相关文档:OpenCV 2.4.5 documentation:http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html?highlight=threshold#cv.Threshold
注意:threshold函数针对的是单通道图像,这个一定要注意!
。这个过程需要OpenCV的两个函数,第一个函数是彩色图像转化为灰度图像:cvtColor函数;下一个函数是由灰度图转化为二值图像函数:threshold函数。用法很简单,代码如下:
[cpp] view plaincopyprint?
cvtColor(img_origin,img_gray,CV_BGR2GRAY);
threshold(img_gray,img_binary,145,255,THRESH_BINARY);
imwrite("/home/hon/result.jpg",img_binary);
imshow("binary image",img_binary);
既然说到了这两个函数,就说说这两个函数的用法吧。这两个函数都是OpenCV中C++系列的函数,函数没有前缀cv(大部分参考书籍上介绍的OpenCV函数是c系列的,有前缀cv)。
cvtColor函数:
原型:
[cpp] view plaincopyprint?
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )
src和dst分别是待转的图像(src)和待转图像转换后的图像(dst);code是一个掩码,表示由src到dst之间是怎么转的,比如是彩色转为灰度,还是彩色转为HSI模式;最后的dstCn表示dst图像的波段数,这个值默认是0,它可以从参数code中推断。
code的模式包括:
CV_RGB2GRAY:<彩色图像---灰度图像>
CV_BGR2YCrCb, CV_RGB2YCrCb, CV_YCrCb2BGR, CV_YCrCb2RGB
CV_BGR2HSV, CV_RGB2HSV, CV_HSV2BGR, CV_HSV2RGB
更多的变换信息可以参考 OpenCV 2.4.5 documentation:http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#void
cvtColor(InputArray src, OutputArray dst, int code, int dstCn)
threshold函数:
原型:
[cpp] view plaincopyprint?
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
src和dst分别是待处理的图像(src)和由src生成的二值图像(dst);thresh是阈值,所谓的阈值函数就肯定要有个阈值;maxval在某些模式使用,type就是模式了。
code的模式包括:
值 | 计算方法 |
THRESH_BINARY | |
THRESH_BINARY_INV | |
THRESH_TRUNC | |
THRESH_TOZERO | |
THRESH_TOZERO_INV |
注意:threshold函数针对的是单通道图像,这个一定要注意!
相关文章推荐
- 要成为linux网站运维工程师必须要掌握的技能
- 希尔排序(shellsort)
- Linux下抓取log的方法
- linux防火墙iptables原理及使用
- Install xrdp on CentOS 7 / RHEL 7
- Linux面试题汇总答案
- Linux进程的虚拟地址空间
- Ubuntu安装arm-linux-gcc编译工具
- Linux-->问题解决-->/dev/mapper/VolGroup-lv_root: unexpected inconsistency; run fsck manually
- 简单重置Centos服务器中Mysql的root密码
- Linux0.11内核--进程的调度(就绪态和运行态之间的转换)
- linux进程---sigaction 函数
- tomcat配置manager
- VMware软件安装问题
- 在tomcat中添加类型如xls的文件以支持直接在url中打开
- Linux系统之更改默认块大小
- Broadcom NetXtreme II BCM5706/5708/5709/5716 Driver 驱动问题处理办法
- 使用JOTM进行Tomcat的JTA调用
- Apache工具类ToStringBuilder用法简介
- 2.1 linux文件系统应用实例:递归遍历目录