title: Opencv Image Overlaying to ROI Java
body: <p>I am trying to overlay an image inside the rectangular region after detection of face and eyes. But I am unable to do so. Can you please guide me how to proceed.
  I have searched a lot on google and tried Mat copy to src file but no result. Please help me in solving this issue.</p>
  
  <pre><code>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) &gt; 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 &lt; 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 &amp;&amp; !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();
  }
  </code></pre>
  
  <p>} </p>