Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

i have this Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor

i'm trying to face detect and motion detect for my internship... here is my code....

public class FaceDetect {

private Button cameraButton, cropButton, pictureButton, close, compare;
private ImageView originalFrame;

private CheckBox haarClassifier;
private CheckBox lbpClassifier;
static Mat imag = null;
private ScheduledExecutorService timer;
private VideoCapture capture;
private boolean cameraActive;
private CascadeClassifier faceCascade;
private int absoluteFaceSize;   
MatOfRect faces ;
Mat grayFrame = new Mat(),frame;
private Rect[] facesArray;

protected void init()
{
    this.capture = new VideoCapture();
    this.faceCascade = new CascadeClassifier();
    this.absoluteFaceSize = 0;
}

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

    if (!this.cameraActive)
    {
        // disable setting checkboxes
        this.haarClassifier.setDisable(true);
        this.lbpClassifier.setDisable(true);
        this.pictureButton.setDisable(false);
        this.cropButton.setDisable(false);
        this.compare.setDisable(false);

        // 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()
                {
                    Image imageToShow = grabFrame();
                    originalFrame.setImage(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);
        this.pictureButton.setDisable(true);
        this.cropButton.setDisable(true);
        this.compare.setDisable(true);
        // stop the timer
        try
        {
            this.timer.shutdown();
            this.timer.awaitTermination(33, TimeUnit.MILLISECONDS);
        }
        catch (InterruptedException e)
        {
            // log the exception
            System.err.println("Exception in stopping the frame capture, trying to release the camera now... " + e);
        }

        // release the camera
        this.capture.release();
        // clean the frame
        this.originalFrame.setImage(null);
    }
}

private Image grabFrame(){
    // init everything
    Image imageToShow = null;
    Mat frame = new Mat();

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

            // if the frame is not empty, process it
            if (!frame.empty()){
                // face detection
                frame= new Mat(frame.size(), CvType.CV_8UC1);
                this.detectAndDisplay(frame);
                    /*//detection of motion
                    imag=frame;
                    ArrayList<Rect> array = new ArrayList<Rect>();
                    Mat outerBox = new Mat(frame.size(), CvType.CV_8UC1);
                    Imgproc.cvtColor(frame, outerBox, Imgproc.COLOR_BGR2GRAY);
                    Imgproc.GaussianBlur(outerBox, outerBox, new Size(3, 3), 0);
                    Mat diff_frame = new Mat(outerBox.size(), CvType.CV_8UC1);
                    Mat tempon_frame = new Mat(outerBox.size(), CvType.CV_8UC1);
                    Core.subtract(outerBox, tempon_frame, diff_frame);
                    Imgproc.adaptiveThreshold(diff_frame, diff_frame, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 5, 2);
                    array = detection_contours(diff_frame);
                    Iterator<Rect> it2 = array.iterator();
                    Rect obj = it2.next();
                    //while (it2.hasNext())
                    Imgproc.rectangle(imag, obj.br(), obj.tl(), new Scalar(0, 255, 0), 1);*/
                ArrayList<Rect> array = new ArrayList<Rect>();
                array = detection_contours(motion(frame));
                if (array.size() > 0) {

                    Iterator<Rect> it2 = array.iterator();
                    while (it2.hasNext()) {
                        Rect obj = it2.next();
                        Imgproc.rectangle(imag, obj.br(), obj.tl(),new Scalar(0, 255, 0), 1);
                    }
                }
                // convert the Mat object (OpenCV) to Image (JavaFX)
                imageToShow = mat2Image(imag);
            }
        }
        catch (Exception e){
            // log the (full) error
            System.err.println("ERROR: " + e);
        }
    }
    return imageToShow;
}   

private void detectAndDisplay(Mat fr ){
    faces = new MatOfRect();
    //Mat grayFrame = new Mat();
    frame =fr;
    // convert the frame in gray scale
    Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY);
    // equalize the frame histogram to improve the result
    Imgproc.equalizeHist(grayFrame, grayFrame);

    // compute minimum face size (20% of the frame height, in our case)
    if (this.absoluteFaceSize == 0){
        int height = grayFrame.rows();
        if (Math.round(height * 0.2f) > 0)
        {
            this.absoluteFaceSize = Math.round(height * 0.2f);
        }
    }

    // detect faces
    this.faceCascade.detectMultiScale(grayFrame, 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!
    facesArray = faces.toArray();
    for (int i = 0; i < facesArray.length; i++)
        Imgproc.rectangle(frame, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 255), 3);
        /*while (it2.hasNext())*/

}
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("C:/Users/Amatalamessinaj/workspace/recognition/src/application/haarcascades/haarcascade_frontalface_alt_tree.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("C:/Users/Amatalamessinaj/workspace/recognition/src/application/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)
    this.faceCascade.load(classifierPath);

    // now the video capture can start
    this.cameraButton.setDisable(false);
}
/**
 * Convert a Mat object (OpenCV) in the corresponding Image for JavaFX
 * 
 * @param frame
 *            the {@link Mat} representing the current frame
 * @return the {@link Image} to show
 */
private Image mat2Image(Mat frame)
{
    // create a temporary buffer
    MatOfByte buffer = new MatOfByte();
    // encode the frame in the buffer, according to the PNG format
    Imgcodecs.imencode(".png", frame, buffer);
    // build and return an Image created from the image encoded in the
    // buffer
    return new Image(new ByteArrayInputStream(buffer.toArray()));
}    
/**
 * event for taking a picture
 * **/
@FXML
protected void takeAPic(){ 
    BufferedImage image;
    image = SwingFXUtils.fromFXImage(grabFrame(), null);
    saveImage(image,2);
}   
//Save an image
public static boolean saveImage(BufferedImage img, int v) {        
    try {
        int val= v;
        File outputfile = new File("C://Users/Amatalamessinaj/Pictures/"+val+"x.png");
        ImageIO.write(img, "jpg", outputfile);

    } catch (Exception e) {
        System.out.println("error");
    }

    return true;
}
public static boolean saveImage(BufferedImage img, Character c) {        
    try {
        Character val=c;
        File outputfile = new File("C://Users/Amatalamessinaj/Pictures/"+val+"compare.png");
        ImageIO.write(img, "jpg", outputfile);

    } catch (Exception e) {
        System.out.println("error");
    }

    return true;
}
@FXML
public void cropFace(){

    for (int i = 0; i < facesArray.length; i++){
     Rect rectCrop = new Rect(facesArray[i].tl(), facesArray[i].br());
     Mat imCrop=  new Mat(frame,rectCrop);

     Image img=mat2Image(imCrop);

     BufferedImage image;
     image = SwingFXUtils.fromFXImage(img, null);

     saveImage(image,3);
    }
}
@FXML
public void close(){
    if (this.capture.isOpened()){this.capture.release();}
    System.exit(0);
}
@FXML
public void compare(){
    BufferedImage img1=null,image=null,img2=null;

    image = SwingFXUtils.fromFXImage(grabFrame(), null);
    saveImage(image,10);
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e2) {
        e2.printStackTrace();
    }
    image = SwingFXUtils.fromFXImage(grabFrame(), null);
    saveImage(image,11);

    try {
        img1 = ImageIO.read(new File("C://Users/Amatalamessinaj/Pictures/10x.png"));
        img2 = ImageIO.read(new File("C://Users/Amatalamessinaj/Pictures/11x.png"));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 

    int width1 = img1.getWidth(null);
    int width2 = img2.getWidth(null);
    int height1 = img1.getHeight(null);
    int height2 = img2.getHeight(null);
    if ((width1 != width2) || (height1 != height2)) {
      System.err.println("Error: Images dimensions mismatch");
      System.exit(1);
    }
    long diff = 0;
    for (int y = 0; y < height1; y++) {
      for (int x = 0; x < width1; x++) {
        int rgb1 = img1.getRGB(x, y);
        int rgb2 = img2.getRGB(x, y);
        int r1 = (rgb1 >> 16) & 0xff;
        int g1 = (rgb1 >>  8) & 0xff;
        int b1 = (rgb1      ) & 0xff;
        int r2 = (rgb2 >> 16) & 0xff;
        int g2 = (rgb2 >>  8) & 0xff;
        int b2 = (rgb2      ) & 0xff;
        diff += Math.abs(r1 - r2);
        diff += Math.abs(g1 - g2);
        diff += Math.abs(b1 - b2);
      }
    }
    double n = width1 * height1 * 3;
    double p = diff / n / 255.0;
    System.out.println("diff percent: " + (p * 100.0));
    String x ="diff percent: " + (p * 100.0);
    JFrame fenetre = new JFrame();       
    fenetre.setTitle("comparaison");
    fenetre.setSize(400, 100);
    fenetre.setLocationRelativeTo(null);
    JPanel pan1 = new JPanel();
    pan1.setLayout(new BorderLayout());
    JLabel l = new JLabel(x);
    pan1.add(l);
    fenetre.add(pan1, BorderLayout.EAST);
    fenetre.setVisible(true);
  }    
public static ArrayList<Rect> detection_contours(Mat outmat) {
    Mat v = new Mat();
    Mat vv = outmat.clone();
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(vv, contours, v, Imgproc.RETR_LIST,
            Imgproc.CHAIN_APPROX_SIMPLE);

    double maxArea = 100;
    int maxAreaIdx = -1;
    Rect r = null;
    ArrayList<Rect> rect_array = new ArrayList<Rect>();

    for (int idx = 0; idx < contours.size(); idx++) { Mat contour = contours.get(idx); double contourarea = Imgproc.contourArea(contour); if (contourarea > maxArea) {
            // maxArea = contourarea;
            maxAreaIdx = idx;
            r = Imgproc.boundingRect(contours.get(maxAreaIdx));
            rect_array.add(r);
            //Imgproc.drawContours(imag, contours, maxAreaIdx, new Scalar(0,0, 255));
        }
    }
    v.release();
    return rect_array;
}
public Mat motion(Mat f){
    Mat frame = f;
    Mat outerBox = new Mat();
    Mat diff_frame = null;
    Mat tempon_frame = null;
    //VideoCapture camera = new VideoCapture("C:/Users/SAAD/Desktop/ratiler/projects/motion.mp4");
    Size sz = new Size(640, 480);
    int i = 0;

    while (true) {
        //camera.open(0);
        //if (camera.read(frame)) {
            Imgproc.resize(frame, frame, sz);
            imag = frame.clone();
           outerBox = new Mat(frame.size(), CvType.CV_8UC1);
           Imgproc.cvtColor(frame, outerBox, Imgproc.COLOR_BGR2GRAY);
           Imgproc.GaussianBlur(outerBox, outerBox, new Size(3, 3), 0);

            if (i == 0) {
                diff_frame = new Mat(frame.size(), CvType.CV_8UC1);
                tempon_frame = new Mat(outerBox.size(), CvType.CV_8UC1);
            }

            if (i == 1) {
                Core.subtract(outerBox, tempon_frame, diff_frame);
                Imgproc.adaptiveThreshold(diff_frame, diff_frame, 255,
                        Imgproc.ADAPTIVE_THRESH_MEAN_C,
                        Imgproc.THRESH_BINARY_INV, 5, 2);

            }
            i = 1;
            return imag;
}}

}

i have this Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor

i'm trying to face detect and motion detect for my internship... here is my code....

public class FaceDetect {

private Button cameraButton, cropButton, pictureButton, close, compare;
private ImageView originalFrame;

private CheckBox haarClassifier;
private CheckBox lbpClassifier;
static Mat imag = null;
private ScheduledExecutorService timer;
private VideoCapture capture;
private boolean cameraActive;
private CascadeClassifier faceCascade;
private int absoluteFaceSize;   
MatOfRect faces ;
Mat grayFrame = new Mat(),frame;
private Rect[] facesArray;

protected void init()
{
    this.capture = new VideoCapture();
    this.faceCascade = new CascadeClassifier();
    this.absoluteFaceSize = 0;
}

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

    if (!this.cameraActive)
    {
        // disable setting checkboxes
        this.haarClassifier.setDisable(true);
        this.lbpClassifier.setDisable(true);
        this.pictureButton.setDisable(false);
        this.cropButton.setDisable(false);
        this.compare.setDisable(false);

        // 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()
                {
                    Image imageToShow = grabFrame();
                    originalFrame.setImage(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);
        this.pictureButton.setDisable(true);
        this.cropButton.setDisable(true);
        this.compare.setDisable(true);
        // stop the timer
        try
        {
            this.timer.shutdown();
            this.timer.awaitTermination(33, TimeUnit.MILLISECONDS);
        }
        catch (InterruptedException e)
        {
            // log the exception
            System.err.println("Exception in stopping the frame capture, trying to release the camera now... " + e);
        }

        // release the camera
        this.capture.release();
        // clean the frame
        this.originalFrame.setImage(null);
    }
}

private Image grabFrame(){
    // init everything
    Image imageToShow = null;
    Mat frame = new Mat();

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

            // if the frame is not empty, process it
            if (!frame.empty()){
                // face detection
                frame= new Mat(frame.size(), CvType.CV_8UC1);
                this.detectAndDisplay(frame);
                    /*//detection of motion
                    imag=frame;
                    ArrayList<Rect> array = new ArrayList<Rect>();
                    Mat outerBox = new Mat(frame.size(), CvType.CV_8UC1);
                    Imgproc.cvtColor(frame, outerBox, Imgproc.COLOR_BGR2GRAY);
                    Imgproc.GaussianBlur(outerBox, outerBox, new Size(3, 3), 0);
                    Mat diff_frame = new Mat(outerBox.size(), CvType.CV_8UC1);
                    Mat tempon_frame = new Mat(outerBox.size(), CvType.CV_8UC1);
                    Core.subtract(outerBox, tempon_frame, diff_frame);
                    Imgproc.adaptiveThreshold(diff_frame, diff_frame, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 5, 2);
                    array = detection_contours(diff_frame);
                    Iterator<Rect> it2 = array.iterator();
                    Rect obj = it2.next();
                    //while (it2.hasNext())
                    Imgproc.rectangle(imag, obj.br(), obj.tl(), new Scalar(0, 255, 0), 1);*/
                ArrayList<Rect> array = new ArrayList<Rect>();
                array = detection_contours(motion(frame));
                if (array.size() > 0) {

                    Iterator<Rect> it2 = array.iterator();
                    while (it2.hasNext()) {
                        Rect obj = it2.next();
                        Imgproc.rectangle(imag, obj.br(), obj.tl(),new Scalar(0, 255, 0), 1);
                    }
                }
                // convert the Mat object (OpenCV) to Image (JavaFX)
                imageToShow = mat2Image(imag);
            }
        }
        catch (Exception e){
            // log the (full) error
            System.err.println("ERROR: " + e);
        }
    }
    return imageToShow;
}   

private void detectAndDisplay(Mat fr ){
    faces = new MatOfRect();
    //Mat grayFrame = new Mat();
    frame =fr;
    // convert the frame in gray scale
    Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY);
    // equalize the frame histogram to improve the result
    Imgproc.equalizeHist(grayFrame, grayFrame);

    // compute minimum face size (20% of the frame height, in our case)
    if (this.absoluteFaceSize == 0){
        int height = grayFrame.rows();
        if (Math.round(height * 0.2f) > 0)
        {
            this.absoluteFaceSize = Math.round(height * 0.2f);
        }
    }

    // detect faces
    this.faceCascade.detectMultiScale(grayFrame, 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!
    facesArray = faces.toArray();
    for (int i = 0; i < facesArray.length; i++)
        Imgproc.rectangle(frame, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 255), 3);
        /*while (it2.hasNext())*/

}
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("C:/Users/Amatalamessinaj/workspace/recognition/src/application/haarcascades/haarcascade_frontalface_alt_tree.xml");
this.checkboxSelection("C://workspace/recognition/src/application/haarcascades/haarcascade_frontalface_alt_tree.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("C:/Users/Amatalamessinaj/workspace/recognition/src/application/lbpcascades/lbpcascade_frontalface.xml");
this.checkboxSelection("c://workspace/recognition/src/application/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)
    this.faceCascade.load(classifierPath);

    // now the video capture can start
    this.cameraButton.setDisable(false);
}
/**
 * Convert a Mat object (OpenCV) in the corresponding Image for JavaFX
 * 
 * @param frame
 *            the {@link Mat} representing the current frame
 * @return the {@link Image} to show
 */
private Image mat2Image(Mat frame)
{
    // create a temporary buffer
    MatOfByte buffer = new MatOfByte();
    // encode the frame in the buffer, according to the PNG format
    Imgcodecs.imencode(".png", frame, buffer);
    // build and return an Image created from the image encoded in the
    // buffer
    return new Image(new ByteArrayInputStream(buffer.toArray()));
}    
/**
 * event for taking a picture
 * **/
@FXML
protected void takeAPic(){ 
    BufferedImage image;
    image = SwingFXUtils.fromFXImage(grabFrame(), null);
    saveImage(image,2);
}   
//Save an image
public static boolean saveImage(BufferedImage img, int v) {        
    try {
        int val= v;
        File outputfile = new File("C://Users/Amatalamessinaj/Pictures/"+val+"x.png");
File("C://Pictures/"+val+"x.png");
        ImageIO.write(img, "jpg", outputfile);

    } catch (Exception e) {
        System.out.println("error");
    }

    return true;
}
public static boolean saveImage(BufferedImage img, Character c) {        
    try {
        Character val=c;
        File outputfile = new File("C://Users/Amatalamessinaj/Pictures/"+val+"compare.png");
File("C://Pictures/"+val+"compare.png");
        ImageIO.write(img, "jpg", outputfile);

    } catch (Exception e) {
        System.out.println("error");
    }

    return true;
}
@FXML
public void cropFace(){

    for (int i = 0; i < facesArray.length; i++){
     Rect rectCrop = new Rect(facesArray[i].tl(), facesArray[i].br());
     Mat imCrop=  new Mat(frame,rectCrop);

     Image img=mat2Image(imCrop);

     BufferedImage image;
     image = SwingFXUtils.fromFXImage(img, null);

     saveImage(image,3);
    }
}
@FXML
public void close(){
    if (this.capture.isOpened()){this.capture.release();}
    System.exit(0);
}
@FXML
public void compare(){
    BufferedImage img1=null,image=null,img2=null;

    image = SwingFXUtils.fromFXImage(grabFrame(), null);
    saveImage(image,10);
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e2) {
        e2.printStackTrace();
    }
    image = SwingFXUtils.fromFXImage(grabFrame(), null);
    saveImage(image,11);

    try {
        img1 = ImageIO.read(new File("C://Users/Amatalamessinaj/Pictures/10x.png"));
File("C://Pictures/10x.png"));
        img2 = ImageIO.read(new File("C://Users/Amatalamessinaj/Pictures/11x.png"));
File("C://Pictures/11x.png"));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 

    int width1 = img1.getWidth(null);
    int width2 = img2.getWidth(null);
    int height1 = img1.getHeight(null);
    int height2 = img2.getHeight(null);
    if ((width1 != width2) || (height1 != height2)) {
      System.err.println("Error: Images dimensions mismatch");
      System.exit(1);
    }
    long diff = 0;
    for (int y = 0; y < height1; y++) {
      for (int x = 0; x < width1; x++) {
        int rgb1 = img1.getRGB(x, y);
        int rgb2 = img2.getRGB(x, y);
        int r1 = (rgb1 >> 16) & 0xff;
        int g1 = (rgb1 >>  8) & 0xff;
        int b1 = (rgb1      ) & 0xff;
        int r2 = (rgb2 >> 16) & 0xff;
        int g2 = (rgb2 >>  8) & 0xff;
        int b2 = (rgb2      ) & 0xff;
        diff += Math.abs(r1 - r2);
        diff += Math.abs(g1 - g2);
        diff += Math.abs(b1 - b2);
      }
    }
    double n = width1 * height1 * 3;
    double p = diff / n / 255.0;
    System.out.println("diff percent: " + (p * 100.0));
    String x ="diff percent: " + (p * 100.0);
    JFrame fenetre = new JFrame();       
    fenetre.setTitle("comparaison");
    fenetre.setSize(400, 100);
    fenetre.setLocationRelativeTo(null);
    JPanel pan1 = new JPanel();
    pan1.setLayout(new BorderLayout());
    JLabel l = new JLabel(x);
    pan1.add(l);
    fenetre.add(pan1, BorderLayout.EAST);
    fenetre.setVisible(true);
  }    
public static ArrayList<Rect> detection_contours(Mat outmat) {
    Mat v = new Mat();
    Mat vv = outmat.clone();
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(vv, contours, v, Imgproc.RETR_LIST,
            Imgproc.CHAIN_APPROX_SIMPLE);

    double maxArea = 100;
    int maxAreaIdx = -1;
    Rect r = null;
    ArrayList<Rect> rect_array = new ArrayList<Rect>();

    for (int idx = 0; idx < contours.size(); idx++) { Mat contour = contours.get(idx); double contourarea = Imgproc.contourArea(contour); if (contourarea > maxArea) {
            // maxArea = contourarea;
            maxAreaIdx = idx;
            r = Imgproc.boundingRect(contours.get(maxAreaIdx));
            rect_array.add(r);
            //Imgproc.drawContours(imag, contours, maxAreaIdx, new Scalar(0,0, 255));
        }
    }
    v.release();
    return rect_array;
}
public Mat motion(Mat f){
    Mat frame = f;
    Mat outerBox = new Mat();
    Mat diff_frame = null;
    Mat tempon_frame = null;
    //VideoCapture camera = new VideoCapture("C:/Users/SAAD/Desktop/ratiler/projects/motion.mp4");
    Size sz = new Size(640, 480);
    int i = 0;

    while (true) {
        //camera.open(0);
        //if (camera.read(frame)) {
            Imgproc.resize(frame, frame, sz);
            imag = frame.clone();
           outerBox = new Mat(frame.size(), CvType.CV_8UC1);
           Imgproc.cvtColor(frame, outerBox, Imgproc.COLOR_BGR2GRAY);
           Imgproc.GaussianBlur(outerBox, outerBox, new Size(3, 3), 0);

            if (i == 0) {
                diff_frame = new Mat(frame.size(), CvType.CV_8UC1);
                tempon_frame = new Mat(outerBox.size(), CvType.CV_8UC1);
            }

            if (i == 1) {
                Core.subtract(outerBox, tempon_frame, diff_frame);
                Imgproc.adaptiveThreshold(diff_frame, diff_frame, 255,
                        Imgproc.ADAPTIVE_THRESH_MEAN_C,
                        Imgproc.THRESH_BINARY_INV, 5, 2);

            }
            i = 1;
            return imag;
}}

}