2015-08-25 22 views
0

我正在處理使用指定算法MSE和SSI處理圖像以用於比較圖像結構的android應用程序。從文件夾中讀取所有圖像文件進行處理

我有工作代碼,它允許我從選定的文件夾中選擇兩個圖像並對它們執行所需的操作,但下一步是允許用戶選擇一個文件夾,然後它會自動讀取所有圖像並執行所需的操作並保存被發現相似的圖像。

我有下面的代碼,但在一分鐘它正在通過圖像工作,它最終會通過超時錯誤。

有什麼我做錯了,或者我應該改變,以便順利運行。有需要處理的文件夾中有172個圖像。

public void addItemsOnSpinner() { 
    spinner1 = (Spinner) findViewById(R.id.spinner); 
    List<String> list = new ArrayList<String>(); 
    list.add("New images"); 
    list.add("All downloads"); 
    list.add("Bluetooth"); 
    list.add("Camera shots"); 
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, list); 
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner1.setAdapter(dataAdapter); 
    selection = String.valueOf(spinner1.getSelectedItem()); 
} 

@Override 
public void onClick(View v) { 
    int n =0; 
    if (v == submit) { 
     for (int i = 1; i <= 172; i++) { 
      for (int j = 1; j <= 172; j++) { 
       absoluteFilePath = folder + "/" + selection + "/image" + i + ".bmp"; 
       bMap1 = BitmapFactory.decodeFile(absoluteFilePath); 
       absoluteFilePath2 = folder + "/" + selection + "/image" + j + ".bmp"; 
       bMap2 = BitmapFactory.decodeFile(absoluteFilePath2); 
       Toast.makeText(LoadFile.this, "Comparing Image" + i + " against Image" + j, 
         Toast.LENGTH_SHORT).show(); 
       //loop through each image compare image by image 
       calculateMSE(bMap1, bMap2, 256, 256); 
       calculateSSI(bMap1, bMap2, 4); 
       //calculateSSI(bMap1, bMap2, 8); 
       //calculateSSI(bMap1, bMap2, 16); 
       //calculateSSI(bMap1, bMap2, 32); 
       //if images are similar save file paths to array 
       if(calculateMSE(bMap1, bMap2, 256, 256) <= 0.4 && calculateSSI(bMap1, bMap2, 4) >= 0.6){ 
        ImageView image1 = (ImageView) findViewById(R.id.imgView); 
        image1.setImageBitmap(bMap1); 
        ImageView image2 = (ImageView) findViewById(R.id.imgView2); 
        image2.setImageBitmap(bMap2); 

        //saving file path to array 
        bitmapArray1 [n] = bMap1; 
        bitmapArray2 [n] = bMap2; 
        n++; 
       } 
+0

嘗試用較少的圖像。如果可行,很有可能您的內存不足 –

+0

請提供LogCat輸出的錯誤。 '允許我從所選文件夾中選擇兩個圖像'的代碼' – Fildor

+0

'。如果你需要兩個兩個圖像,那麼你會如何從目錄列表中選擇它們? '在當前正在處理圖像的時刻'?請詳細解釋你的意思。 '它最終會通過一個超時錯誤'。請更好地解釋發生了什麼。 – greenapps

回答

0

您可能會在UI線程上做很多工作來進行計算。運行一個AsyncTask來運行後臺線程。更新進度更新和完成後的UI。

典型用途:

// in your onclick method. in function if(v == submit) 
new AsyncTask(n, bitmapArray1, bitmapArray2).execute(); 

// in AsyncTask class 
public AsyncTask extends AsyncTask< Boolean, Boolean, Void) 
{ 

int n; 
Bitmap[] bitmapArray1, bitmapArray2; 

public AsyncTask (int n, Bitmap[] bitmapArray1, Bitmap[] bitmapArray2) 
{ 
    this.n = n; 
    this.bitmapArray1 = bitmapArray1; 
    this.bitmapArray2 = bitmapArray2; 
} 

@Override 
protected void onPreExecute() 
{} 

@Override 
protected void onPostExecute(Boolean bool) 
{ 
    if (bool) 
    { 
    ImageView image1 = (ImageView) findViewById(R.id.imgView); 
       image1.setImageBitmap(bMap1); 
       ImageView image2 = (ImageView) findViewById(R.id.imgView2); 
       image2.setImageBitmap(bMap2); 

       //saving file path to array 
       bitmapArray1 [n] = bMap1; 
       bitmapArray2 [n] = bMap2; 
    } 
} 

@Override 
protected void onProgressUpdate(Bitmap result) 
{ 
    // update UI here. 
    // best UI update is the progressbar. 
    Toast.makeText(LoadFile.this, "Comparing Image" + i + " against Image" + j,Toast.LENGTH_SHORT).show(); 
} 

@Override 
protected Boolean doInBackground(Void... params) 
{ 
    for (int i = 1; i <= 172; i++) { 
     for (int j = 1; j <= 172; j++) { 
      absoluteFilePath = folder + "/" + selection + "/image" + i + ".bmp"; 
      bMap1 = BitmapFactory.decodeFile(absoluteFilePath); 
      absoluteFilePath2 = folder + "/" + selection + "/image" + j + ".bmp"; 
      bMap2 = BitmapFactory.decodeFile(absoluteFilePath2); 

      //loop through each image compare image by image 
      calculateMSE(bMap1, bMap2, 256, 256); 
      calculateSSI(bMap1, bMap2, 4); 
      //calculateSSI(bMap1, bMap2, 8); 
      //calculateSSI(bMap1, bMap2, 16); 
      //calculateSSI(bMap1, bMap2, 32); 
      //if images are similar save file paths to array 
      if(calculateMSE(bMap1, bMap2, 256, 256) <= 0.4 && calculateSSI(bMap1, bMap2, 4) >= 0.6){ 
       n++; 
       return true; 
      } else 
       return false; 
      } 
     } 
    } 
     return null; 
} 
} 
相關問題