linux opencv 安装和配置

在fedora16 和 CentOS 下面配置OpenCV,由于不同主机装的软件的都不一样,所以不能一概而论

大概需要的开发套件:

pkgconfig  libpng  zlib libjpeg  libtiff libjasper

ffmepeg GTK+ 的基本库(这里可选,用到的时候再装也行)

下面是我的主机安装配置OpenCV。

opencv 下载

 http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.2/

在centos和fedora16下面,一般都把用户的软件装在 /usr/local/xxx 下面

$ cp OpenCV-2.4.0.tar.bz2 /usr/local/

$ tar -xvf OpenCV-2.4.0.tar.bz2

$ cd  OpenCV-2.4.0

2.4.0的下面是以CMakeLists.txt形式

$ yum install cmake

$ cmake CMakeLists.txt

$ make && make install

OpenCV 会安装在 /usr/local/lib 目录和 /usr/local/include/opencv/

环境变量配置

$ vim /etc/ld.so.conf

最后一行加入 /usr/local/lib

$ ldconfig

或者

export LD_LIBRARY_PATH=/usr/local/lib

OpenCV测试

  1. #ifdef _CH_  
  2. #pragma package <opencv>  
  3. #endif  
  4.   
  5. #define CV_NO_BACKWARD_COMPATIBILITY  
  6.   
  7. #ifndef _EiC  
  8. #include "cv.h"  
  9. #include "highgui.h"  
  10. #include <stdlib.h>  
  11. #include <stdio.h>  
  12. #endif  
  13.   
  14. #define NUMBER 100  
  15. #define DELAY 5  
  16. char wndname[] = "Drawing Demo";  
  17.   
  18. CvScalar random_color(CvRNG* rng)  
  19. {  
  20.     int icolor = cvRandInt(rng);  
  21.     return CV_RGB(icolor&255, (icolor>>8)&255, (icolor>>16)&255);  
  22. }  
  23.   
  24. int main( int argc, char** argv )  
  25. {  
  26.     int line_type = CV_AA; // change it to 8 to see non-antialiased graphics  
  27.     int i;  
  28.     CvPoint pt1,pt2;  
  29.     double angle;  
  30.     CvSize sz;  
  31.     CvPoint  ptt[6];  
  32.     CvPoint* pt[2];  
  33.     int  arr[2];  
  34.     CvFont font;  
  35.     CvRNG rng;  
  36.     int width = 1000, height = 700;  
  37.     int width3 = width*3, height3 = height*3;  
  38.     CvSize text_size;  
  39.     int ymin = 0;  
  40.     // Load the source image  
  41.     IplImage* image = cvCreateImage( cvSize(width,height), 8, 3 );  
  42.     IplImage* image2;  
  43.   
  44.     // Create a window  
  45.     cvNamedWindow(wndname, 1 );  
  46.     cvZero( image );  
  47.     cvShowImage(wndname,image);  
  48.     cvWaitKey(DELAY);  
  49.   
  50.     rng = cvRNG((unsigned)-1);  
  51.     pt[0] = &(ptt[0]);  
  52.     pt[1] = &(ptt[3]);  
  53.   
  54.     arr[0] = 3;  
  55.     arr[1] = 3;  
  56.   
  57.     for (i = 0; i< NUMBER; i++)  
  58.     {  
  59.         pt1.x=cvRandInt(&rng) % width3 - width;  
  60.         pt1.y=cvRandInt(&rng) % height3 - height;  
  61.         pt2.x=cvRandInt(&rng) % width3 - width;  
  62.         pt2.y=cvRandInt(&rng) % height3 - height;  
  63.   
  64.         cvLine( image, pt1, pt2, random_color(&rng), cvRandInt(&rng)%10, line_type, 0 );  
  65.         cvShowImage(wndname,image);  
  66.         if(cvWaitKey(DELAY) >= 0) return 0;  
  67.     }  
  68.   
  69.     for (i = 0; i< NUMBER; i++)  
  70.     {  
  71.         pt1.x=cvRandInt(&rng) % width3 - width;  
  72.         pt1.y=cvRandInt(&rng) % height3 - height;  
  73.         pt2.x=cvRandInt(&rng) % width3 - width;  
  74.         pt2.y=cvRandInt(&rng) % height3 - height;  
  75.   
  76.         cvRectangle( image,pt1, pt2, random_color(&rng), cvRandInt(&rng)%10-1, line_type, 0 );  
  77.         cvShowImage(wndname,image);  
  78.         if(cvWaitKey(DELAY) >= 0) return 0;  
  79.     }  
  80.   
  81.     for (i = 0; i< NUMBER; i++)  
  82.     {  
  83.         pt1.x=cvRandInt(&rng) % width3 - width;  
  84.         pt1.y=cvRandInt(&rng) % height3 - height;  
  85.         sz.width =cvRandInt(&rng)%200;  
  86.         sz.height=cvRandInt(&rng)%200;  
  87.         angle = (cvRandInt(&rng)%1000)*0.180;  
  88.   
  89.         cvEllipse( image, pt1, sz, angle, angle - 100, angle + 200,  
  90.                    random_color(&rng), cvRandInt(&rng)%10-1, line_type, 0 );  
  91.         cvShowImage(wndname,image);  
  92.         if(cvWaitKey(DELAY) >= 0) return 0;  
  93.     }  
  94.   
  95.     for (i = 0; i< NUMBER; i++)  
  96.     {  
  97.         pt[0][0].x=cvRandInt(&rng) % width3 - width;  
  98.         pt[0][0].y=cvRandInt(&rng) % height3 - height;  
  99.         pt[0][1].x=cvRandInt(&rng) % width3 - width;  
  100.         pt[0][1].y=cvRandInt(&rng) % height3 - height;  
  101.         pt[0][2].x=cvRandInt(&rng) % width3 - width;  
  102.         pt[0][2].y=cvRandInt(&rng) % height3 - height;  
  103.         pt[1][0].x=cvRandInt(&rng) % width3 - width;  
  104.         pt[1][0].y=cvRandInt(&rng) % height3 - height;  
  105.         pt[1][1].x=cvRandInt(&rng) % width3 - width;  
  106.         pt[1][1].y=cvRandInt(&rng) % height3 - height;  
  107.         pt[1][2].x=cvRandInt(&rng) % width3 - width;  
  108.         pt[1][2].y=cvRandInt(&rng) % height3 - height;  
  109.   
  110.         cvPolyLine( image, pt, arr, 2, 1, random_color(&rng), cvRandInt(&rng)%10, line_type, 0 );  
  111.         cvShowImage(wndname,image);  
  112.         if(cvWaitKey(DELAY) >= 0) return 0;  
  113.     }  
  114.   
  115.     for (i = 0; i< NUMBER; i++)  
  116.     {  
  117.         pt[0][0].x=cvRandInt(&rng) % width3 - width;  
  118.         pt[0][0].y=cvRandInt(&rng) % height3 - height;  
  119.         pt[0][1].x=cvRandInt(&rng) % width3 - width;  
  120.         pt[0][1].y=cvRandInt(&rng) % height3 - height;  
  121.         pt[0][2].x=cvRandInt(&rng) % width3 - width;  
  122.         pt[0][2].y=cvRandInt(&rng) % height3 - height;  
  123.         pt[1][0].x=cvRandInt(&rng) % width3 - width;  
  124.         pt[1][0].y=cvRandInt(&rng) % height3 - height;  
  125.         pt[1][1].x=cvRandInt(&rng) % width3 - width;  
  126.         pt[1][1].y=cvRandInt(&rng) % height3 - height;  
  127.         pt[1][2].x=cvRandInt(&rng) % width3 - width;  
  128.         pt[1][2].y=cvRandInt(&rng) % height3 - height;  
  129.   
  130.         cvFillPoly( image, pt, arr, 2, random_color(&rng), line_type, 0 );  
  131.         cvShowImage(wndname,image);  
  132.         if(cvWaitKey(DELAY) >= 0) return 0;  
  133.     }  
  134.   
  135.     for (i = 0; i< NUMBER; i++)  
  136.     {  
  137.         pt1.x=cvRandInt(&rng) % width3 - width;  
  138.         pt1.y=cvRandInt(&rng) % height3 - height;  
  139.   
  140.         cvCircle( image, pt1, cvRandInt(&rng)%300, random_color(&rng),  
  141.                   cvRandInt(&rng)%10-1, line_type, 0 );  
  142.         cvShowImage(wndname,image);  
  143.         if(cvWaitKey(DELAY) >= 0) return 0;  
  144.     }  
  145.   
  146.     for (i = 1; i< NUMBER; i++)  
  147.     {  
  148.         pt1.x=cvRandInt(&rng) % width3 - width;  
  149.         pt1.y=cvRandInt(&rng) % height3 - height;  
  150.   
  151.         cvInitFont( &font, cvRandInt(&rng) % 8,  
  152.                     (cvRandInt(&rng)%100)*0.05+0.1, (cvRandInt(&rng)%100)*0.05+0.1,  
  153.                     (cvRandInt(&rng)%5)*0.1, cvRound(cvRandInt(&rng)%10), line_type );  
  154.   
  155.         cvPutText( image, "Testing text rendering!", pt1, &font, random_color(&rng));  
  156.         cvShowImage(wndname,image);  
  157.         if(cvWaitKey(DELAY) >= 0) return 0;  
  158.     }  
  159.   
  160.     cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX, 3, 3, 0.0, 5, line_type );  
  161.   
  162.     cvGetTextSize( "OpenCV forever!", &font, &text_size, &ymin );  
  163.   
  164.     pt1.x = (width - text_size.width)/2;  
  165.     pt1.y = (height + text_size.height)/2;  
  166.     image2 = cvCloneImage(image);  
  167.   
  168.     for( i = 0; i < 255; i++ )  
  169.     {  
  170.         cvSubS( image2, cvScalarAll(i), image, 0 );  
  171.         cvPutText( image, "OpenCV forever!", pt1, &font, CV_RGB(255,i,i));  
  172.         cvShowImage(wndname,image);  
  173.         if(cvWaitKey(DELAY) >= 0) return 0;  
  174.     }  
  175.   
  176.     // Wait for a key stroke; the same function arranges events processing  
  177.     cvWaitKey(0);  
  178.     cvReleaseImage(&image);  
  179.     cvReleaseImage(&image2);  
  180.     cvDestroyWindow(wndname);  
  181.   
  182.     return 0;  
  183. }  
  184.   
  185. #ifdef _EiC  
  186. main(1,"drawing.c");  
  187. #endif  

$ g++ `pkg-config opencv --libs --cflags opencv` drawing.c -o drawing
$ ./drawing

OK!!


转载地址: http://blog.csdn.net/firefoxbug/article/details/7540378

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值