1

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,

yusuf
  • 3,591
  • 8
  • 45
  • 86
  • 1
    Not sure but : `Mat` type is only for C++ Opencv and `cvQueryFrame` is used with C OpenCV, so you should choose one of those possibility : everything in C or everything in C++ – Pierre Jan 26 '16 at 10:46
  • So what is the alternative of cvQueryFrame in c++? – yusuf Jan 26 '16 at 10:46
  • 2
    using a `VideoCapture` object (example at the end) : http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html – Pierre Jan 26 '16 at 10:49
  • No, it didn't solve the problem. sorry. – yusuf Jan 26 '16 at 10:51
  • 1
    Still the same error ? Can you update the code ? – Pierre Jan 26 '16 at 10:53
  • please **avoid** using IplImage* and cv* functions at all cost. opencv moved away from that half a decade ago, you **must** use the c++ api (cv::Mat) consistently now. – berak Jan 29 '16 at 12:43

3 Answers3

4

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);
}
Kornel
  • 5,264
  • 2
  • 21
  • 28
  • Thank you for your answer Kornel. But I am encountering a makefile error: /usr/local/lib/libopencv_imgproc.so.3.1: error adding symbols: DSO missing from command line – yusuf Jan 26 '16 at 12:10
  • 1
    this is a linker error, see the following for more information: http://stackoverflow.com/questions/19901934/strange-linking-error-dso-missing-from-command-line – Kornel Jan 26 '16 at 12:12
  • Kornel, actually this opencv3 is a headache. Is it logical to install opencv older versions? – yusuf Jan 26 '16 at 12:13
  • 1
    yes, why not? OpenCV 2.4.x is quite stable and provides a lot of functionality. I also have and maintain a lot of projects where we're using 2.4.x – Kornel Jan 26 '16 at 12:18
1

Try using the constructor:

Mat frame(cvQueryFrame(capture));

Or with newer OpenCV versions where the constructor has been removed:

Mat frame = cvarrToMat(cvQueryFrame(capture));
Siim Veskilt
  • 80
  • 10
0
  • First get image in IplImage like this:

    IplImage* image = cvQueryFrame(capture);
    

    then,

    Mat matImage(image);
    
Sagar Patel
  • 864
  • 1
  • 11
  • 22