如何使用OpenCV在Android中保存检测到的人脸?
我正在使用OpenCV的示例代码检测android设备的人脸。我只想将检测到的面部区域保存到sd卡。我正在尝试将mat转换为位图并保存它。但我的问题是它保存了整个图像,而不仅仅是人脸。下面是我将mat转换为位图的方法如何使用OpenCV在Android中保存检测到的人脸?,android,opencv,face-detection,Android,Opencv,Face Detection,我正在使用OpenCV的示例代码检测android设备的人脸。我只想将检测到的面部区域保存到sd卡。我正在尝试将mat转换为位图并保存它。但我的问题是它保存了整个图像,而不仅仅是人脸。下面是我将mat转换为位图的方法 Bitmap bitmap = Bitmap.createBitmap(mGray.cols(), mGray.rows(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(mGray, bitmap);
Bitmap bitmap = Bitmap.createBitmap(mGray.cols(), mGray.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mGray, bitmap);
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
我是Opencv的初学者。请帮忙。提前感谢您您的代码看起来不错。我想问题出在你的matrix mGray上。看起来mGray包含了整个图像像素,您正在使用它创建位图。因此,我的建议是首先检查您的mGray矩阵,并获取面部区域,然后将像素复制到另一个矩阵,然后使用仅包含面部的新矩阵创建位图。
希望有帮助。您的代码看起来不错。我想问题出在你的matrix mGray上。看起来mGray包含了整个图像像素,您正在使用它创建位图。因此,我的建议是首先检查您的mGray矩阵,并获取面部区域,然后将像素复制到另一个矩阵,然后使用仅包含面部的新矩阵创建位图。
希望能有所帮助。问题是,你从来没有尝试过获取面部像素。在您检测到人脸后,我建议您采取以下措施:
Mat mFaceMatrix = mRgba.submat(facesArray.y, facesArray.y + facesArray.heigth, facesArray.x, facesArray.x + facesArray.width);
现在将这个矩阵传递给createBitmap函数就可以了
Bitmap bitmap = Bitmap.createBitmap(mFaceMatrix.cols(), mFaceMatrix.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mFaceMatrix, bitmap);
问题是,您从未尝试获取人脸像素。在您检测到人脸后,我建议您采取以下措施:
Mat mFaceMatrix = mRgba.submat(facesArray.y, facesArray.y + facesArray.heigth, facesArray.x, facesArray.x + facesArray.width);
现在将这个矩阵传递给createBitmap函数就可以了
Bitmap bitmap = Bitmap.createBitmap(mFaceMatrix.cols(), mFaceMatrix.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mFaceMatrix, bitmap);
假设只有一张脸。我们可以裁剪人脸检测的结果,并按照以下python脚本中的描述保存它:
import cv2
import sys
cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.waitKey(1) & 0xFF == ord('c'):
crop = frame[y: y + h, x: x + w]
cv2.imwrite("face.jpg", crop)
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
假设只有一张脸。我们可以裁剪人脸检测的结果,并按照以下python脚本中的描述保存它:
import cv2
import sys
cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.waitKey(1) & 0xFF == ord('c'):
crop = frame[y: y + h, x: x + w]
cv2.imwrite("face.jpg", crop)
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
非常感谢。我稍微修改了代码。我把mat从mGray换成了mRgba,现在我得到了脸周围的矩形,但我仍然得到了整个图像。让我展示另一种绘制矩形的方法。我通过回答que发布了我的方法。我不能用其他方法。谢谢。我稍微修改了代码。我把mat从mGray换成了mRgba,现在我得到了脸周围的矩形,但我仍然得到了整个图像。让我展示另一种绘制矩形的方法。我通过回答que发布了我的方法。我没有别的办法。