如何使用 OpenCV 在 Android 中保存检测到的人脸?
How to save detected face in Android using OpenCV?
我正在使用 OpenCV 的示例代码通过 android 设备检测人脸。我只想将检测到的面部区域保存到 SD 卡。我正在尝试将垫子转换为位图并保存。但我的问题是它保存了整个图像而不仅仅是面部。这是我将垫子转换为位图的方法
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 的初学者。请帮忙。提前谢谢你
您的代码看起来不错。我认为问题出在您的矩阵 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);
假设只有一张脸。我们可以裁剪面部检测的结果并按照此 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()
我正在使用 OpenCV 的示例代码通过 android 设备检测人脸。我只想将检测到的面部区域保存到 SD 卡。我正在尝试将垫子转换为位图并保存。但我的问题是它保存了整个图像而不仅仅是面部。这是我将垫子转换为位图的方法
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 的初学者。请帮忙。提前谢谢你
您的代码看起来不错。我认为问题出在您的矩阵 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);
假设只有一张脸。我们可以裁剪面部检测的结果并按照此 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()