Reputation: 3781
I am trying to run the following code:
#include <highgui.h>
#include <iostream>
#include <stdio.h>
#include <cv.h>
using namespace std;
using namespace cv;
using namespace std;
int main()
{
cvNamedWindow("Brezel detecting camera", 1);
// Capture images from any camera connected to the system
CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
// Load the trained model
CascadeClassifier brezelDetector;
brezelDetector.load("src/person.xml");
if (brezelDetector.empty())
{
printf("Empty model.");
return 0;
}
char key;
while (true)
{
// Get a frame from the camera
Mat frame = cvQueryFrame(capture); //----->>>>>>> This line
std::vector<Rect> brezels;
// Detect brezels
brezelDetector.detectMultiScale(frame, brezels, 1.1, 30,
0 | CV_HAAR_SCALE_IMAGE, Size(200, 320));
for (int i = 0; i < (int) brezels.size(); i++)
{
Point pt1(brezels[i].x, brezels[i].y);
Point pt2(brezels[i].x + brezels[i].width,
brezels[i].y + brezels[i].width);
// Draw a rectangle around the detected brezel
rectangle(frame, pt1, pt2, Scalar(0, 0, 255), 2);
putText(frame, "Brezel", pt1, FONT_HERSHEY_PLAIN, 1.0,
Scalar(255, 0, 0), 2.0);
}
// Show the transformed frame
imshow("Brezel detecting camera", frame);
// Read keystrokes, exit after ESC pressed
key = cvWaitKey(10);
if (char(key) == 27)
{
break;
}
}
return 0;
}
But I am getting "conversion from ‘IplImage* {aka _IplImage*}’ to non-scalar type ‘cv::Mat’ requested" error on this line:
Mat frame = cvQueryFrame(capture);
I am using opencv3. How can I fix this issue?
Thanks,
Upvotes: 1
Views: 4701
Reputation: 852
First get image in IplImage like this:
IplImage* image = cvQueryFrame(capture);
then,
Mat matImage(image);
Upvotes: 0
Reputation: 5354
Well, I don't really advise to you to use the old/obsolete IplImage
format, but the conversion into cv::Mat
is possible as below:
cv::Ptr<IplImage> iplimg(cvQueryFrame(capture)); // cv::Ptr<T> is safe ref-counting pointer class
if(!iplimg)
{
break;
}
// cv::Mat replaces the CvMat and IplImage, but it's easy to convert
// between the old and the new data structures (by default, only the header
// is converted, while the data is shared)
cv::Mat img = cv::cvarrToMat(iplimg);
Why don't you use cv::VideoCapture
instead?
cv::VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
while(true)
{
cv::Mat frame;
cap >> frame; // get a new frame from camera
cv::imshow("frame", frame);
cv::waitKey(1);
}
Upvotes: 4
Reputation: 80
Try using the constructor:
Mat frame(cvQueryFrame(capture));
Or with newer OpenCV versions where the constructor has been removed:
Mat frame = cvarrToMat(cvQueryFrame(capture));
Upvotes: 1