2012-12-28 38 views
1

最近我在我的機器上安裝了Opencv。它在Python中工作的很好(我只是通過一些程序來檢查它)。但由於缺乏python教程,我決定搬到c。我剛剛從http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/如何在C中安裝opencv的頭文件在我的Ubuntu 11.10

運行Hello World程序我的程序是

#include <stdlib.h> 
#include <stdio.h> 
#include <math.h> 
#include <cv.h> 
#include <highgui.h> 


int main(int argc, char *argv[]) 
{ 
    IplImage* img = 0; 
    int height,width,step,channels; 
    uchar *data; 
    int i,j,k; 

    if(argc<2){ 
    printf("Usage: main <image-file-name>\n\7"); 
    exit(0); 
    } 

    // load an image 
    img=cvLoadImage(argv[1]); 
    if(!img){ 
    printf("Could not load image file: %s\n",argv[1]); 
    exit(0); 
    } 

    // get the image data 
    height = img->height; 
    width  = img->width; 
    step  = img->widthStep; 
    channels = img->nChannels; 
    data  = (uchar *)img->imageData; 
    printf("Processing a %dx%d image with %d channels\n",height,width,channels); 

    // create a window 
    cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); 
    cvMoveWindow("mainWin", 100, 100); 

    // invert the image 
    for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++) 
    data[i*step+j*channels+k]=255-data[i*step+j*channels+k]; 

    // show the image 
    cvShowImage("mainWin", img); 

    // wait for a key 
    cvWaitKey(0); 

    // release the image 
    cvReleaseImage(&img); 
    return 0; 
} 

首先在編譯時,我得到了以下錯誤

hello-world.c:4:16: fatal error: cv.h: No such file or directory 
compilation terminated. 

,我通過編譯這樣

糾正這個錯誤
gcc -I/usr/lib/perl/5.12.4/CORE -o hello-world hello-world.c 

但現在的錯誤是

In file included from hello-world.c:4:0: 
/usr/lib/perl/5.12.4/CORE/cv.h:14:5: error: expected specifier-qualifier-list before ‘_XPV_HEAD’ 
hello-world.c:5:21: fatal error: highgui.h: No such file or directory 
compilation terminated. 

問題: 這個頭文件沒有安裝在我的系統中嗎?當我使用這個命令find/usr -name「highgui.h」我找不到任何東西 如果這個頭文件不在我的sysytem hoew中,我安裝了這個?

請幫幫我。我在OpenCV的新

回答

-1

我在我的項目中的以下標題:

#include <opencv2/opencv.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <opencv2/objdetect/objdetect.hpp> 
#include <opencv2/features2d/features2d.hpp> 

的OpenCV的版本2.4.2

4
如果highgui.h存在於您的計算機上

首先檢查:

sudo find /usr/include -name "highgui.h" 

如果你找到它的路徑可以說 「/usr/include/opencv/highgui.h」 然後使用:

#include <opencv/highgui.h> in your c file. 

在編譯時您可以添加

-I/usr/include/opencv in gcc line 

但隨後你包括在C文件行應該變成:

#include "highgui.h" 

如果,你的第一個命令失敗,那就是你不t在你的機器上找到「highgui.h」。那麼顯然你錯過了一些包。爲了弄清楚該套件名稱,使用apt-find命令:

sudo apt-find search highgui.h 

我的機器上,它給了我這樣的:

libhighgui-dev: /usr/include/opencv/highgui.h 
libhighgui-dev: /usr/include/opencv/highgui.hpp 

如果你沒有容易找到的,然後先安裝它,使用:

sudo apt-get install apt-find 

所以,現在你知道包名稱,然後問題:

sudo apt-get install libhighgui-dev 

一旦完成,請使用find命令查看確切位置,標頭已安裝,然後使用,然後相應地更改包含路徑