2015-06-14 99 views
0

我正在使用MATLAB進行與圖像識別相關的項目,目前我正在使用Android應用程序來幫助進行一些預處理步驟。我認爲用矩陣代替位圖很容易。我終於設法完成我的算法並將其導入Eclipse。問題是,我意識到我不知道如何將Bitmap圖像轉換爲MATLAB可以讀取的算法。將Android圖像轉換爲MATLAB可以讀取的文件

你對我如何做到這一點有什麼想法嗎?

回答

1

如果我正確解釋了您的問題,您將在Bitmap類中存儲圖像,並且希望將其保存到Android設備的本地文件中。然後,您需要將此圖像加載到MATLAB中以用於圖像識別算法。

鑑於您的圖像通過Android是在內存中,可以使用方法compresshttp://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream

,那麼你會使用這一點,並保存到文件的圖像,然後你就可以將其加載到MATLAB,例如使用imread

以下是您可以爲Android應用程序編寫的一些示例代碼。假設你的位圖實例存儲在一個名爲bmp變量,這樣做:

FileOutputStream out = null; // For writing to the device 
String filename = "out.png"; // Output file name 

// Full path to save 
// This accesses the pictures directory of your device and saves the file there 
String output = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), filename); 

try { 
    out = new FileOutputStream(filename); // Open up a new file stream 
    // Save the Bitmap instance to file 
    // First param - type of image 
    // Second param - Compression factor 
    // Third param - The full path to the file 
    // Note: PNG is lossless, so the compression factor (100) is ignored 
    bmp.compress(Bitmap.CompressFormat.PNG, 100, out); 
} 
// Catch any exceptions that happen 
catch (Exception e) { 
    e.printStackTrace(); 
} 
// Execute this code even if exception happens 
finally { 
    try { 
     // Close the file if it was open to write 
     if (out != null) 
      out.close(); 
    } 
    // Catch any exceptions with the closing here 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

上面的代碼將節省您的設備上的圖像到您的默認圖片目錄。一旦你拉出來的形象,您可以通過使用imread讀取圖像到MATLAB:因此

im = imread('out.png'); 

im是,你現在可以使用您的圖像識別算法在圖像的原始RGB像素。

+1

對不起,如果我沒有正確解釋我自己,通過一個Matlab圖像我的意思是說。謝謝你的回答,這真的很有用 – AlvaroNav

+0

@AlvaroNav - 完全沒問題。很高興我能幫上忙! – rayryeng