2012-11-08 106 views
1

我正在從OpenCV Cookbook中學習OpenCV。在那裏,他給出了視頻中Canny邊緣檢測的代碼。這是他給我的代碼,我曾嘗試過。OpenCV Canny邊緣檢測C++中的視頻

#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功能的錯誤,我無法修復它。任何人都可以幫忙嗎?

+0

如果你說錯誤是什麼,幫助就容易多了。 – molbdnilo

+0

VideoProcessor中的'process'應該聲明爲函數指針,如void(* porcess)(Mat&,Mat&);'。並且在'run()'中用'(* process)(frame,output)調用它;' – luhb

回答

1

函數指針的初始化是錯誤的。你需要的功能的引用傳遞指針過程

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

順便說一句,這個問題無關的標題。我會建議改變標題。

+0

我該怎麼改變它? – pratnala

+0

「使用函數指針」如何? –