我正在尝试在 Android 中使用 OpenCV 在位图中实现一些过滤器,第一步是检测人脸并分成 2 张图像(眼睛)。但是当我尝试实现一个 Mat 函数来做一些图像处理时,它就死了。
错误文本是这样的:
08-05 17:54:58.659: E/AndroidRuntime(11005): java.lang.UnsatisfiedLinkError: n_Mat
代码是这样的,我对 OpenCV 越来越着迷了。
package org.opencv.samples.puzzle15;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.util.Log;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class Puzzle15Activity extends Activity {
private static final String TAG = "Sample::TestFilter::Activity";
private ImageView face;
private ImageView leftEyeImg;
private ImageView rightEyeImg;
private Bitmap fL;
private Bitmap fR;
FaceDetector fD;
FaceDetector.Face[] faceArray;
Bitmap rightEye;
Bitmap leftEye;
int y;
private int mGameWidth;
private int mGameHeight;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
//starEverything();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Log.i(TAG, "Ejecutando onCreate");
LinearLayout layout = new LinearLayout(this);
face = new ImageView(this);
face.setImageResource(R.drawable.asd);
leftEyeImg = new ImageView(this);
rightEyeImg = new ImageView(this);
int max = 5;
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inPreferredConfig = Bitmap.Config.RGB_565;
bfo.inScaled = false;
bfo.inDither = false;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.face1, bfo);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
FaceDetector fd = new FaceDetector(w, h, max);
Face[] faces = new Face[max];
int c = fd.findFaces(bitmap, faces);
Log.d("TAG", "FACES: "+c);
for (int i=0;i<c;i++) {
Log.d("TAG", Float.toString(faces[i].eyesDistance()));
}
PointF fpx;
fpx = new PointF();
faces[0].getMidPoint(fpx);
float fds = faces[0].eyesDistance();
Log.d("TAG", "x: "+fpx.x);
Log.d("TAG", "y: "+fpx.y);
Log.d("TAG", "fsd: "+fds);
Log.d("TAG", "w: "+w);
Log.d("TAG", "h: "+h);
fL =cropBitmap1(bitmap,fpx.x, fpx.y, fds, fds);
fR =cropBitmap1(bitmap, fpx.x-fpx.y/2, fpx.y, fds, fds);
rightEyeImg.setImageBitmap(fR);
leftEyeImg.setImageBitmap(fL);
layout.addView(leftEyeImg);
layout.addView(rightEyeImg);
setContentView(layout);
someFilter(fL);
}
public void someFilter(Bitmap a){
synchronized (this){
Mat tmp = new Mat (a.getWidth(), a.getHeight(), CvType.CV_8UC1);
Utils.bitmapToMat(a, tmp);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_GRAY2RGB, 4);
Utils.matToBitmap(tmp, a);
}
}
private Bitmap cropBitmap1(Bitmap source, Float x, Float y, Float h, Float w)
{
Bitmap cropped = Bitmap.createBitmap(source, Math.round(x),Math.round(y)-Math.round(h)/4, Math.round(h), Math.round(h)/2);
return cropped;
}
}