Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to fix OpenCV Error Assertion ((scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) ) ?

So I bought a new cam A4Tech PK-635G, and when I run the program this error pops up image description

But when I try my old cam, it worked properly. Here's the main code.

  Mat cameraFeed;
Mat threshold;
Mat HSV;
//video capture object to acquire webcam feed
VideoCapture capture;

//open capture object at location zero (default location for webcam)
capture.open(0);

//set height and width of capture frame
capture.set(0,FRAME_WIDTH);
capture.set(0,FRAME_HEIGHT);

//start an infinite loop where webcam feed is copied to cameraFeed matrix
//all of our operations will be performed within this loop
while(1)
{

    //store image to matrix
    capture.read(cameraFeed);

    //convert frame from BGR to HSV colorspace
    cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);



        //create some temp fruit objects so that
        //we can use their member functions/information
        Fruit apple("Front"), banana("Back"), tar ("target");


        //first find apples
        cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);
        inRange(HSV,apple.getHSVmin(),apple.getHSVmax(),threshold);
        morphOps(threshold);
        trackRedFilteredObject(apple,threshold,HSV,cameraFeed);

        //then bananas
        cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);
        inRange(HSV,banana.getHSVmin(),banana.getHSVmax(),threshold);
        morphOps(threshold);
        trackYellowFilteredObject(banana,threshold,HSV,cameraFeed);

        //then bananas
        cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);
        inRange(HSV,tar.getHSVmin(),tar.getHSVmax(),threshold);
        morphOps(threshold);
        trackTargetFilteredObject(tar,threshold,HSV,cameraFeed);

        drawLine ( cameraFeed );
        getOrientation ( Point ( redX, redY ), Point ( yellowX, yellowY ), cameraFeed );

        drawLineTar ( cameraFeed );
        getTargetOrientation (  Point ( tarX, tarY ), Point ( botX, botY ), cameraFeed );

        if ( tarX > 0 && tarY > 0 && botX > 0 && botY > 0 && redX > 0 && redY > 0 ) getDegree ( cameraFeed );

    //}

    imshow(windowName,cameraFeed);
    waitKey(30);

}

return 0;

}

click to hide/show revision 2
retagged

updated 2014-01-04 02:52:44 -0600

berak gravatar image

How to fix OpenCV Error Assertion ((scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) ) ?

So I bought a new cam A4Tech PK-635G, and when I run the program this error pops up image description

But when I try my old cam, it worked properly. Here's the main code.

  Mat cameraFeed;
Mat threshold;
Mat HSV;
//video capture object to acquire webcam feed
VideoCapture capture;

//open capture object at location zero (default location for webcam)
capture.open(0);

//set height and width of capture frame
capture.set(0,FRAME_WIDTH);
capture.set(0,FRAME_HEIGHT);

//start an infinite loop where webcam feed is copied to cameraFeed matrix
//all of our operations will be performed within this loop
while(1)
{

    //store image to matrix
    capture.read(cameraFeed);

    //convert frame from BGR to HSV colorspace
    cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);



        //create some temp fruit objects so that
        //we can use their member functions/information
        Fruit apple("Front"), banana("Back"), tar ("target");


        //first find apples
        cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);
        inRange(HSV,apple.getHSVmin(),apple.getHSVmax(),threshold);
        morphOps(threshold);
        trackRedFilteredObject(apple,threshold,HSV,cameraFeed);

        //then bananas
        cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);
        inRange(HSV,banana.getHSVmin(),banana.getHSVmax(),threshold);
        morphOps(threshold);
        trackYellowFilteredObject(banana,threshold,HSV,cameraFeed);

        //then bananas
        cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);
        inRange(HSV,tar.getHSVmin(),tar.getHSVmax(),threshold);
        morphOps(threshold);
        trackTargetFilteredObject(tar,threshold,HSV,cameraFeed);

        drawLine ( cameraFeed );
        getOrientation ( Point ( redX, redY ), Point ( yellowX, yellowY ), cameraFeed );

        drawLineTar ( cameraFeed );
        getTargetOrientation (  Point ( tarX, tarY ), Point ( botX, botY ), cameraFeed );

        if ( tarX > 0 && tarY > 0 && botX > 0 && botY > 0 && redX > 0 && redY > 0 ) getDegree ( cameraFeed );

    //}

    imshow(windowName,cameraFeed);
    waitKey(30);

}

return 0;

}