颜色通道
RGB 图像有4 个默认通道:红色、绿色和蓝色各有一个通道,以及一个用于编辑图像复合通道(主通道)
彩色深度
8位色,每个像素所能显示的彩色数为2的8次方,即256种颜色。
16位增强色,16位彩色,每个像素所能显示的彩色数为2的16次方,即65536种颜色。
24位真彩色,每个像素所能显示的彩色数为24位,即2的24次方,约1680万种颜色。
32位真彩色,即在24位真彩色图像的基础上再增加一个表示图像透明度信息的Alpha通道。
Alpha通道:一张图片的透明和半透明度
CV_8UC3系列解读
- CV_
(S|U|F)C - /*
- 1.bit_depth: 像素点占用空间大小 bit
- 2.S|U|F:
- S:signed int -->有符号
- U:unsigned int -->无符号
- F:float -->单精度浮点
- 3.number_of_channels
- 3.1 单通道图像,即为灰度图像
- 3.2 双通道图像
- 3.3 3通道图像
- 4.4 带Alpha通道的彩色图像,4通道图像
- */
Mat结构
- class Mat
- {
-
- public:
- /*
- flag:
- 1.数字签名
- 2.维度
- 3.通道数
- 4.连续性
- */
- int flags;
- int dims; //数据维数
- int rows,cols; //数据行列
- uchar *data; //存储的数据
- const uchar* datastart; //数据开始
- const uchar* dataend; //数据结束
- const uchar* datalimit; //数据边界
- //其他成员
- //.....
- //其他方法
- //.....
- public: //构造方式
- // 默认构造函数 Mat A;
- Mat ()
- // 常用构造函数 Mat A(10,10,CV_8UC3);
- Mat (int rows, int cols, int type)
- //Mat A(300, 400, CV_8UC3,Scalar(255,255,255));
- Mat (int ndims, const int *sizes, int type, const Scalar &s)
- Mat (Size size, int type)
- Mat (int rows, int cols, int type, const Scalar &s)
- Mat (Size size, int type, const Scalar &s)
- Mat (int ndims, const int *sizes, int type)
- Mat (const Mat &m)
- Mat (int rows, int cols, int type, void *data, size_t step=AUTO_STEP)
- Mat (Size size, int type, void *data, size_t step=AUTO_STEP)
- Mat (int ndims, const int *sizes, int type, void *data, const size_t *steps=0)
- Mat (const Mat &m, const Range &rowRange, const Range &colRange=Range::all())
- //Mat D (A, Rect(10, 10, 100, 100) );
- Mat (const Mat &m, const Rect &roi)
- Mat (const Mat &m, const Range *ranges)
-
- };
格式化打印Mat数据
- cout << "{}方式" << endl << format(img3, Formatter::FMT_C) << endl;
- cout << "C语言方式" << endl << format(img3, Formatter::FMT_NUMPY) << endl;
- cout << ",方式" << endl << format(img3, Formatter::FMT_CSV) << endl;
- cout << "py方式" << endl << format(img3, Formatter::FMT_PYTHON) << endl;
- cout <<"[]:" << img3 << endl;
访问或设置像素强度值
Scalar 它将各个通道的值构成一个整体,赋给具有相同通道数的矩阵元素,通俗点就是一个复合数据
- Mat A(10,10,CV_8UC3,Scalar(1,2,3));
- //CV_8UC3:8位存储的无符号3通道
- //分别把每一个像素点颜色通道赋值为1,2,3
imread函数读取图片
- Mat imread( const String& filename, int flags = IMREAD_COLOR );
- /****************************************************************
- * filename: 文件路径
- * flags : 显示方式
- *****************************************************************/
- enum ImreadModes {
- IMREAD_UNCHANGED = -1, //按原样返回加载的图像(带有alpha通道,否则会被裁剪)
- IMREAD_GRAYSCALE = 0, //单通道灰度图像
- IMREAD_COLOR = 1, //3通道BGR彩色图像
- IMREAD_ANYDEPTH = 2, //16位/32位图像,其他则转换为8位
- IMREAD_ANYCOLOR = 4, //图像以任何可能的颜色格式读取
- IMREAD_LOAD_GDAL = 8, //gdal驱动程序加载映像
- IMREAD_REDUCED_GRAYSCALE_2 = 16, //单通道灰度图像,并将图像大小减小1/2
- IMREAD_REDUCED_COLOR_2 = 17, //3通道BGR彩色图像,使图像大小减小1/2
- IMREAD_REDUCED_GRAYSCALE_4 = 32, //单通道灰度图像,并将图像尺寸减小1/4
- IMREAD_REDUCED_COLOR_4 = 33, //3通道BGR彩色图像,使图像大小减小1/4
- IMREAD_REDUCED_GRAYSCALE_8 = 64, //单通道灰度图像,并将图像尺寸减小1/8
- IMREAD_REDUCED_COLOR_8 = 65, //3通道BGR彩色图像,使图像大小减小1/8
- IMREAD_IGNORE_ORIENTATION = 128 //不要根据EXIF的方向标志旋转图像
- };
imshow()显示图片
- imshow(const string& str,InputArray mat);
- /*****************************************************/
- * str: 窗口名
- * mat: 图像
- *****************************************************/
销毁窗口
destroyAllWindows(): 销毁所有窗口
destroyWindow(const char* windowName) :销毁指定窗口
imwrite()函数保存图片
- bool imwrite( const String& filename, InputArray img,
- const std::vector<int>& params = std::vector<int>());
- /*****************************************************/
- * filename: 保存的文件名
- * img: 图像
- * params:设置图片质量,压缩率 一般不写
- *****************************************************/
注意.clone()方法和copyTo()方法
- #include
- #include
- using namespace cv;
- void TestIntMat()
- {
- //No.1 构造方式
- Mat img(10, 10, CV_8UC1);
- imshow("1", img);
- //std::cout << img << std::endl;
- //No.2
- Mat img2(300, 300, CV_8UC3, Scalar(0, 255, 0)); //BGR
- imshow("2", img2);
- //No.3
- Mat img3 = img2.clone(); //clone方法拷贝
- imshow("3", img3);
- //No.4
- Mat img4;
- img3.copyTo(img4);
- imshow("4", img4);
-
- //No.5
- Mat img5 = imread("mm.jpg");
- imshow("5", img5);
-
- //No.6
- Mat img6 = imread("mm.jpg", IMREAD_GRAYSCALE);
- imshow("6", img6);
-
- Mat img7 = imread("mm.jpg", IMREAD_REDUCED_COLOR_2);
- imshow("7", img7);
-
- }
- void PrintMat()
- {
- Mat mat(10, 10, CV_8UC1);
- //No.1 通过重载方式打印
- std::cout << "重载方式遍历:" << std::endl;
- std::cout << mat << std::endl;
- std::cout << "C:" << std::endl;
- std::cout << format(mat, Formatter::FMT_C) << std::endl;
- std::cout << "py:" << std::endl;
- std::cout << format(mat, Formatter::FMT_NUMPY) << std::endl;
- std::cout << "csv:" << std::endl;
- std::cout << format(mat, Formatter::FMT_CSV) << std::endl;
- std::cout << "py:" << std::endl;
- std::cout << format(mat, Formatter::FMT_PYTHON) << std::endl;
- }
- void testSaveImg(const char* fileName)
- {
- Mat mm = imread("mm.jpg", IMREAD_GRAYSCALE);
- imshow("mm", mm);
- imwrite(fileName, mm);
- }
-
- int main()
- {
- //TestIntMat();
- //PrintMat();
- testSaveImg("love.jpg");
- waitKey(0);
- return 0;
- }