opencv - EmguCV - Mat.Data array is always null after image loading -


when trying read image file, after load mat.data array alway null. when looking mat object during debug there byte array in data image.

mat image1 = cvinvoke.imread("minion.bmp", emgu.cv.cvenum.loadimagetype.anydepth); 

do have idea why?

i recognize question super old, hit same issue , suspect answer lies in emgu wiki. specifically:

accessing pixels mat

unlike image<,> class, memory pre-allocated , fixed, memory of mat can automatically re-allocated open cv function calls. cannot > pre-allocate managed memory , assume same memory used through life time of mat object. result, mat class not contains data > property image<,> class, pixels can access through managed array. access data of mat, there few possible choices. easy way , safe way cost additional memory copy

the first option copy mat image<,> object using mat.toimage function. e.g.

image<bgr, byte> img = mat.toimage<bgr, byte>();

the pixel data can accessed using image<,>.data property.

you can convert mat matrix<> object. assuming mat contains 8-bit data,

matrix<byte> matrix = new matrix<byte>(mat.rows, mat.cols, mat.numberofchannels); mat.copyto(matrix); 

note should create matrix<> matching type mat object. if mat contains 32-bit floating point value, should replace matrix in above code matrix. pixel data can accessed using matrix<>.data property. fastest way no memory copy required. caution!!!

the second option little bit tricky, provide best performance. require know size of mat object before created. can allocate managed data array, , create mat object forcing use pinned managed memory. e.g.

//load 3 channel bgr image here mat m1 = ...;  //3 channel bgr image data, if single channel, size should m1.width * m1.height  byte[] data = new byte[m1.width * m1.height * 3];` gchandle handle = gchandle.alloc(data, gchandletype.pinned);` using (mat m2 = new mat(m1.size, depthtype.cv8u, 3, handle.addrofpinnedobject(), m1.width * 3))`     cvinvoke.bitwisenot(m1, m2);` handle.free(); 

at point data array contains pixel data of inverted image. note if mat m2 allocated wrong size, data[] array contains 0s, , no exception thrown. careful when performing above operations.

tl;dr: can't use data object in way you're hoping (as of version 3.2 @ least). must copy object allows use of data object.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -