繁体   English   中英

OpenCV检测ROI,创建子垫并复​​制到原始垫

[英]OpenCV detecting ROI, creating submat and copy to original mat

我正在尝试使图像中所有人的脸变灰。 虽然我可以检测到他们的脸并将它们变灰成较小的垫子,但是我无法将灰色的脸“复制”到原始垫子上。 这样最终结果将具有所有面为灰色的垫子。

        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);
    }

结果看起来像 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM