2

我正在从 OpenCV Cookbook 中学习 OpenCV。在那,他在视频中给出了精明边缘检测的代码。这是我尝试过的他给出的代码。

#include "cv.h"
#include "highgui.h"
#include <string>
using namespace cv;
using namespace std;

void canny (Mat& img, Mat& out)
{
    // Convert image to gray
    if (img.channels () == 3)
    {
        cvtColor (img, out, CV_BGR2GRAY);
    }
    Canny (out, out, 100, 200);
    threshold (out, out, 128, 255, THRESH_BINARY_INV);
}

class VideoProcessor
{
    private:
        VideoCapture capture;
        bool callIt;
        void* process (Mat&, Mat&);
        string windowNameInput;
        string windowNameOutput;
        int delay;
        long fnumber;
        long frameToStop;
        bool stop;
    public:
        VideoProcessor () : callIt (true), delay (0), fnumber (0), stop (false), frameToStop (-1) {};
        int getFrameRate ()
        {
            return capture.get (CV_CAP_PROP_FPS);
        }
        void setFrameProcessor (void* frameProcessingCallback (Mat&, Mat&))
        {
            process = frameProcessingCallback;
        }
        bool setInput (string filename)
        {
            fnumber = 0;
            capture.release ();
            return capture.open (filename);
        }
        void displayInput (string wn)
        {
            windowNameInput = wn;
            namedWindow (windowNameInput);
        }
        void displayOutput (string wn)
        {
            windowNameOutput = wn;
            namedWindow (windowNameOutput);
        }
        void dontDisplay ()
        {
            destroyWindow (windowNameInput);
            destroyWindow (windowNameOutput);
            windowNameInput.clear ();
            windowNameOutput.clear ();
        }
        void run ()
        {
            Mat frame;
            Mat output;
            if (!isOpened ())
            {
                return;
            }
            stop = false;
            while (!isStopped ())
            {
                if (!readNextFrame (frame))
                {
                    break;
                }
                if (windowNameInput.length () != 0)
                {
                    imshow (windowNameInput, frame);
                }
                if (callIt)
                {
                    process (frame, output);
                    fnumber++;
                }
                else
                {
                    output = frame;
                }
                if (windowNameOutput.length () != 0)
                {
                    imshow (windowNameOutput, output);
                }
                if (delay >= 0 && waitKey (delay) >= 0)
                {
                    stopIt ();
                }
                if (frameToStop >= 0 && getFrameNumber () == frameToStop)
                {
                    stopIt ();
                }
            }
        }
        void stopIt ()
        {
            stop = true;
        }
        bool isStopped ()
        {
            return stop;
        }
        bool isOpened ()
        {
            capture.isOpened ();
        }
        void setDelay (int d)
        {
            delay = d;
        }
        bool readNextFrame (Mat& frame)
        {
            return capture.read (frame);
        }
        void callProcess ()
        {
            callIt = true;
        }
        void dontCallProcess ()
        {
            callIt = false;
        }
        void stopAtFrameNo (long frame)
        {
            frameToStop = frame;
        }
        long getFrameNumber ()
        {
            long fnum = static_cast <long> (capture.get (CV_CAP_PROP_POS_FRAMES));
            return fnum;
        }
};

int main ()
{
    VideoProcessor processor;
    processor.setInput ("video2.MOV");
    processor.displayInput ("Current Frame");
    processor.displayOutput ("Output Frame");
    processor.setDelay (1000 / processor.getFrameRate ());
    processor.process (, canny);
    processor.run ();
}

编译器在函数中给出错误,setFrameProcessor我无法修复它。有人可以帮忙吗?

4

1 回答 1

2

您对函数指针的初始化是错误的。您需要将函数的引用传递给指针进程

void setFrameProcessor (void frameProcessingCallback (Mat&, Mat&))
{
   process = &frameProcessingCallback;
}

另一个简单的例子:

#include <stdio.h>
void A()
{
printf("A");
}
void B(void A(void))
{
void (*f_ptr)(void);
f_ptr = &A;
f_ptr();
}
int main()
{
B(A);
return 0;
}

有关更多详细信息,请参阅以下链接。

http://www.cprogramming.com/tutorial/function-pointers.html

顺便说一句,这个问题与标题无关。我建议更改标题。

于 2012-11-08T12:14:04.370 回答