Lucien Song Lucien Song - 1 month ago 11
C++ Question

opencv Mat 16 bits from QVector

It's perhaps a very basic question.
I want to build a cv::Mat from QVector. It works well in 8 bits Mat.

QVector<uchar> vector;
vector<<1<<289<<3000<<7<<2<<255<<266<<288;
Mat testMat(2,4,CV_8UC1,vector.data());


output:

[ 1, 33, 184, 7;
2, 255, 10, 32]


But when i use 16 bits Mat.

QVector<uint> vector;
vector<<1<<289<<3000<<7<<2<<255<<266<<288;
Mat testMat(2,4,CV_16UC1,vector.data());


The result becomes:

[1, 0, 289, 0;
3000, 0, 7, 0]


My question is why this happened and how to show without these 0.
Thank you!

Answer

CV_16UC1 has underlying type unsigned short, but not uint. Problem caused by different size of these types. Define your vector as QVector<unsigned short> vector;. This should fix the problem:

QVector<unsigned short> vector;
vector<<1<<289<<3000<<7<<2<<255<<266<<288; 
Mat testMat(2,4,CV_16UC1,vector.data());