zoukankan      html  css  js  c++  java
  • opencv学习

    编译

    gcc Test.c -o Test `pkg-config --cflags --libs opencv`

    判断是否正确读入的方法:

    if( argc != 2 || !(src=imread(argv[1], 1)).data )
      return -1;

    ---

     if( src.empty() )

      { return -1; }


    颜色通道转换:

    cvtColor(src, gray, COLOR_BGR2GRAY);

    cvtColor(src, hsv, COLOR_BGR2HSV);


    Mat属性:

    mat.channels();//通道个数

    mat.depth();//每个像素用多少位表示 


    Mat的构造函数和工厂函数:

    Mat M(2,2, CV_8UC3, Scalar(0,0,255));

    M.create(4,4, CV_8UC(2));//You cannot initialize the matrix values with this construction. It will only reallocate its matrix data memory if the new size will not fit into the old one.

    Mat E = Mat::eye(4,4, CV_64F);

    Mat O = Mat::ones(2,2, CV_32F);

    Mat Z = Mat::zeros(3,3, CV_8UC1);

    Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0); //For small matrices you may use comma separated initializers

    randu(R, Scalar::all(0), Scalar::all(255)); //need to give the lower and upper value for the random values

    vector<float> v; v.push_back( (float)CV_PI);  v.push_back(2);  v.push_back(3.01f); cout << "Vector of floats via Mat = " << Mat(v) << endl << endl; //[3.1415;2;3.01]


    Region of Interest:

    Mat D(A, Rect(10, 10, 100, 100));

    Mat E = A(Range::all(), Range(1,3));

    Mat RowClone = C.row(1).clone();//Create a new header for an existing Mat object


    specify the data type to use for storing the elements and the number of channels per matrix point.

    CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number]

    If you need more, you can create the type with the upper macro:

    int sz[3] = {2,2,2};

    Mat L(3,sz, CV_8UC(1), Scalar::all(0)); //create a matrix with more than two dimensions


    输出格式化 Output formatting

    cout << "R (default) = " << endl << R << endl << endl;
    cout << "R (python) = " << endl << format(R,"python") << endl << endl;
    cout << "R (csv) = " << endl << format(R,"csv" ) << endl << endl;
    cout << "R (numpy) = " << endl << format(R,"numpy" ) << endl << endl;
    cout << "R (c) = " << endl << format(R,"C" ) << endl << endl;

    //参考http://docs.opencv.org/2.4/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html#output-formatting


    遍历Mat中的像素有三种方法,简单记录,具体查看http://docs.opencv.org/2.4.13/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html

    //第一种方法
    for( i = 0; i < nRows; ++i) {   p = I.ptr<uchar>(i);   for ( j = 0; j < nCols; ++j)   {     p[j] = table[p[j]]; //其中p[j]就是像素值   } }
    //第二种方法
    switch(channels) { case 1: { MatIterator_<uchar> it, end; for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it) *it = table[*it]; break; } case 3: { MatIterator_<Vec3b> it, end; for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it) { (*it)[0] = table[(*it)[0]]; (*it)[1] = table[(*it)[1]]; (*it)[2] = table[(*it)[2]]; } } }
    //第三种方法
    switch(channels) { case 1: { for( int i = 0; i < I.rows; ++i) for( int j = 0; j < I.cols; ++j ) I.at<uchar>(i,j) = table[I.at<uchar>(i,j)]; break; } case 3: { Mat_<Vec3b> _I = I; for( int i = 0; i < I.rows; ++i) for( int j = 0; j < I.cols; ++j ) { _I(i,j)[0] = table[_I(i,j)[0]]; _I(i,j)[1] = table[_I(i,j)[1]]; _I(i,j)[2] = table[_I(i,j)[2]]; } I = _I; break; } }
    //最后一种方法使用LUT函数
    Mat lookUpTable(1, 256, CV_8U); uchar* p = lookUpTable.data; for( int i = 0; i < 256; ++i) p[i] = table[i]; LUT(I, lookUpTable, J);

    Mat数据类型和像素值的放缩:

    Mat a; a.convertTo(dst, data_type, alpha, beta); 

      


    Mat基本运算

    矩阵相减,subtract(src2, src1, dst, noArray(), DataType<sift_wt>::type);  //其中一个是Mask

    通道分割,vector<Mat> bgr_planes; split(src, bgr_planes);

    矩阵转置,transpose(src, dst); 


    数学函数

    cv::hal::fastAtan2(Y, X, Ori, len, true); //计算梯度方向
    cv::hal::magnitude32f(X, Y, Mag, len); //计算幅值
    cv::hal::exp32f(W, W, len); //计算权重

  • 相关阅读:
    ccBPM典型的树形表单和多表头表单的流程示例
    Arrays -数组工具类,数组转化字符串,数组排序等
    String
    ArrayList
    Random
    Scanner
    Phone-java标准类
    HelloWorld-java
    c++ 由无向图构造邻接表,实现深度优先遍历、广度优先遍历。
    c++实现哈夫曼树,哈夫曼编码,哈夫曼解码(字符串去重,并统计频率)
  • 原文地址:https://www.cnblogs.com/Key-Ky/p/5899038.html
Copyright © 2011-2022 走看看