2
votes

i'm trying to learn openCV in C++ and i'm working on codeblocks 13.12 with the latest version of openCV on Windows 7(x64). As a beginning i tested a simple output of my webcam, witch worked without troubles. Next, i tried to make modify the output with cvtColor(img, img_gray, CV_BGR2GRAY). As you can see below i'm getting 2 error messages. Also to clarify, VisuWork vW is a class that i made in order to code properly in OOP as exercise. The class is not doing anything special. Please can anyone help me to find an issue to this problem? Tks for your help and time.


main.cpp :

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <opencv/highgui.h>

using namespace cv;
using namespace std;

    class VisuWork
    {
        CvCapture* capture;
    public:
        VisuWork()
        {
            capture=cvCaptureFromCAM(CV_CAP_ANY);
        };
        template<typename Type> void sHow(Type frame)
        {
    //        IplImage* frame = cvQueryFrame(capture); //Create image frames from capture
            cvShowImage("Camera_Output", frame); //Show image frames on created window
        }
    //    void imHSV(IplImage* frame){
    //        Mat im(frame);
    //        Mat hsv;
    //        cvtColor(im, hsv, CV_BGR2HSV);
    //    }
        ~VisuWork()
        {
            cvReleaseCapture(&capture); //Release capture.
            cvDestroyWindow("Camera_Output"); //Destroy Window
        }

    };

        int main( int argc, const char** argv )
        {
            CvCapture* capture = 0;
            capture = cvCaptureFromCAM(CV_CAP_ANY);
            if (!capture)
            {
                // print error, quit application
            }
            else
            {
                VisuWork vW;
                while(1)  //Create infinte loop for live streaming
                {
                    IplImage* im = cvQueryFrame(capture);
                    if(!im){}
                    else{
                    //        vW.imHSV(frame);

                    Mat img_gray(im);
                    vW.sHow(im);
                  cvtColor(img, img_gray, CV_BGR2GRAY);
                    }
                    int key = cvWaitKey(10); //Capture Keyboard stroke
                    if (char(key) == 27)
                    {
                        break; //If you hit ESC key loop will break.
                    }
                }
                cvReleaseCapture(&capture); //Release capture.
                cvDestroyWindow("Camera_Output"); //Destroy Window
            }

        }

The error messages:

C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text+0x7b): undefined reference to cv::Mat::Mat(_IplImage const*, bool) C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]+0x2d): undefined reference to cv::fastFree(void*)


This is the complete build log:


mingw32-g++.exe -std=c++11 -Wall -IC:\RPS\python\edp32\opencv\build\include -c C:\Users\PTOSH\Documents\tried.cpp -o C:\Users\PTOSH\Documents\tried.o mingw32-g++.exe -LC:\RPS\python\edp32\opencv\build\x86\vc12\lib -L"C:\Program Files (x86)\CodeBlocks\MinGW\lib" -LC:\RPS\python\edp32\opencv\sources\3rdparty -o C:\Users\PTOSH\Documents\tried.exe C:\Users\PTOSH\Documents\tried.o C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_calib3d248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_calib3d248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_contrib248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_contrib248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_core248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_core248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_features2d248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_features2d248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_flann248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_flann248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_gpu248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_gpu248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_highgui248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_highgui248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_imgproc248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_imgproc248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_legacy248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_legacy248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_ml248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_ml248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_nonfree248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_nonfree248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_objdetect248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_objdetect248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_ocl248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_ocl248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_photo248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_photo248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_stitching248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_stitching248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_superres248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_superres248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_ts248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_ts248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_video248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_video248d.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_videostab248.lib C:\RPS\python\edp32\opencv\build\x86\vc12\lib\opencv_videostab248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_calib3d248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_calib3d248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_contrib248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_contrib248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_core248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_core248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_features2d248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_features2d248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_flann248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_flann248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_gpu248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_gpu248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_highgui248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_highgui248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_imgproc248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_imgproc248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_legacy248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_legacy248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_ml248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_ml248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_nonfree248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_nonfree248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_objdetect248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_objdetect248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_ocl248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_ocl248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_photo248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_photo248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_stitching248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_stitching248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_superres248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_superres248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_ts248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_ts248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_video248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_video248d.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_videostab248.lib C:\RPS\python\edp32\opencv\build\x64\vc12\lib\opencv_videostab248d.lib C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text+0x7b): undefined reference to cv::Mat::Mat(_IplImage const*, bool)' C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]+0x2d): undefined reference tocv::fastFree(void)'* c:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\PTOSH\Documents\tried.o: bad reloc address 0x2d in section `.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]' c:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation collect2.exe: error: ld returned 1 exit status Process terminated with status 1 (0 minute(s), 1 second(s)) 2 error(s), 0 warning(s) (0 minute(s), 1 second(s))

3
"i'm trying to learn openCV in C++" - but you're using the c-api (which is a bad idea)berak
Is it a good idea to mix C API inside a class?a-Jays

3 Answers

1
votes

if you really want to use the c++ api (and you should, since the old c-one is dead),

CvCapture* capture; 
// no, use: cv::VideoCapture

IplImage* frame = cvQueryFrame(capture); 
// no, use: cv::Mat frame; capture.read(frame)

etc. throw out all cv* functions and replace them with their cv:: counterparts (maybe better to start again from scratch)

(btw, code snippets from SO are almost always outdated, be wary of that)

and read the docs (tutorial links at the bottom)


the whole camera-capture boils down to very few lines in c++:

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;

int main()
{
    VideoCapture cap(0); // get first cam
    while( cap.isOpened() )
    {
        Mat frame;
        if ( ! cap.read(frame) ) // cam might need some warmup
            continue;

        // your processing goes here

        imshow("lalala",frame);
        if ( waitKey(10)==27 )
            break;
    }
    return 0;
}
0
votes

@berak: Ok i removed #include <opencv/highgui.h>. But still facing problems with the cvtColor() part.

C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text+0x75): undefined reference to cv::Mat::Mat(_IplImage const*, bool)' C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text+0x9b): undefined reference tocv::_OutputArray::_OutputArray(cv::Mat&)' C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text+0xae): undefined reference to cv::_InputArray::_InputArray(cv::Mat const&)' C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text+0xd3): undefined reference tocv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'

@a-Jays: Why not? Anyway, i removed the class, maybe easier by starting to test openCV first without.

Tks for your responses.

0
votes

@berak:

if you really want to use the c++ api (and you should, since the old c-one is dead),

> CvCapture* capture;  // no, use: cv::VideoCapture
> 
> IplImage* frame = cvQueryFrame(capture);  // no, use: cv::Mat frame;
> capture.read(frame)

etc. throw out all cv* functions and replace them with their cv:: counterparts (maybe better to start again from scratch)

(btw, code snippets from SO are almost always outdated, be wary of that)

and read the docs (tutorial links at the bottom)

the whole camera-capture boils down to very few lines in c++:

> #include "opencv2/core/core.hpp"
> #include "opencv2/imgproc/imgproc.hpp"
> #include "opencv2/highgui/highgui.hpp"
> 
> using namespace cv;
> 
> int main() {
>     VideoCapture cap(0); // get first cam
>     while( cap.isOpened() )
>     {
>         Mat frame;
>         if ( ! cap.read(frame) ) // cam might need some warmup
>             continue;
> 
>         // your processing goes here
> 
>         imshow("lalala",frame);
>         if ( waitKey(10)==27 )
>             break;
>     }
>     return 0; }

Your code is returning me this errors below, is it perhaps revealing a link failure in my codeblocks settings?:

C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text+0x35): undefined reference to cv::VideoCapture::VideoCapture(int)' C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text+0x57): undefined reference tocv::VideoCapture::read(cv::Mat&)' C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text+0x7b): undefined reference to cv::_InputArray::_InputArray(cv::Mat const&)' C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text+0xb5): undefined reference tocv::imshow(std::string const&, cv::_InputArray const&)' C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text+0xd5): undefined reference to cv::waitKey(int)' C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text+0x109): undefined reference tocv::VideoCapture::isOpened() const' C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text+0x120): undefined reference to cv::VideoCapture::~VideoCapture()' C:\Users\PTOSH\Documents\tried.o:tried.cpp:(.text+0x162): undefined reference tocv::VideoCapture::~VideoCapture()'

Tks for your help.