我正在尝试学习C++中的openCV,并且正在使用Windows 7(x64)上最新版本的openCV来处理代码块13.12。一开始,我测试了我的网络摄像头的简单输出,女巫的工作没有麻烦。接下来,我尝试使用cvtColor(img, img_gray, CV_BGR2GRAY)修改输出。如您在下面看到的,我收到2条错误消息。同样要澄清的是,VisuWork vW是我为了在练习中正确地在OOP中进行编码而制作的一个类。该类没有做任何特别的事情。
请任何人可以帮助我找到有关此问题的问题吗?
谢谢您的帮助和时间。

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
            }

        }

错误消息:

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*)

这是完整的构建日志:

最佳答案

如果您确实要使用c++ api(并且您应该这样做,因为旧的c-one已死),

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

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

等抛出所有cv *函数,并用其cv::对应物替换
(最好从头开始)

(顺便说一句,SO的代码片段几乎总是过时了,请注意)

并阅读docs(底部的教程链接)

在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;
}

07-26 06:06