2012-07-13 57 views
0

我需要發送一個圖像到一個字節數組使用c + +/cli。圖像最初是Iplimage格式。元帥::複製CvMat

int img_sz1 = img1->width * img1->height * img1->nChannels; 
    array <Byte>^ hh1 = gcnew array<Byte> (img_sz1); 
    Marshal::Copy((IntPtr)img->imageData, hh1, 0, img_sz1); 

它工作正常。

我添加了編碼步驟,將其作爲JPEG

CvMat* buf1 = cvEncodeImage(".jpeg", img1, jpeg_params); 
    img_sz1=buf1->width*buf1->height 
    Marshal::Copy((IntPtr)buf1, hh1, 0, img_sz1); 

,現在它編譯罰款,但給我的名帥錯誤:複製線

An unhandled exception of type 'System.AccessViolationException' occurred in 
mscorlib.dll. Additional information: Attempted to read or write protected memory. 

任何幫助是非常讚賞。

+0

在後一種情況下,你是否還在調整'hh1'來匹配'buf1-> width * buf1-> height'而不是'img1-> width * img1-> height * img1-> nChannels'?或者,也可能是'buf1-> width * buf1-> height'錯誤地不考慮'nChannels'? – ildjarn 2012-07-13 01:51:57

+0

編碼緩衝區中沒有nchannels參數。可能是因爲所有通道現在都編碼成一個jpeg流。 – fmvpsenior 2012-07-13 16:27:25

回答

1

cvEncodeImage的返回是一個單行矩陣,包含編碼的圖像數據。你現在正在複製的是結構本身,例如寬度字段,高度字段等。我相信你需要從buf1->data中複製。

+0

我試過buf1-> data.ptr並編譯。我仍然需要測試我是否得到正確的圖像。 – fmvpsenior 2012-07-13 18:54:24