Ask Your Question
0

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

asked 2014-01-04 01:47:35 -0600

Potsky gravatar image

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

berak gravatar image

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;

}

edit retag flag offensive close merge delete

Comments

See if you can get more information on variable the cameraFeed. I would start by printing out cameraFeed.rows, cameraFeed.cols, cameraFeed.type(), cameraFeed.channels().

Nghia gravatar imageNghia ( 2014-01-04 19:05:44 -0600 )edit
2

do what UtkarshV said, the resp. c++ line would be :

bool ok = capture.read(cameraFeed);
if ( ! ok ) continue;   // 1st frame might be invalid
berak gravatar imageberak ( 2014-01-05 08:25:02 -0600 )edit

how did you reright this code ? i have same problem

salomegogisvanidze gravatar imagesalomegogisvanidze ( 2015-06-10 09:47:03 -0600 )edit

He should have checked if the reading of the capture actually returned a value. He can do that by checking the returning boolean value as indicated by @berak.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-06-11 08:23:48 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2014-01-05 00:13:51 -0600

UtkarshV gravatar image

updated 2014-01-05 00:14:43 -0600

Do a truth check after capture.read on the returned value, if the return is true, only then proceed. I faced a similar problem, this fixed that.

See this example:

import numpy as np
import cv2

cap=cv2.VideoCapture(0)  
while(cap.isOpened()):
    #cpature frame by frame
    ret,frame=cap.read()
    if(ret):        #if cam read is successful

        #ops here:
        gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

        #disp the resulting frame
        cv2.imshow('Grayframe :: q to exit',gray)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
#when everything else done,release capture
cap.release()
cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

Yep, it works now, thanks!! :D

Potsky gravatar imagePotsky ( 2014-01-10 20:37:49 -0600 )edit

Question Tools

Stats

Asked: 2014-01-04 01:47:35 -0600

Seen: 11,844 times

Last updated: Jun 10 '15