我想使用Android opencv检测脸部。但是在图像中没有检测到脸部。为了测试我已经在pc中尝试了这个代码,它正在工作。为什么它不适用于Android?我的opencv版本是2.4.9。谢谢。
private void my_opencv() throws IOException
{
// TODO Auto-generated method stub
Utils.bitmapToMat(bitmap, image);
Log.d("MyTag","open_cv2");
//Imgproc.cvtColor(image, grayscaleImage, Imgproc.COLOR_RGBA2RGB);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
File mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml");
// Load the cascade classifier
CascadeClassifier faceDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
/**/
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
Log.d("MyTag","open_cv3");
Rect rectCrop = null;
// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray())
{
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new
Scalar(255, 0, 0));
// for crop face
rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
Core.putText(image, "Edited by me", new Point(rect.x,rect.y),
Core.FONT_HERSHEY_PLAIN, 1.0 ,new Scalar(0,255,255));
}
// Use the classifier to detect faces
// Save the visualized detection.
String format = new SimpleDateFormat("yyyyMMddHHmmss",
java.util.Locale.getDefault()).format(new Date());
Bitmap bmp = null;
try {
bmp = Bitmap.createBitmap(image.cols(), image.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(image, bmp);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + format+".jpg");
Log.d("MyTag","$$$$$$$$$$"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM));
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (CvException e) {
Log.d(TAG, e.getMessage());
}
}
解决了代码:
private void my_opencv() throws IOException
{
// TODO Auto-generated method stub
Utils.bitmapToMat(bitmap, image);
Log.d("MyTag","open_cv2");
//Imgproc.cvtColor(image, grayscaleImage, Imgproc.COLOR_RGBA2RGB);
// load cascade file from application resources
InputStream is = getResources().openRawResource(
R.raw.lbpcascade_frontalface);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
File mCascadeFile = new File(cascadeDir,
"lbpcascade_frontalface.xml");
FileOutputStream os = new FileOutputStream(mCascadeFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1)
{
os.write(buffer, 0, bytesRead);
}
is.close();
os.close();
CascadeClassifier faceDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
//must add this line
//faceDetector.load( mCascadeFile.getAbsolutePath() );
/**/
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
Log.d("MyTag","open_cv3");
Rect rectCrop = null;
Rect[] rect = faceDetections.toArray();
// Draw a bounding box around each face.
// for ( Rect rect : faceDetections.toArray() )
for ( int i = 0; i < rect.length; i++ )
{
Core.rectangle(image, new Point(rect[i].x, rect[i].y), new Point(rect[i].x + rect[i].width, rect[i].y + rect[i].height), new
Scalar(255, 0, 0));
// for crop face
//rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
Core.putText(image, "Edited by me", new Point(rect[i].x,rect[i].y),
Core.FONT_HERSHEY_PLAIN, 1.0 ,new Scalar(0,255,255));
}
// Use the classifier to detect faces
// Save the visualized detection.
String format = new SimpleDateFormat("yyyyMMddHHmmss",
java.util.Locale.getDefault()).format(new Date());
Bitmap bmp = null;
try {
bmp = Bitmap.createBitmap(image.cols(), image.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(image, bmp);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + format+".jpg");
Log.d("MyTag","$$$$$$$$$$"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM));
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
}
catch (CvException e)
{
Log.d(TAG, e.getMessage());
}
}