2016-07-19 11 views
1

私はAPIを使用して画像内の顔を検出しています。しかし、私は顔にイメージをトリミングする方法を見つけることができませんでした。私はビットマップを切り抜く方法を知っていますが、ビットマップと幅と高さで顔の左上の位置を取得する必要があります。私は顔検出API座標

points = face.getPosition(); 

    Bitmap bmp = Bitmap.createBitmap(bit,(int)points.x,(int)(-1.0*points.y),(int)face.getWidth(),(int)face.getHeight()); 

しかしを使用して、左上位置を照会するとき、私はポイントを見たときに、I yは-63.5555であり、xは235.6666であることに気づきます。なぜ、負のy座標があるのか​​わかりません。私はいくつかのデバッグを行い、顔オブジェクトの内部を見ました。私はそれがすでに正のxとyの座標を持つPointFオブジェクトを含んでいることが分かりました。では、この場合、負のy座標が返されるのはなぜですか?

答えて

3

あるバウンディングボックスは、それが写真内に完全に表示されない場合であっても、頭の大きさを推定します。顔が画像の上端または左端によって切り取られた場合、座標は負であってもよい(例えば、ヘッドの頂部が画像の上から切り取られ、結果としてy座標が0より大きい)。

デバッグ時に表示される違いは、実装が内部的に頭の中心位置を使用して(目のほぼ中間点にある)位置を表すという事実によるものですが、APIはこれを、あなたの便宜のために、getPositionを呼び出すときに左の位置に移動します。

境界ボックスは必ずしも顔のタイトな境界である必要はありません。よりぴったり合うようにするには、ランドマークの検出を有効にし、返されたランドマークに対してクロッピングのレベルを計算する必要があります。

0

これまで同じAPIを使用していて、顔を正常にトリミングできました。

  //Crop face option 

      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
      //Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath(), options); 
      Bitmap bitmap = getRotatedImageToUpload(pictureFile.getAbsolutePath()); 


      Bitmap faceBitmap = Bitmap.createBitmap(bitmap, (int) faceCentre.x, (int) faceCentre.y, (int) faceWidth, (int) faceHeight); 

      FileOutputStream out = null; 
      try { 
       out = new FileOutputStream(getOutputMediaFile()); 
       faceBitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance 
       // PNG is a lossless format, the compression factor (100) is ignored 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } finally { 
       try { 
        if (out != null) { 
         out.close(); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

      //End of Crop face option 

を試してみて、getRotateImageToUploadのコードが

public Bitmap getRotatedImageToUpload(String filePath) { 
     try { 
      String file = filePath; 
      BitmapFactory.Options bounds = new BitmapFactory.Options(); 
      bounds.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(file, bounds); 

     BitmapFactory.Options opts = new BitmapFactory.Options(); 
     Bitmap bm = BitmapFactory.decodeFile(file, opts); 
     ExifInterface exif = null; 

     exif = new ExifInterface(file); 

     String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION); 
     int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL; 

     int rotationAngle = 0; 
     if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90; 
     if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180; 
     if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270; 

     Matrix matrix = new Matrix(); 
     matrix.setRotate(rotationAngle, (float) bm.getWidth()/2, (float) bm.getHeight()/2); 
     Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true); 
     return rotatedBitmap; 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 
+0

ビジュアルAPIのオーバーレイで画像をキャプチャするにはどうすればよいですか? –

関連する問題