Opencv图像覆盖到ROI Java

时间:2017-05-13 08:12:10

标签: java opencv javafx

我试图在检测到脸部和眼睛后将图像叠加在矩形区域内。但我无法这样做。你能指导我如何继续。 我在谷歌搜索了很多,并尝试将Mat复制到src文件,但没有结果。请帮我解决这个问题。

public class FXController {

// FXML buttons
@FXML
private Button cameraButton;
// the FXML area for showing the current frame
@FXML
private ImageView originalFrame;
// checkboxes for enabling/disabling a classifier
@FXML
private CheckBox haarClassifier;
@FXML
private CheckBox lbpClassifier;

// a timer for acquiring the video stream
private ScheduledExecutorService timer;
// the OpenCV object that performs the video capture
private VideoCapture capture;
// a flag to change the button behavior
private boolean cameraActive;

// face cascade classifier
private CascadeClassifier faceCascade;
private int absoluteFaceSize;


/**
 * Init the controller, at start time
 */
protected void init()
{
    this.capture = new VideoCapture();
    this.faceCascade = new CascadeClassifier();
    this.absoluteFaceSize = 0;

    // set a fixed width for the frame
    originalFrame.setFitWidth(600);
    // preserve image ratio
    originalFrame.setPreserveRatio(true);
}

/**
 * The action triggered by pushing the button on the GUI
 */
@FXML
protected void startCamera()
{   
    if (!this.cameraActive)
    {
        // disable setting checkboxes
        this.haarClassifier.setDisable(true);
        this.lbpClassifier.setDisable(true);

        // start the video capture
        this.capture.open(0);

        // is the video stream available?
        if (this.capture.isOpened())
        {
            this.cameraActive = true;

            // grab a frame every 33 ms (30 frames/sec)
            Runnable frameGrabber = new Runnable() {

                @Override
                public void run()
                {
                    // effectively grab and process a single frame
                    Mat frame = grabFrame();
                    // convert and show the frame
                    Image imageToShow = Utils.mat2Image(frame);
                    updateImageView(originalFrame, imageToShow);
                }
            };

            this.timer = Executors.newSingleThreadScheduledExecutor();
            this.timer.scheduleAtFixedRate(frameGrabber, 0, 33, TimeUnit.MILLISECONDS);

            // update the button content
            this.cameraButton.setText("Stop Camera");
        }
        else
        {
            // log the error
            System.err.println("Failed to open the camera connection...");
        }
    }
    else
    {
        // the camera is not active at this point
        this.cameraActive = false;
        // update again the button content
        this.cameraButton.setText("Start Camera");
        // enable classifiers checkboxes
        this.haarClassifier.setDisable(false);
        this.lbpClassifier.setDisable(false);

        // stop the timer
        this.stopAcquisition();
    }
}

/**
 * Get a frame from the opened video stream (if any)
 * 
 * @return the {@link Image} to show
 */
private Mat grabFrame()
{
    Mat frame = new Mat();

    // check if the capture is open
    if (this.capture.isOpened())
    {
        try
        {
            // read the current frame
            this.capture.read(frame);

            // if the frame is not empty, process it
            if (!frame.empty())
            {
                // face detection
                this.detectAndDisplay(frame);
            }

        }
        catch (Exception e)
        {
            // log the (full) error
            System.err.println("Exception during the image elaboration: " + e);
        }
    }

    return frame;
}

/**
 * Method for face detection and tracking
 * 
 * @param frame
 *            it looks for faces in this frame
 */
private void detectAndDisplay(Mat frame)
{
    MatOfRect faces = new MatOfRect();
    //Mat grayFrameSrc = new Mat();
    Mat grayFrameDest=Imgcodecs.imread("images/face.png");

    // convert the frame in gray scale
    //Imgproc.cvtColor(frame, grayFrameSrc, Imgproc.COLOR_BGR2GRAY);
    Imgproc.cvtColor(frame, grayFrameDest, Imgproc.COLOR_BGR2GRAY);
    // equalize the frame histogram to improve the result
    //Imgproc.equalizeHist(grayFrameSrc, grayFrameSrc);
    Imgproc.equalizeHist(grayFrameDest, grayFrameDest);
    //int height = grayFrameSrc.rows();
    //int width = grayFrameSrc.width();
    int height = grayFrameDest.rows();
    // compute minimum face size (20% of the frame height, in our case)
    if (this.absoluteFaceSize == 0)
    {

        //System.out.println("The height = "+width);
        if (Math.round(height * 0.1f) > 0)
        {
            this.absoluteFaceSize = Math.round(height * 0.1f);
        }
    }



    // detect faces
    this.faceCascade.detectMultiScale(grayFrameDest, faces, 1.1, 2, 0 | Objdetect.CASCADE_SCALE_IMAGE,
            new Size(this.absoluteFaceSize, this.absoluteFaceSize), new Size());


    // each rectangle in faces is a face: draw them!
    Rect[] facesArray = faces.toArray();

    for (int i = 0; i < facesArray.length; i++){
        int x=facesArray[i].x;
        int y=facesArray[i].y;
        int h=facesArray[i].height;
        int w=facesArray[i].width;
        Rect rect=facesArray[i];
        Imgproc.rectangle(frame, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0), 3);
        //Imgproc.putText(frame, "Hi Ankit", new Point(x, y), 0, 0, new Scalar(0, 255, 0));
        Imgcodecs.imwrite("/home/ankit-mathur/Desktop/mask.png", frame);
        Mat temp = new Mat();
        Imgproc.cvtColor(grayFrameDest, temp, Imgproc.COLOR_BGRA2GRAY,0);
        Mat temp_rgba = new Mat();
        Imgproc.cvtColor(temp, temp_rgba, Imgproc.COLOR_GRAY2BGRA,0);
        temp_rgba.copyTo(grayFrameDest);



    }




}








@FXML
protected void haarSelected(Event event)
{
    // check whether the lpb checkbox is selected and deselect it
    if (this.lbpClassifier.isSelected())
        this.lbpClassifier.setSelected(false);

    this.checkboxSelection("resources/haarcascades/haarcascade_frontalface_alt.xml");
}

/**
 * The action triggered by selecting the LBP Classifier checkbox. It loads
 * the trained set to be used for frontal face detection.
 */
@FXML
protected void lbpSelected(Event event)
{
    // check whether the haar checkbox is selected and deselect it
    if (this.haarClassifier.isSelected())
        this.haarClassifier.setSelected(false);

    this.checkboxSelection("resources/lbpcascades/lbpcascade_frontalface.xml");
}

/**
 * Method for loading a classifier trained set from disk
 * 
 * @param classifierPath
 *            the path on disk where a classifier trained set is located
 */
private void checkboxSelection(String classifierPath)
{
    // load the classifier(s)

    //System.out.println(classifierPath);



    if (this.faceCascade.load(classifierPath)) {
        this.faceCascade.load(classifierPath);
    }
    else{
        System.out.println("Unable To Load FaceCascade");
    }

    // now the video capture can start
    this.cameraButton.setDisable(false);
}

/**
 * Stop the acquisition from the camera and release all the resources
 */
private void stopAcquisition()
{
    if (this.timer!=null && !this.timer.isShutdown())
    {
        try
        {
            // stop the timer
            this.timer.shutdown();
            this.timer.awaitTermination(33, TimeUnit.MILLISECONDS);
        }
        catch (InterruptedException e)
        {
            // log any exception
            System.err.println("Exception in stopping the frame capture, trying to release the camera now... " + e);
        }
    }

    if (this.capture.isOpened())
    {
        // release the camera
        this.capture.release();
    }
}

/**
 * Update the {@link ImageView} in the JavaFX main thread
 * 
 * @param view
 *            the {@link ImageView} to update
 * @param image
 *            the {@link Image} to show
 */
private void updateImageView(ImageView view, Image image)
{
    Utils.onFXThread(view.imageProperty(), image);
}

/**
 * On application close, stop the acquisition from the camera
 */
protected void setClosed()
{
    this.stopAcquisition();
}

}

1 个答案:

答案 0 :(得分:0)

我正在iOS上做同样的事情。我就是这样做的

Subscribe inner Observable: 0
2803 '0:0'
3208 '0:1'
3615 '0:2'
4016 '0:3'
Subscribe inner Observable: 1
4853 '1:0'
5254 '1:1'
5658 '1:2'
6061 '1:3'
Subscribe inner Observable: 2
7814 '2:0'
8218 '2:1'
8622 '2:2'
9026 '2:3'
Subscribe inner Observable: 3
9180 '3:0'
9583 '3:1'
9987 '3:2'
10391 '3:3'
Subscribe inner Observable: 5
10393 '5:0'
10796 '5:1'