i have this Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor [closed]
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 ...
BTW, you don't have to copy_paste all your code...and try to do a little debugging.
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)
ok i'll try debug thx