2010-11-24 13 views
4

我在緩衝區jpegBuffer中有一個jpeg圖像。我試圖將它傳遞給CV :: imdecode功能:如何使用cv :: imdecode,如果圖像文件的內容在char數組中?

Mat matrixJprg = imdecode(Mat(jpegBuffer), 1); 

我得到這個錯誤:

/home/richard/Desktop/richard/client/src/main.cc:108: error: no matching function for call to ‘cv::Mat::Mat(char*&)’ 

這是我如何填寫jpegBuffer:

FILE* pFile; 
long lSize; 
char * jpegBuffer; 
pFile = fopen ("img.jpg", "rb"); 
if (pFile == NULL) 
{ 
    exit (1); 
} 

// obtain file size. 
fseek (pFile , 0 , SEEK_END); 
lSize = ftell (pFile); 
rewind (pFile); 

// allocate memory to contain the whole file. 
jpegBuffer = (char*) malloc (lSize); 
if (jpegBuffer == NULL) 
{ 
    exit (2); 
} 

// copy the file into the buffer. 
fread (jpegBuffer, 1, lSize, pFile); 

// terminate 
fclose (pFile); 

回答

15

墊有沒有構造函數接受char *參數。試試這個:

std::ifstream file("img.jpg"); 
std::vector<char> data; 

file >> std::noskipws; 
std::copy(std::istream_iterator<char>(file), std::istream_iterator<char>(), std::back_inserter(data)); 

Mat matrixJprg = imdecode(Mat(data), 1); 

編輯:

你也應該看看LoadImageM

如果你的數據已經存在char *緩衝區中,一種方法是將數據複製到std :: vector中。

std::vector<char> data(buf, buf + size); 
+1

非常感謝。但是如果我只在緩衝區中存在圖像(例如char * jpegBuffer),並且它不作爲文件保存在磁盤上呢?那麼我如何將緩衝區發送給imdecode呢?上面的代碼只是一個小測試,但後來我只是將jpg圖像從wifi連接讀取到緩衝區而不保存到文件。 – 2010-11-24 22:35:15

相關問題