在得到使用圆形裁剪的建议后,我在我的应用程序中实现了它,用于从位图中提取人脸,但它的效率太低,我的意思是它没有用,类似于从矩形人脸裁剪。现在这是我到目前为止所做的:
- 推出相机
- 拍照
- 检测到图片中的人脸
- 已检测到在区域人脸中绘制矩形窗口
现在面临的严重问题是从该位图中获得面子。我只是想打脸。忽略面部检测窗口中的所有其他细节。如果是 matlab,在边缘检测技术和分割算法和函数的帮助下,它可能会变得过于简单。我想知道如何在 android 应用程序中执行相同的过程?如果你知道的话,请分享代码片段。这占用了我太多时间,我只想完成这个面部提取部分。
我到现在为止的代码:
public class Makeover extends Activity {
private static final int TAKE_PICTURE_CODE = 100;
private static final int MAX_FACES = 5;
private Bitmap cameraBitmap = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button)findViewById(R.id.take_picture)).setOnClickListener(btnClick);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(TAKE_PICTURE_CODE == requestCode){
processCameraImage(data);
}
}
private void openCamera(){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_CODE);
}
private void processCameraImage(Intent intent){
setContentView(R.layout.detectlayout);
((Button)findViewById(R.id.detect_face)).setOnClickListener(btnClick);
ImageView imageView = (ImageView)findViewById(R.id.image_view);
cameraBitmap = (Bitmap)intent.getExtras().get("data");
imageView.setImageBitmap(cameraBitmap);
}
private void detectFaces(){
Bitmap bmFace = null;
if(null != cameraBitmap){
int width = cameraBitmap.getWidth();
int height = cameraBitmap.getHeight();
FaceDetector detector = new FaceDetector(width, height,Makeover.MAX_FACES);
Face[] faces = new Face[Makeover.MAX_FACES];
Bitmap bitmap565 = Bitmap.createBitmap(width, height, Config.RGB_565);
Paint ditherPaint = new Paint();
Paint drawPaint = new Paint();
//jo bhi krna hai ab bitmap565 k sath krna hai apse wo image hai main
ditherPaint.setDither(true);
drawPaint.setColor(Color.RED);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeWidth(2);
Canvas canvas = new Canvas();
canvas.setBitmap(bitmap565);
canvas.drawBitmap(cameraBitmap, 0, 0, ditherPaint);
int facesFound = detector.findFaces(bitmap565, faces);
PointF midPoint = new PointF();
float eyeDistance = 0.0f;
float confidence = 0.0f;
Log.i("FaceDetector", "Number of faces found: " + facesFound);
if(facesFound > 0)
{
for(int index=0; index<facesFound; ++index){
faces[index].getMidPoint(midPoint);
eyeDistance = faces[index].eyesDistance();
confidence = faces[index].confidence();
Log.i("FaceDetector",
"Confidence: " + confidence +
", Eye distance: " + eyeDistance +
", Mid Point: (" + midPoint.x + ", " + midPoint.y + ")");
canvas.drawRect((int)midPoint.x - eyeDistance ,
(int)midPoint.y - eyeDistance ,
(int)midPoint.x + eyeDistance,
(int)midPoint.y + eyeDistance, drawPaint);
}
}
for(int count=0;count<detector.findFaces(bitmap565, faces);count++)
{
float left;
float top;
PointF midPoints=new PointF();
faces[count].getMidPoint(midPoint);
eyeDistance=faces[count].eyesDistance();
left = midPoint.x - (float)(1.4 * eyeDistance);
top = midPoint.y - (float)(1.8 * eyeDistance);
bmFace = Bitmap.createBitmap(cameraBitmap, (int) left, (int) top, (int) (2.8 * eyeDistance), (int) (3.6 * eyeDistance));
}
String filepath = Environment.getExternalStorageDirectory() + "/facedetect" + System.currentTimeMillis() + ".jpg";
try {
FileOutputStream fos = new FileOutputStream(filepath);
bitmap565.compress(CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ImageView imageView = (ImageView)findViewById(R.id.image_view);
imageView.setImageBitmap(bmFace);
}
}
private View.OnClickListener btnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.take_picture: openCamera(); break;
case R.id.detect_face: detectFaces(); break;
}
}
};
}