Nel mio approccio che uso OpenCV Mat e script da https://gist.github.com/camdenfullmer/dfd83dfb0973663a7974
Prima di tutto si converte il YUV_420_888 Immagine a Mat con il codice nel link sopra.
* mImage è mio oggetto Immagine che ottengo in ImageReader.OnImageAvailableListener
Mat mYuvMat = imageToMat(mImage);
public static Mat imageToMat(Image image) {
ByteBuffer buffer;
int rowStride;
int pixelStride;
int width = image.getWidth();
int height = image.getHeight();
int offset = 0;
Image.Plane[] planes = image.getPlanes();
byte[] data = new byte[image.getWidth() * image.getHeight() * ImageFormat.getBitsPerPixel(ImageFormat.YUV_420_888)/8];
byte[] rowData = new byte[planes[0].getRowStride()];
for (int i = 0; i < planes.length; i++) {
buffer = planes[i].getBuffer();
rowStride = planes[i].getRowStride();
pixelStride = planes[i].getPixelStride();
int w = (i == 0) ? width : width/2;
int h = (i == 0) ? height : height/2;
for (int row = 0; row < h; row++) {
int bytesPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.YUV_420_888)/8;
if (pixelStride == bytesPerPixel) {
int length = w * bytesPerPixel;
buffer.get(data, offset, length);
if (h - row != 1) {
buffer.position(buffer.position() + rowStride - length);
}
offset += length;
} else {
if (h - row == 1) {
buffer.get(rowData, 0, width - pixelStride + 1);
} else {
buffer.get(rowData, 0, rowStride);
}
for (int col = 0; col < w; col++) {
data[offset++] = rowData[col * pixelStride];
}
}
}
}
Mat mat = new Mat(height + height/2, width, CvType.CV_8UC1);
mat.put(0, 0, data);
return mat;
}
Abbiamo 1 canale YUV Mat. Definire nuova Mat per BGR (non RGB ancora) immagine:
Mat bgrMat = new Mat(mImage.getHeight(), mImage.getWidth(),CvType.CV_8UC4);
Ho appena iniziato a studiare in modo OpenCV propably questo non deve essere Mat 4 canali e invece potrebbe essere a 3 canali, ma funziona per me. Ora uso il metodo convert color per cambiare il mio yuv Mat in bgr Mat.
Imgproc.cvtColor(mYuvMat, bgrMat, Imgproc.COLOR_YUV2BGR_I420);
Ora possiamo fare tutto il trattamento delle immagini come trovare contorni, dei colori, cerchi, ecc Per stampare un'immagine di nuovo sullo schermo abbiamo bisogno di convertirlo in bitmap:
Mat rgbaMatOut = new Mat();
Imgproc.cvtColor(bgrMat, rgbaMatOut, Imgproc.COLOR_BGR2RGBA, 0);
final Bitmap bitmap = Bitmap.createBitmap(bgrMat.cols(), bgrMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgbaMatOut, bitmap);
ho tutti i miei l'elaborazione delle immagini in thread separati in modo tale da impostare il mio ImageView, ho bisogno di farlo sul thread dell'interfaccia utente.
runOnUiThread(new Runnable() {
@Override
public void run() {
if(bitmap != null) {
mImageView.setImageBitmap(bitmap);
}
}
});
possibile duplicato del [Conversione YUV-> RGB (elaborazione di immagini) -> YUV durante onPreviewFrame in Android ?] (http://stackoverflow.com/questions/9325861/converting-yuv-rgbimage-processing-yuv-during-onpreviewframe-in-android) – JAL
Questo vale per l'implementazione della fotocamera precedente, che non mi aiuta. Grazie comunque. –
Qualcuno ha convertito YUV_420_888 in NV21 (YUV_420_SP)? –