Ask Your Question
0

Convert Mat to 16 bit int

asked 2016-11-18 01:48:21 -0600

ManuKlause gravatar image

Hi,

I want to convert my 16 bit images from format cv::Mat to 16 bit. Currently, I am using this function

int size = image.total() * image.elemSize(); // number of elements * size of elements
float * bytes = new float[size];  
std::memcpy(bytes, &image.data, size * sizeof(float));
return bytes;

which only allows me to use 8 bit images (format bytes). Do you know a function call to convert cv::Mat to my desired bit format?

Best

ManuKlause

edit retag flag offensive close merge delete

Comments

aww dear, code above is all wrong (why float? size is also already in bytes)

can't you just use mat1.convertTo(mat2, CV_16U)

berak gravatar imageberak ( 2016-11-18 01:53:24 -0600 )edit

I want to use the data of cv::Mat in CUDA, and CUDA does not support cv::Mat. So, using mat1.convertTo(mat2, CV_16U) wouldnt be helpful.

ManuKlause gravatar imageManuKlause ( 2016-11-18 01:59:11 -0600 )edit

wait, i probably misread it. are those Mat's 16bit already ?

you could still get ushort*ps = mat.ptr<ushort>()

berak gravatar imageberak ( 2016-11-18 02:04:11 -0600 )edit
1

Yes, they are 16 bit.

ManuKlause gravatar imageManuKlause ( 2016-11-18 02:12:13 -0600 )edit

ah, ok, does not need any conversion , then (my bad)

berak gravatar imageberak ( 2016-11-18 02:13:14 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2016-11-18 02:18:36 -0600

berak gravatar image

updated 2016-11-18 02:51:04 -0600

if you have a 16bit Mat (CV_16U), please access the underlying memory like:

CV_Assert(mat.type() == CV_16U); // make sure !    
ushort * ps = mat.ptr<ushort>(); // ptr to 1st element

if you need a copy of that, it's :

size_t len = mat.total() * mat.elemSize(); // in bytes
ushort *cs = new ushort[len]; // *not* float!
memcpy(cs, mat.ptr<ushort>(), len);
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-11-18 01:48:21 -0600

Seen: 6,187 times

Last updated: Nov 18 '16