OpenCV之reshape函数

函数原型:

/** @brief Changes the shape and/or the number of channels of a 2D matrix without copying the data.
    The method makes a new matrix header for \*this elements. The new matrix may have a different size
    and/or different number of channels. Any combination is possible if:
    -   No extra elements are included into the new matrix and no elements are excluded. Consequently,
        the product rows\*cols\*channels() must stay the same after the transformation.
    -   No data is copied. That is, this is an O(1) operation. Consequently, if you change the number of
        rows, or the operation changes the indices of elements row in some other way, the matrix must be
        continuous. See Mat::isContinuous .
    For example, if there is a set of 3D points stored as an STL vector, and you want to represent the
    points as a 3xN matrix, do the following:
    @code
        std::vector vec;
        ...
        Mat pointMat = Mat(vec). // convert vector to Mat, O(1) operation
                          reshape(1). // make Nx3 1-channel matrix out of Nx1 3-channel.
                                      // Also, an O(1) operation
                             t(); // finally, transpose the Nx3 matrix.
                                  // This involves copying all the elements
    @endcode
    @param cn New number of channels. If the parameter is 0, the number of channels remains the same.
    @param rows New number of rows. If the parameter is 0, the number of rows remains the same.
     */
   Mat reshape(int cn, int rows=0) const;

 cn表示通道数,默认为0,表示跟原图通道数一致,

rows表示行数,rows默认为0表示跟原图排列一致;

若原图大小为MxN

当rows=1时,输出矩阵为1x(M*N);

当rows=M*N时,输出 (M*N)x1。

测试1:

Mat src(3, 3, CV_8UC1, Scalar(1));//通道数为1的3*3矩阵,值为1
Mat dst = src.reshape(0, 1);
cout << dst << endl;

输出:

[ 1,  1,  1,  1,  1,  1,  1,  1,  1]

 测试2:

Mat src(3, 3, CV_8UC1, Scalar(2));//通道数为1的3*3矩阵,值为1
Mat dst = src.reshape(0, 3*3);
cout << dst << endl;

输出:

[ 1; 1; 1;  1;  1;  1;  1;  1;  1]

你可能感兴趣的:(图像视频处理,opencv,人工智能,计算机视觉)