Strange behaviour with OpenCV Camera “things”

I am currently working on a project that does some image processing and I have to use a webcam as image source. My choice is OpenCV and Qt (of course).
The new OpenCV version (2.3) just came out when I started my project so I got the source and compiled it.
After looking through the documentation I tried to use the new C++ API and cv::VideoCapture class instead of the C-style CvCapture structure but failed since cv::VideoCapture can’t find my webcam for some reason.

However my old projects that used the webcam still worked I simply kept on using CvCapture.
After a while I got an error when tried to use cv::Mat::operator() for subimage extraction and received strange runtime errors.

Since the OpenCV samples did not work without a camera I created some console applications to figure out what goes wrong.

My final product I here:

This simple program does nothing special. If you give no argument to it, it will try to retreive a picture from your webcam using the C-style old API. If any argument is given, it will try to open the first one and create a subimage of it.

Check out the code I continue the story below it.

#include <iostream>
#include <opencv2/highgui/highgui.hpp>

int main( int argc, char** argv )
{
    cv::Mat img;
    CvCapture *cam = 0;

    if(argc < 2)
    {
        std::cout << "Using webcam" << std::endl;
        cam = cvCreateCameraCapture(0);
        assert(cam);
        img = cvQueryFrame(cam);
    }
    else
    {
        std::cout << "Opening file: " << argv[1] << std::endl;
        img = cv::imread(argv[1]);
        if(img.empty())
        {
         return -1;
        }
        cv::Mat subimg = img(cv::Rect(0, 0, 100, 100));
        std::cout << subimg.cols << "x" << subimg.rows << std::endl;
    }

 std::cout << img.cols << "x" << img.rows << std::endl;

        cv::namedWindow("dunno");
        cv::imshow("dunno",img);
        cv::waitKey();
 if(cam != 0)
         cvReleaseCapture(&cam);

 return 0;
}

So now you know what this tiny program does.
The interesting thing is that it only does one of the two functions listed above and what is does is determined by how you compiled it.

  1. g++ -lopencv_core -lopencv_highgui -o librarytest librarytest.cpp
  2. g++ -lcv -lhighgui -lopencv_core -lopencv_highgui -o librarytest librarytest.cpp
  3. g++ -lopencv_legacy -lopencv_core -lopencv_highgui -o librarytest librarytest.cpp

These 3 lines are what I came up with for compilation and the outcomes are as follows:

  1. It works with a file but cannot find the camera. CvCreateCameraCapture returns 0 even if it is called with -1 argument (find any camera).
  2. It works with the camera, but gives segmentation fault when trying to use cv::Mat::operator() or cv::imread() so any of the “new stuff”.
  3. Seemed like there’s hope but it’s the same as the first one.
What lies under?
It is simple. I have the previous install of OpenCV back from March named libcv.so.2.0.0 and libhighgui.so.2.0.0 (referenced as -lcv and -lhighgui at compilation) and I also have the new 2.3 library built in July named libopencv_core.so.2.3.0, etc.
I get segmentation fault when trying to use both libraries because the old api simply does not contain what the new does and messes up these. It is absolutely ok but my main problem remains still since I can’t get VideoCapture or the new CvCapture to work with my laptops webcam.
I will also post this on OpenCV-users mailing list and share the solution. I hope this will help someone (me at least) 🙂
Advertisements

2 thoughts on “Strange behaviour with OpenCV Camera “things”

  1. Hey did you ever find a solution to this? I’ve been wrestling with segmentation faults whenever I call cvCreateCameraCapture. As I have previous versions of OpenCV installed, it seems this issue is related.

    1. It surely is related. I think that the best solution here is either to remove the previous OpenCV versions or hide them from the PATH when the building is done and only specify those files that belong to the desired version to use.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s