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<Point3f> 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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
OpenCV中,reshape函数用于改变矩阵的形状。该函数可以改变矩阵的通道数,并且可以对矩阵元素进行序列化,而无需复制数据。如果要改变矩阵的通道数,可以使用reshape函数的第一个参数来指定新的通道数。如果要将矩阵序列化为行向量或列向量,可以使用reshape函数的第二个参数。例如,如果我们有一个3x4的矩阵A,可以使用A.reshape(1, 12)将其序列化为一个大小为12的行向量B。但是不能使用A.reshape(1, 3, 4)这样的形式来改变矩阵的形状。在OpenCV中,矩阵的序列化是按行进行的,从上到下,从左到右。所以,对于一个2x3的矩阵data,可以使用data.reshape(0, 6)将其序列化为一个列向量,使用data.reshape(0, 1)将其序列化为一个行向量。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Opencvreshape函数](https://blog.csdn.net/jpc20144055069/article/details/102928418)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [OpenCV中的reshape](https://blog.csdn.net/yang6464158/article/details/20129991)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

视图猿人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值