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
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;
}
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().
do what UtkarshV said, the resp. c++ line would be :
how did you reright this code ? i have same problem
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.