2017-04-24 15 views
1

カメラから取得したYUV_420_888画像があります。私は画像処理アルゴリズムに供給するためにこの画像のグレースケールから矩形を切り抜きたい。これは私がこれまで持っているものです。カメラ2から長方形を切り抜く画像

public static byte[] YUV_420_888toCroppedY(Image image, Rect cropRect) { 
    byte[] yData; 

    ByteBuffer yBuffer = image.getPlanes()[0].getBuffer(); 

    int ySize = yBuffer.remaining(); 

    yData = new byte[ySize]; 

    yBuffer.get(yData, 0, ySize); 

    if (cropRect != null) { 

     int cropArea = (cropRect.right - cropRect.left) * (cropRect.bottom - cropRect.top); 

     byte[] croppedY = new byte[cropArea]; 

     int cropIndex = 0; 

     // from the top of the rectangle, to the bottom, sequentially add rows to the output array, croppedY 
     for (int y = cropRect.top; y < cropRect.top + cropRect.height(); y++) { 

      // (2x+W) * y + x 
      int rowStart = (2*cropRect.left + cropRect.width()) * y + cropRect.left; 

      // (2x+W) * y + x + W 
      int rowEnd = (2*cropRect.left + cropRect.width()) * y + cropRect.left + cropRect.width(); 

      for (int x = rowStart; x < rowEnd; x++) { 
       croppedY[cropIndex] = yData[x]; 
       cropIndex++; 
      } 
     } 

     return croppedY; 
    } 

    return yData; 
} 

この関数は、エラーなしで実行されますが、私はそれから抜け出す画像がゴミである - それは次のようになります。

enter image description here

私はありませんこの問題を解決する方法や私が間違っていることを確認してください。

+2

私はあなたが私はそれを持っていない元の画像 –

+0

@YandryPozoを投稿すべきだと思う、私は私のプレビュー画面を通じて見て、それはあなたがかもしれないと – Carpetfizz

+1

ようには見えません。 grallocとandroidのバッファを読み込んで答えを見つけてください:https://source.android.com/devices/graphics/arch-bq-grallocこれは、バッファの幅(バイト数)が画像の幅(ピクセル単位)と異なる理由を説明する素敵な概要です:https://www.codeproject.com/articles/991640/androids-graphics-buffer-management-system -part-i – l85m

答えて

1

あなたのrowStart/end計算は間違っています。

トリミングウィンドウのサイズではなく、ソースイメージのサイズに基づいて行の開始位置を計算する必要があります。そして、私はあなたが2の要因をどこから得るか分からない。画像のYチャンネルに1ピクセルあたり1バイトがあります。

彼らはおおよそ次のようになります。

int yRowStride = image.getPlanes()[0].getRowStride(); 
.. 
int rowStart = y * yRowStride + cropRect.left(); 
int rowEnd = y * yRowStride + cropRect.left() + cropRect.width(); 
関連する問題