我正在尝试使图像中所有人的脸变灰。虽然我可以检测到他们的脸并将它们变灰成较小的垫子,但是我无法将灰色的脸“复制”到原始垫子上。这样最终结果将具有所有面为灰色的垫。
faceDetector.detectMultiScale(mat, faceDetections);
for (Rect rect : faceDetections.toArray())
{
Rect rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
Mat imageROI = new Mat(mat,rectCrop);
//convert to B&W
Imgproc.cvtColor(imageROI, imageROI, Imgproc.COLOR_RGB2GRAY);
//Uncomment below will grayout the faces (one by one) but my objective is to have them grayed out on the original mat only.
//Highgui.imwrite(JTestUtil.DESKTOP_PATH+"cropImage_"+(++index)+".jpg",imageROI);
//add to mat? doesn't do anything :-(
mat.copyTo(imageROI);
}
最佳答案
imageROI是3或4通道图像。将cvtColor转换为灰色可提供单通道输出,并且可能会破坏imageROI对mat的引用。
使用缓冲区进行灰度转换,并以dst作为imageROI转换回RGBA或RGB。
faceDetector.detectMultiScale(mat, faceDetections);
for (Rect rect : faceDetections.toArray())
{
Rect rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
//Get ROI
Mat imageROI = mat.submat(rectCrop);
//Move this declaration to onCameraViewStarted
Mat bw = new Mat();
//Use Imgproc.COLOR_RGB2GRAY for 3 channel image.
Imgproc.cvtColor(imageROI, bw, Imgproc.COLOR_RGBA2GRAY);
Imgproc.cvtColor(bw, imageROI, Imgproc.COLOR_GRAY2RGBA);
}
结果看起来像