我試圖從通過網絡攝像頭獲得的圖像中獲得一個稱爲testdata的單浮動矢量。一旦圖像被轉換爲單個浮動矢量,它就被傳遞給訓練好的神經網絡。爲了測試網絡,我使用函數float CvANN_MLP: :預測(const Mat &輸入,Mat &輸出)。該功能需要測試樣本的格式如下: -如何將圖像存儲爲1 * n浮點向量?
輸入向量的浮點矩陣,每行一個向量。
TESTDATA向量被定義爲如下: -
// define testing data storage matrices
//NumberOfTestingSamples is 1 and AttributesPerSample is number of rows *number of columns
Mat testing_data = Mat(NumberOfTestingSamples, AttributesPerSample, CV_32FC1);
爲了存儲圖像的每個行中的CSV格式,i執行以下操作: -
Formatted row0= format(Image.row(0),"CSV"); //Get all rows to store in a single vector
Formatted row1= format(Image.row(1),"CSV"); //Get all rows to store in a single vector
Formatted row2= format(Image.row(2),"CSV"); //Get all rows to store in a single vector
Formatted row3= format(Image.row(3),"CSV"); //Get all rows to store in a single vector
我然後輸出所有格式化行其儲存在ROW0到ROW3成文本文件作爲例如: -
store_in_file<<row0<<", "<<row1<<", "<<row2<<", "<<row3<<endl;
這將存儲整個馬t在一條線上。
文本文件被關閉.I重新打開相同的文本文件以提取存儲到載體TESTDATA
// if we can't read the input file then return 0
FILE* Loadpixel = fopen("txtFileValue.txt", "r");
if(!Loadpixel) // file didn't open
{
cout<<"ERROR: cannot read file \n";
return 0; // all not OK;
}
for(int attribute = 0; attribute < AttributesPerSample; attribute++)
{
fscanf(Loadpixel, "%f,",&colour_value);//Reads a single attribute and stores it in colour_value
testdata.at<float>(0, attribute) = colour_value;
}
這工作的數據,在一段時間內的文件未打開後然而,並顯示錯誤消息:「錯誤:無法讀取文件」。此方法有很多限制,無需花費時間將其存儲在文本文件中,然後重新打開並提取。將圖像(Mat)存儲到單個浮點的最佳方式是什麼矢量類似於testdata.at<float>(0, attribute)
?還是有一種簡單的方法來確保文件始終打開,基本上是正確的問題?
你可能想澄清什麼'Formatted'和'Image'是。 – molbdnilo
你確實意識到你寫的東西在你的腦海中可能是有意義的,但是對其他人來說是完全安全的?爲它設置一些上下文,描述這些「向量」是什麼,爲使用的變量提供聲明,描述問題的「大圖」。 –
我剛剛解決了發佈的問題,以解釋什麼我實現更詳細,希望這可以幫助。變量「圖像」是從網絡攝像頭獲得一個墊子。使用opencv,墊子可以從默認格式轉換爲CSV格式使用: - 格式化row0 =格式(Image.row(0),「CSV」); – Sade