2013-10-04 32 views
0

我試圖從通過網絡攝像頭獲得的圖像中獲得一個稱爲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)?還是有一種簡單的方法來確保文件始終打開,基本上是正確的問題?

+0

你可能想澄清什麼'Formatted'和'Image'是。 – molbdnilo

+0

你確實意識到你寫的東西在你的腦海中可能是有意義的,但是對其他人來說是完全安全的?爲它設置一些上下文,描述這些「向量」是什麼,爲使用的變量提供聲明,描述問題的「大圖」。 –

+0

我剛剛解決了發佈的問題,以解釋什麼我實現更詳細,希望這可以幫助。變量「圖像」是從網絡攝像頭獲得一個墊子。使用opencv,墊子可以從默認格式轉換爲CSV格式使用: - 格式化row0 =格式(Image.row(0),「CSV」); – Sade

回答

0

理智的解決方案當然是直接在內存中轉換值。正如你懷疑的那樣,整個檔案中間是一個令人難以置信的混亂。

如果您使用標準C++類型,如std::vector,我們可以提供實際的代碼。相當於您的代碼的簡單算法是一次只遍歷一個像素的2D圖像,並將每個像素的值附加到1D向量的背面。

但是,無論如何,這對網絡攝像頭圖像的神經網絡處理來說是個壞主意。如果您的輸入向下移動一個像素 - 完全可能 - 整個1D矢量會改變。因此建議首先將輸入標準化。這可能需要先翻譯,縮放和旋轉圖像。

[編輯] 標準C++例如:

std::vector<std::vector<int>> Image2D; 
std::vector<float> Vector1D; 
for (auto const& row : Image2D) { 
    for (auto pixel : row) { 
    Vector1D.push_back(pixel); 
    } 
} 
+0

將圖像裁剪爲標準大小,然後進行標準化,均衡並轉換爲二進制圖像。此過程是正確的並且預測得很好,問題在於某些圖像文件無法打開後,我不想使用文本文件獲取Image.May的浮點向量,請提供一個示例,詳細說明您的建議方法? – Sade