Ask Your Question
0

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

asked 2017-02-15 04:52:12 -0500

DrMessina gravatar image

updated 2017-02-15 04:57:29 -0500

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 ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by DrMessina
close date 2017-04-04 03:32:13.175995

Comments

1

BTW, you don't have to copy_paste all your code...and try to do a little debugging.

kbarni gravatar imagekbarni ( 2017-02-15 05:09:10 -0500 )edit

you're actually overwriting your frame with an empty one. (just discard that line. the detection will do a grayscale conversion on it's own, under the hood)

berak gravatar imageberak ( 2017-02-15 05:25:50 -0500 )edit

ok i'll try debug thx

DrMessina gravatar imageDrMessina ( 2017-02-15 05:56:21 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-02-15 05:00:49 -0500

kbarni gravatar image

It's absolutely normal. Just check the code:

frame= new Mat(frame.size(), CvType.CV_8UC1); //8 bit/pixel, one channel (grayscale)
...
Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY); //trying to convert a color (BGR) image to grayscale

The error says that for a color->grayscale (COLOR_BGR2GRAY) conversion it needs a color (3 or 4 channel) image as input.

edit flag offensive delete link more

Comments

that's kinda weird... i test the motion alone and it says nothing the error only appear when i try to do the motion and face detection at the same time... thank you i'll try to fix this

DrMessina gravatar imageDrMessina ( 2017-02-15 05:57:20 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-02-15 04:52:12 -0500

Seen: 3,641 times

Last updated: Feb 15 '17