• 图像通道处理

图像读取和处理都是按BGR通道顺序进行的

 1 #include <iostream>
 2 #include <opencv2/opencv.hpp>
 3 #include "opencv2/highgui/highgui.hpp"
 4 #include "opencv2/imgproc/imgproc.hpp"
 5
 6 int main(int argc, char **argv) {
 7     cv::Mat src = cv::imread("/home/cv/Downloads/images/P1180141.JPG", 1);
 8     cv::resize(src, src, cv::Size(src.cols / 3, src.rows / 3), cv::INTER_CUBIC);
 9     cv::imshow("original_after_resize", src);
10
11     // blue channel
12     cv::Mat mytemp1(src.rows, src.cols, CV_8UC3, cv::Scalar(255, 0, 0));
13     cv::imshow("temp test", mytemp1);
14     // green channel
15     cv::Mat mytemp2(src.rows, src.cols, CV_8UC3, cv::Scalar(0, 255, 0));
16     cv::imshow("temp test", mytemp2);
17     // red channel
18     cv::Mat mytemp3(src.rows, src.cols, CV_8UC3, cv::Scalar(0, 0, 255));
19     cv::imshow("temp test", mytemp3);
20     cv::waitKey(0);
21     return 0;
22 }

或者使用mixChannels函数

 1 #include <opencv2/opencv.hpp>
 2 #include "opencv2/highgui/highgui.hpp"
 3 #include "opencv2/imgproc/imgproc.hpp"
 4
 5 int main(int argc, char **argv) {
 6     cv::Mat src = cv::imread("/home/cv/Downloads/images/P1180141.JPG", 1);
 7     cv::resize(src, src, cv::Size(src.cols / 3, src.rows / 3), cv::INTER_CUBIC);
 8     for (int i = 0; i < 3; i++) {
 9         cv::Mat temp_bgr(src.rows, src.cols, CV_8UC3, cv::Scalar(0, 0, 0));
10         cv::Mat temp(src.rows, src.cols, CV_8UC1);
11         cv::Mat out[] = {temp_bgr};
12         int from_to[] = {i, i};
13         cv::mixChannels(&src, 1, out, 1, from_to, 1);
14         cv::imshow("single channel", temp_bgr);
15         cv::waitKey(0);
16     }
17
18     return 0;
19 }

转换到HSV颜色空间查看效果

 1 #include <iostream>
 2 #include <opencv2/opencv.hpp>
 3 #include "opencv2/highgui/highgui.hpp"
 4 #include "opencv2/imgproc/imgproc.hpp"
 5
 6 int main(int argc, char **argv) {
 7     cv::Mat src = cv::imread("/home/cv/Downloads/images/P1180141.JPG", 1);
 8     cv::resize(src, src, cv::Size(src.cols / 3, src.rows / 3), cv::INTER_CUBIC);
 9     cv::imshow("original_after_resize", src);
10
11     cv::Mat img_hsv;
12     cv::cvtColor(src, img_hsv, CV_BGR2HSV);
13     cv::Mat dst;
14     dst.create(img_hsv.size(), img_hsv.depth());
15
16     int ch[] = {0, 0};
17     int ch1[] = {1, 0};
18     int ch2[] = {2, 0};
19
20     // Hue 色调通道
21     cv::mixChannels(&img_hsv, 1, &dst, 1, ch, 1);
22     cv::imshow("hsv-h", dst);
23     // Saturation 饱和度通道
24     cv::mixChannels(&img_hsv, 1, &dst, 1, ch1, 1);
25     cv::imshow("hsv-s", dst);
26     // Lightness/Value 亮度/像素值通道
27     cv::mixChannels(&img_hsv, 1, &dst, 1, ch2, 1);
28     cv::imshow("hsv-v", dst);
29     cv::waitKey(0);
30
31     return 0;
32 }

转换到YUV空间或YCrCb空间查看效果

 1 #include <iostream>
 2 #include <vector>
 3 #include <opencv2/opencv.hpp>
 4 #include "opencv2/highgui/highgui.hpp"
 5 #include "opencv2/imgproc/imgproc.hpp"
 6
 7 int main(int argc, char **argv) {
 8     cv::Mat src = cv::imread("/home/cv/Downloads/images/P1180141.JPG", 1);
 9     cv::resize(src, src, cv::Size(src.cols / 3, src.rows / 3), cv::INTER_CUBIC);
10     cv::imshow("original_after_resize", src);
11
12     // YUV color space
13     cv::Mat img_yuv;
14     cv::cvtColor(src, img_yuv, CV_BGR2YUV);
15     cv::Mat dst;
16     dst.create(img_yuv.size(), img_yuv.depth());
17
18     // Luminance, 明亮度,灰度值
19     int ch[] = {0, 0};
20     cv::mixChannels(&img_yuv, 1, &dst, 1, ch, 1);
21     cv::imshow("yuv-y", dst);
22     // Chrominance, 色彩及饱和度,指定像素的颜色
23     int ch1[] = {1, 0};
24     cv::mixChannels(&img_yuv, 1, &dst, 1, ch1, 1);
25     cv::imshow("yuv-u", dst);
26     int ch2[] = {2, 0};
27     cv::mixChannels(&img_yuv, 1, &dst, 1, ch2, 1);
28     cv::imshow("yuv-v", dst);
29
30     // YCrCb color space
31     cv::Mat ycrb;
32     cv::cvtColor(src, ycrb, CV_BGR2YCrCb);
33     std::vector<cv::Mat> mv;
34     cv::split(ycrb, (std::vector<cv::Mat>&) mv);
35
36     // Y channel
37     cv::Mat chy(src.rows, src.cols, CV_8UC1);
38     chy = mv[0].clone();
39     cv::imshow("Y", chy);
40     // Cr and Cb channel
41     // Cr反映RGB输入信号红色部分与RGB信号亮度值之间的差异
42     // Cb反应RGB输入信号蓝色部分与RGB信号亮度值之间的差异
43     cv::Mat chu(src.rows, src.cols, CV_8UC1);
44     chu = mv[1].clone();
45     cv::imshow("Cr", chu);
46     cv::Mat chv(src.rows, src.cols, CV_8UC1);
47     chv = mv[2].clone();
48     cv::imshow("Cb", chv);
49     cv::waitKey(0);
50
51     return 0;
52 }

  •  cv::Mat 类型的成员函数 depth 

查看官方文档

inline
int Mat::depth() const
{return CV_MAT_DEPTH(flags);
}

其中的 CV_MAT_DEPTH 定义如下

#define CV_MAT_DEPTH(flags)     ((flags) & CV_MAT_DEPTH_MASK)

这里的 CV_MAT_DEPTH_MASK 定义如下

#define CV_MAT_DEPTH_MASK       (CV_DEPTH_MAX - 1)

宏定义中的 CV_DEPTH_MAX 定义如下

#define CV_CN_SHIFT 3
#define CV_DEPTH_MAX (1 << CV_CN_SHIFT)

Mat::type
Returns the type of a matrix element.

C++: int Mat::type() const
The method returns a matrix element type. This is an identifier compatible with the CvMat type system, like CV_16SC3 or 16-bit signed 3-channel array, and so on.

表格来源(OpenCV学习十七:OpenCV中Mat的type)

出处暂不可考

Mat::depth
Returns the depth of a matrix element.

C++: int Mat::depth() const
The method returns the identifier of the matrix element depth (the type of each individual channel). For example, for a 16-bit signed element array, the method returns CV_16S . A complete list of matrix types contains the following values:

#define CV_8U 0
#define CV_8S 1
#define CV_16U 2
#define CV_16S 3
#define CV_32S 4
#define CV_32F 5
#define CV_64F 6
#define CV_USRTYPE1 7

CV_8U - 8-bit unsigned integers ( 0..255 )
CV_8S - 8-bit signed integers ( -128..127 )
CV_16U - 16-bit unsigned integers ( 0..65535 )
CV_16S - 16-bit signed integers ( -32768..32767 )
CV_32S - 32-bit signed integers ( -2147483648..2147483647 )
CV_32F - 32-bit floating-point numbers ( -FLT_MAX..FLT_MAX, INF, NAN )
CV_64F - 64-bit floating-point numbers ( -DBL_MAX..DBL_MAX, INF, NAN )

转换函数

void cv::transform (InputArray src,OutputArray dst,InputArray m
)The function cv::transform performs the matrix transformation of every element of the array src and stores the results in dst :???(I)=?⋅???(I)    (when m.cols=src.channels()), or???(I)=?⋅[???(I);1]    (when m.cols=src.channels()+1)Every element of the N-channel array src is interpreted as N-element vector that is transformed using the MxN or Mx(N+1) matrix m to M-element vector - the corresponding element of the output array dst.The function may be used for geometrical transformation of N-dimensional points, arbitrary linear color space transformation(such as various kinds of RGB to YUV transforms), shuffling the image channels, and so forth.Parameterssrc    input array that must have as many channels(1 to 4) as m.cols or m.cols-1.dst    output array of the same size and depth as src; it has as many channels as m.rows.m    transformation 2x2 or 2x3 floating-point matrix.

  • 以切片方式读取cv::Mat

读取矩阵中指定区域内的数据, OpenCV 有提供一种切片操作

template<typename _Tp> class Rect_
{public:typedef _Tp value_type;//! default constructor
    Rect_();Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);Rect_(const Rect_& r);Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz);Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2);Rect_& operator = ( const Rect_& r );//! the top-left cornerPoint_<_Tp> tl() const;//! the bottom-right cornerPoint_<_Tp> br() const;//! size (width, height) of the rectangleSize_<_Tp> size() const;//! area (width*height) of the rectangle_Tp area() const;//! true if emptybool empty() const;//! conversion to another data typetemplate<typename _Tp2> operator Rect_<_Tp2>() const;//! checks whether the rectangle contains the pointbool contains(const Point_<_Tp>& pt) const;_Tp x; //!< x coordinate of the top-left corner_Tp y; //!< y coordinate of the top-left corner_Tp width; //!< width of the rectangle_Tp height; //!< height of the rectangle
};typedef Rect_<int> Rect2i;
typedef Rect_<float> Rect2f;
typedef Rect_<double> Rect2d;
typedef Rect2i Rect;

cv::Rect

cv::Rect(int x, int y, int width, int height)

其中xy分别表示所取区域的其实坐标, width 和 height 分别表示所取区域的宽度和高度。例如

(cv::Mat(cv::Size(500, 500), CV_8UC1, cv::Scalar(0.0f)))(cv::Rect(0, 0, 100, 25))

就表示取原矩阵中以 (0, 0) 为起点,宽100高25的区域。

  • 保存图片压缩问题

https://www.cnblogs.com/skyfsm/p/7136709.html

高斯金字塔操作

https://www.cnblogs.com/Matrix420/p/4214123.html

矩阵数据类型转换操作

https://blog.csdn.net/iracer/article/details/49204147

https://blog.csdn.net/haima1998/article/details/82079042

取对数操作/归一化操作等

https://blog.csdn.net/huayunhualuo/article/details/81486685

平滑操作

https://www.jianshu.com/p/6d203b0ffa11

图像加减乘除

https://blog.csdn.net/qq_35294564/article/details/81058226

https://blog.csdn.net/qq_38784454/article/details/81514056

https://blog.csdn.net/u011177305/article/details/51264294

https://www.cnblogs.com/little-monkey/p/7202628.html

未完待续……

转载于:https://www.cnblogs.com/phillee/p/11003473.html

OpenCV-3.4.3图像通道处理相关推荐

  1. OpenCV(九)—————图像通道合并与分离(C++)

    1.分离 将一张图像分别分成三个通道的图像. 使用split()函数,分离之后的数据存到一个Mat对象的容器中. #include <opencv2/opencv.hpp> #includ ...

  2. 【OpenCV 例程200篇】12. 图像通道的合并(cv2.merge)

    [OpenCV 例程200篇]12. 图像通道的合并(cv2.merge) 欢迎关注 『OpenCV 例程200篇』 系列,持续更新中 欢迎关注 『Python小白的OpenCV学习课』 系列,持续更 ...

  3. 【OpenCV 例程200篇】11. 图像通道的拆分(cv2.split)

    [OpenCV 例程200篇]11. 图像通道的拆分(cv2.split) 欢迎关注 『OpenCV 例程200篇』 系列,持续更新中 欢迎关注 『Python小白的OpenCV学习课』 系列,持续更 ...

  4. 使用Python,OpenCV获取、更改像素,修改图像通道,剪裁ROI

    使用Python,OpenCV获取.更改像素,修改图像通道,剪裁ROI 1. 效果图 2. 源码 参考 这篇博客将介绍使用Python,OpenCV获取.更改像素,修改图像通道,截取图像感兴趣ROI: ...

  5. OpenCV分离图像通道

    opencv的imread函数读取的灰度图是单通道的. opencv分离图像通道: 源码: Mat img = imread("D:/1.jpg",1);Mat imgR,imgG ...

  6. Opencv 图像处理:图像通道、直方图与色彩空间

    本文已收录于Opencv系列专栏: 深入浅出OpenCV ,专栏旨在详解Python版本的Opencv,为计算机视觉的开发与研究打下坚实基础.免费订阅,持续更新. 文章目录 1.图像通道 通道分离 通 ...

  7. 02 OpenCV图像通道处理

    1 通道提取与合并 在数字图像处理中,图像通道是指一个图像中的颜色信息被分离为不同的颜色分量.常见的图像通道包括RGB通道.灰度通道.HSV通道等. RGB通道是指将图像分离为红色.绿色和蓝色三个颜色 ...

  8. 使用OpenCV和Python计算图像的“彩色度”

    使用OpenCV和Python计算图像"彩色度" 1. 效果图 2. 炫彩度量方法是什么? 3. 源代码 参考 你是否尝试过计算每个图像的炫彩值,并根据炫彩值对自己的图像数据集进行 ...

  9. 图像通道、Scalar、分离、合成通道

    http://lib.csdn.net/article/opencv/33264 http://blog.csdn.net/laohu_tiger/article/details/17359777 h ...

  10. opencv4 c++ 提取图片中的白色区域_修正!【从零学习OpenCV 4】分割图像——分水岭法...

    点击上方"小白学视觉",选择"星标"公众号重磅干货,第一时间送达 经过几个月的努力,小白终于完成了市面上第一本OpenCV 4入门书籍<OpenCV 4开 ...

最新文章

  1. centos 7 文档服务器,centos 7 web服务器
  2. python web为什么不火-Python语言为什么这么火?老男孩Python入门培训
  3. Linux - 收藏集 - 掘金
  4. AngularJS table 按照表头字段排序功能(升序和降序)
  5. 图像孔洞填充与小连通域的删除
  6. 大学考试计算机基础应用试题及答案,大学计算机基础考试试题及答案
  7. 阿里巴巴制定了这 16 条
  8. line和spline_探索适用于Apache Spark的Spline Data Tracker和可视化工具(第1部分)
  9. Lecture 17 Shortest Paths I
  10. solr后台登录验证
  11. java trie_java字典树(Trie)实现中文模糊匹配
  12. 第一章 初识 MyBatis
  13. quotes 整站数据爬取存mongo
  14. 计算机科学技术名家讲座许进,论高校计算机科学与技术课程中存在的问题与改革方向.pdf...
  15. 关于Activity跳转动画大汇总
  16. 【滤波器】基于matlab脉冲响应不变法+双线性变换法数字滤波器设计【含Matlab源码 884期】
  17. 免费学plc的手机app_PLC网校app手机版 v1.2
  18. 计算机网络VLAN间路由,交换机必学知识 VLAN间路由理论指导
  19. 企业管理系统可视化权限功能设计
  20. 【JS逆向系列】某服务器平台sm系列算法分析

热门文章

  1. 蓝桥杯备战(一)3n+1问题
  2. c语言无符号扩展,C语言中的无符号扩展和带符号扩展
  3. 安装红帽linux显示半屏,在linux下模拟win+arrow来左右半屏当前窗口
  4. a4如何打印双面小册子_小册子打印
  5. Mybatis递归自查询查(无限极分类)
  6. 二十四、Java集合框架(一)
  7. Servlet期末复习笔记3
  8. LayaAir UI 组件 # Image 位图、Label 标签
  9. 21模块-orientation【管理设备的方向信息】
  10. radio按钮样式美化和checkbox按钮样式美化