• 2--OpenCV:图形基础


    图像基础

    • 颜色通道

      • RGB 图像4 个默认通道红色、绿色和蓝色各有一个通道,以及一个用于编辑图像复合通道(主通道)

    • 彩色深度

      • 8位色,每个像素所能显示的彩色数为2的8次方,即256种颜色。

      • 16位增强色,16位彩色,每个像素所能显示的彩色数为2的16次方,即65536种颜色。

      • 24位真彩色,每个像素所能显示的彩色数为24位,即2的24次方,约1680万种颜色。

      • 32位真彩色,即在24位真彩色图像的基础上再增加一个表示图像透明度信息的Alpha通道。

        • Alpha通道:一张图片的透明和半透明度

    • CV_8UC3系列解读

    1. CV_(S|U|F)C
    2. /*
    3. 1.bit_depth: 像素点占用空间大小 bit
    4. 2.S|U|F:
    5. S:signed int   -->有符号
    6. U:unsigned int -->无符号
    7. F:float   -->单精度浮点
    8.    3.number_of_channels
    9.   3.1 单通道图像,即为灰度图像
    10.   3.2 双通道图像
    11.   3.3 3通道图像
    12.   4.4 带Alpha通道的彩色图像,4通道图像
    13. */

    读取图像

    • Mat结构

    1. class  Mat
    2. {
    3. public
    4. /*
    5. flag:
    6. 1.数字签名
    7. 2.维度
    8. 3.通道数
    9. 4.连续性
    10. */
    11. int flags;
    12. int dims; //数据维数
    13. int rows,cols; //数据行列
    14. uchar *data; //存储的数据
    15.    const uchar* datastart; //数据开始
    16.    const uchar* dataend; //数据结束
    17.    const uchar* datalimit; //数据边界
    18. //其他成员  
    19. //.....
    20. //其他方法
    21. //.....
    22. public: //构造方式
    23.    // 默认构造函数 Mat A;
    24.    Mat ()
    25.    // 常用构造函数 Mat A(10,10,CV_8UC3);
    26.    Mat (int rows, int cols, int type)
    27.    //Mat A(300, 400, CV_8UC3,Scalar(255,255,255));
    28.    Mat (int ndims, const int *sizes, int type, const Scalar &s)
    29.    Mat (Size size, int type)
    30.    Mat (int rows, int cols, int type, const Scalar &s)
    31.    Mat (Size size, int type, const Scalar &s)
    32.    Mat (int ndims, const int *sizes, int type)
    33.    Mat (const Mat &m)
    34.    Mat (int rows, int cols, int type, void *data, size_t step=AUTO_STEP)
    35.    Mat (Size size, int type, void *data, size_t step=AUTO_STEP)
    36.    Mat (int ndims, const int *sizes, int type, void *data, const size_t *steps=0)
    37.    Mat (const Mat &m, const Range &rowRange, const Range &colRange=Range::all())
    38.    //Mat D (A, Rect(10, 10, 100, 100) );
    39.    Mat (const Mat &m, const Rect &roi)
    40.    Mat (const Mat &m, const Range *ranges)
    41.    
    42. };
    • 格式化打印Mat数据

    1. cout << "{}方式" << endl << format(img3, Formatter::FMT_C) << endl;
    2. cout << "C语言方式" << endl << format(img3, Formatter::FMT_NUMPY) << endl;
    3. cout << ",方式" << endl << format(img3, Formatter::FMT_CSV) << endl;
    4. cout << "py方式" << endl << format(img3, Formatter::FMT_PYTHON) << endl;
    5. cout <<"[]:" << img3 << endl;
    • 访问或设置像素强度值

    Scalar 它将各个通道的值构成一个整体,赋给具有相同通道数的矩阵元素,通俗点就是一个复合数据

    1. Mat A(10,10,CV_8UC3,Scalar(1,2,3));
    2. //CV_8UC3:8位存储的无符号3通道
    3. //分别把每一个像素点颜色通道赋值为1,2,3
    • imread函数读取图片

    1. Mat imread( const String& filename, int flags = IMREAD_COLOR );
    2. /****************************************************************
    3. * filename: 文件路径
    4. * flags   : 显示方式
    5. *****************************************************************/
    6. enum ImreadModes {
    7.       IMREAD_UNCHANGED            = -1, //按原样返回加载的图像(带有alpha通道,否则会被裁剪)
    8.       IMREAD_GRAYSCALE            = 0, //单通道灰度图像
    9.       IMREAD_COLOR                = 1, //3通道BGR彩色图像
    10.       IMREAD_ANYDEPTH             = 2, //16位/32位图像,其他则转换为8位
    11.       IMREAD_ANYCOLOR             = 4, //图像以任何可能的颜色格式读取
    12.       IMREAD_LOAD_GDAL            = 8, //gdal驱动程序加载映像
    13.       IMREAD_REDUCED_GRAYSCALE_2  = 16, //单通道灰度图像,并将图像大小减小1/2
    14.       IMREAD_REDUCED_COLOR_2      = 17, //3通道BGR彩色图像,使图像大小减小1/2
    15.       IMREAD_REDUCED_GRAYSCALE_4  = 32, //单通道灰度图像,并将图像尺寸减小1/4
    16.       IMREAD_REDUCED_COLOR_4      = 33, //3通道BGR彩色图像,使图像大小减小1/4
    17.       IMREAD_REDUCED_GRAYSCALE_8  = 64, //单通道灰度图像,并将图像尺寸减小1/8
    18.       IMREAD_REDUCED_COLOR_8      = 65, //3通道BGR彩色图像,使图像大小减小1/8
    19.       IMREAD_IGNORE_ORIENTATION   = 128 //不要根据EXIF的方向标志旋转图像
    20. };

    显示图像

    • imshow()显示图片

      1. imshow(const string& str,InputArray mat);
      2. /*****************************************************/
      3. * str: 窗口名
      4. * mat: 图像
      5. *****************************************************/  
    • 销毁窗口

      • destroyAllWindows(): 销毁所有窗口

      • destroyWindow(const char* windowName) :销毁指定窗口

    保存图像

    • imwrite()函数保存图片

      1. bool imwrite( const String& filename, InputArray img,
      2.              const std::vector<int>& params = std::vector<int>());
      3. /*****************************************************/
      4. * filename: 保存的文件名
      5. * img: 图像
      6. * params:设置图片质量,压缩率  一般不写
      7. *****************************************************/  

    实例代码:

      注意.clone()方法和copyTo()方法

    1. #include
    2. #include
    3. using namespace cv;
    4. void TestIntMat()
    5. {
    6. //No.1 构造方式
    7. Mat img(10, 10, CV_8UC1);
    8. imshow("1", img);
    9. //std::cout << img << std::endl;
    10. //No.2
    11. Mat img2(300, 300, CV_8UC3, Scalar(0, 255, 0));  //BGR
    12. imshow("2", img2);
    13. //No.3
    14. Mat img3 = img2.clone(); //clone方法拷贝
    15. imshow("3", img3);
    16. //No.4
    17. Mat img4;
    18. img3.copyTo(img4);
    19. imshow("4", img4);
    20. //No.5
    21. Mat img5 = imread("mm.jpg");
    22. imshow("5", img5);
    23. //No.6
    24. Mat img6 = imread("mm.jpg", IMREAD_GRAYSCALE);
    25. imshow("6", img6);
    26. Mat img7 = imread("mm.jpg", IMREAD_REDUCED_COLOR_2);
    27. imshow("7", img7);
    28. }
    29. void PrintMat()
    30. {
    31. Mat mat(10, 10, CV_8UC1);
    32. //No.1 通过重载方式打印
    33. std::cout << "重载方式遍历:" << std::endl;
    34. std::cout << mat << std::endl;
    35. std::cout << "C:" << std::endl;
    36. std::cout << format(mat, Formatter::FMT_C) << std::endl;
    37. std::cout << "py:" << std::endl;
    38. std::cout << format(mat, Formatter::FMT_NUMPY) << std::endl;
    39. std::cout << "csv:" << std::endl;
    40. std::cout << format(mat, Formatter::FMT_CSV) << std::endl;
    41. std::cout << "py:" << std::endl;
    42. std::cout << format(mat, Formatter::FMT_PYTHON) << std::endl;
    43. }
    44. void testSaveImg(const char* fileName)
    45. {
    46. Mat mm = imread("mm.jpg", IMREAD_GRAYSCALE);
    47. imshow("mm", mm);
    48. imwrite(fileName, mm);
    49. }
    50. int main()
    51. {
    52. //TestIntMat();
    53. //PrintMat();
    54. testSaveImg("love.jpg");
    55. waitKey(0);
    56. return 0;
    57. }
  • 相关阅读:
    java93-线程的创建方法二
    系统可靠性分析与设计
    mysql json字段使用以及常用json函数,配合springBoot和mybatis-plus简化开发
    《学术小白学习之路13》基于DTM和主题共现网络——实现主题时序演化网络分析(数据代码在结尾)
    数据资产入表,给企业带来的机遇和挑战
    Activiti监听器
    FGUI编辑器插件开发(不推荐使用,当做参考吧)
    什么叫做虚基类,它有何作用
    现代化个人博客系统 ModStartBlog v5.6.0 备案信息完善,功能组件优化
    云计算———虚拟化技术镜像的构建及Harbor的使用(三)
  • 原文地址:https://blog.csdn.net/zjjaibc/article/details/126461361