OpenCV reshape The Matrix is not continuous, thus its number of rows can not be changed

 

When using OpenCV  reshape and gets this error:

OpenCV Error: Image step is wrong (The matrix is not continuous, thus its number
 of rows can not be changed) in unknown function,

Let's look at the documentation of the reshape function, Wrong parameters can also cause this error

According to the documentation the signature is

Mat Mat::reshape(int cn, int rows=0) const

With the following meaning of the arguments:

  • cn – New number of channels. If the parameter is 0, the number of channels remains the same.
  • rows – New number of rows. If the parameter is 0, the number of rows remains the same.

Note that the number of columns is implicit -- it's calculated from the existing matrix properties and the two parameters.

According to this, the code

data = mu.reshape(5, 5);

creates a 5-channel matrix of 5 rows and 1 column.

In order to reshape you matrix to a single channel 5x5 matrix, you have to do the following:

data = mu.reshape(1, 5);

Alternately, since the input matrix is already single channel, you can also use

data = mu.reshape(0, 5);

 

Besides:

there are 3 cases, where you'd get non-continuous Mat's.

  • it's a roi
  • bmp images and the like sometimes come padded
  • you made a mat from a pixel pointer ( i.e some non-opencv capture device ) and that might be padded, too

since you have to reorder the memory in all those cases, checking for continuity and a clone() acll will solve it.

 
if ( ! mat.isContinuous() )
{ 
    mat = mat.clone();
}

 

 

参考:

 https://stackoverflow.com/questions/36191118/opencv-reshape-function-does-not-work-properly

 https://answers.opencv.org/question/22742/create-a-memory-continuous-cvmat-any-api-could-do-that/

转载于:https://www.cnblogs.com/jins-note/p/10901277.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值