Capture camera preview for use in OpenCV. Convert to RGB and Gray Mat's. Java. Android

I want to detect faces on a camera preview. I saw this example in OpenCV samples:

@Override protected Bitmap processFrame(VideoCapture capture) { capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME); if (mCascade != null) { int height = mGray.rows(); int faceSize = Math.round(height * FdActivity.minFaceSize); List<Rect> faces = new LinkedList<Rect>(); mCascade.detectMultiScale(mGray, faces, 1.1, 2, 2 // TODO: objdetect.CV_HAAR_SCALE_IMAGE , new Size(faceSize, faceSize)); for (Rect r : faces) Core.rectangle(mRgba, r.tl(), r.br(), new Scalar(0, 255, 0, 255), 3); } Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888); if (Utils.matToBitmap(mRgba, bmp)) return bmp; bmp.recycle(); return null; } 

I rewrote this code for my project (byte [] input from onPreviewFrame () from PreviewCallback):

 public Highlighting[] get(byte[] data) { matYuv = new Mat(480, 320, CvType.CV_8UC1); matYuv.put(0, 0, data); Imgproc.cvtColor(matYuv, matRgb, Imgproc.COLOR_YUV420sp2RGB, 4); Highlighting[] hl = null; Imgproc.cvtColor(matRgb, matGray, Imgproc.COLOR_RGB2GRAY, 0); if (cascade != null) { int faceSize = 50; List<Rect> faces = new LinkedList<Rect>(); cascade.detectMultiScale(matGray, faces, 1.1, 2, 2, new Size( faceSize, faceSize)); hl = new Highlighting[faces.size()]; int i = 0; for (Rect r : faces) { hl[i] = new Highlighting((int) r.tl().x, (int) r.tl().y, (int) r.br().x, (int) r.br().y, ""); i++; } Log.i("FACES", String.valueOf(faces.size())); } return hl; } 

But I have a problem, my code does not work like the original one - it does not detect faces. Could there be a problem in converting a byte array?

+4
source share
1 answer

480 + 240 (in height) is obtained from the YUV 420 format.

this format has a Y-plane with 480x320, U, and V planes with each resolution of 0.5 * Y-plane (see YUV formats for more details). Since all 3 plans of one frame are stored in one image, you need to allocate enough space.

+3
source

Source: https://habr.com/ru/post/1401868/


All Articles