我試圖將(雙)的矩陣轉換成一個無符號的字符,然後我可以寫入一個.pmg文件...但它不工作。將double轉換爲unsigned char?
void writePNG(vector<double>& matrix)
{
vector<unsigned char> image;
ofstream myfile;
myfile.open("newFile.txt", ios::out); // writing to .txt file for now for testing.
if(!myfile.is_open())
{
cout << "Cannot open file";
}
for(int i=0; (i < 512*512); i++)
{
image[i] = (unsigned char) matrix[i];
}
myfile.close();
}
它不會轉換數據。有任何想法嗎??謝謝:)
到底是什麼錯誤?是否有編譯器錯誤?該文件是否爲空(這肯定是這種情況,因爲您不寫任何內容)?有沒有運行時錯誤? – alfa 2012-04-11 14:45:48
將聲明更改爲'vector圖像(512 * 512);'。 –
2012-04-11 14:46:16
那麼你看到的錯誤是什麼?矩陣向量中值的範圍是什麼?例如。如果-1 ... 1,那麼你需要首先縮放值。 – 2012-04-11 14:47:48