Opencv学习1: 在Linux下编译运行第一个opencv程序

前一段在安装Caffe时候把Opencv2.4.11版本安装在redhat6.4系统上,为了测试在Linux下如何编译运行opencv代码,特意查询了网上相关资料(主要参考三个网页)。本文主要给出三种编译运行的方式:命令行的方式;Makefile(两种)方式;Cmake工具方式。

本文测试的源程序比较简单,怎么显示lena.jpg图像的代码testShowLena.cpp。

#include
#include
using namespace std;
using namespace cv;

int main(){

    Mat srcImage = imread("lena.jpg");

    imshow("raw image", srcImage);

    waitKey(0);

    return 0;

}
  1. 命令行方式编译运行
    在终端中输入:
    g++ testShowLena.cpp -o testShowLena pkg-config --cflags --libs opencv
    或者
    icpc testShowLena.cpp -o testShowLena pkg-config --cflags --libs opencv
    icpc是Intel的C++编译器
    使用pkg-config时,选项–cflags 它是用来指定程序在编译时所需要头文件所在的目录,选项 –libs则是指定程序在链接时所需要的动态链接库的目录。
    产生testShowLena可执行文件,然后运行./testShowLena 即可显示图像

    Opencv学习1: 在Linux下编译运行第一个opencv程序_第1张图片

  2. Makefile 文件编译
    2.1 采用pkg-config编写的makefile

#================================================================================
#
# Makefile1 for testing OPENCV 
#
# Email: wjzln57@gmail.com
#
#================================================================================
#CC = g++
CC = icpc

CFLAGS = -Wall -Wconversion -O3

IFLAGS = `pkg-config --cflags opencv`
LIBS   = `pkg-config --libs   opencv`

#================================================================================

SOURCE_FILE:=testShowLena.cpp

# multifile 
# SOURCE_FILE:=testShowLena.cpp \
    # test2.cpp \
    # test3.cpp

TARGET = testShowLena

OBJS = ${SOURCE_FILE:%.cpp=%.o}

all: $(TARGET)

$(TARGET):${OBJS}
    $(CC) $(IFLAGS) -o $(TARGET) $(OBJS) $(LIBS)

.SUFFIXES:
.SUFFIXES:.cpp .o

.cpp.o:
    $(CC) $(CFLAGS) -o $@ -c $< $(IFLAGS)

clean:
    rm -f *.o *~ $(TARGET)

#================================================================================
2.2 没有采用pkg-config编写的makefile
#================================================================================
#
# Makefile2 for testing OPENCV 
#
# Email: [email protected]
#
#================================================================================

CC = g++
# CC = icpc

CFLAGS = -Wall -Wconversion -O3

IFLAGS = -I.
IFLAGS += -I/usr/local/include/opencv -I/usr/local/include/opencv2
IFLAGS += -I/usr/local/include/opencv2/calib3d
IFLAGS += -I/usr/local/include/opencv2/contrib
IFLAGS += -I/usr/local/include/opencv2/core
IFLAGS += -I/usr/local/include/opencv2/features2d
IFLAGS += -I/usr/local/include/opencv2/flann
IFLAGS += -I/usr/local/include/opencv2/gpu
IFLAGS += -I/usr/local/include/opencv2/highgui
IFLAGS += -I/usr/local/include/opencv2/imgproc
IFLAGS += -I/usr/local/include/opencv2/legacy
IFLAGS += -I/usr/local/include/opencv2/ml
IFLAGS += -I/usr/local/include/opencv2/nonfree
IFLAGS += -I/usr/local/include/opencv2/objdetect
IFLAGS += -I/usr/local/include/opencv2/ocl
IFLAGS += -I/usr/local/include/opencv2/photo
IFLAGS += -I/usr/local/include/opencv2/stitching
IFLAGS += -I/usr/local/include/opencv2/superres
IFLAGS += -I/usr/local/include/opencv2/ts
IFLAGS += -I/usr/local/include/opencv2/video
IFLAGS += -I/usr/local/include/opencv2/videostab
IFLAGS += -g

LIBS = 
LIBS += -L/usr/local/lib/ -lopencv_core -lopencv_imgproc \
        -lopencv_flann -lopencv_highgui -lopencv_features2d \
        -lopencv_calib3d -lopencv_contrib -lopencv_gpu -lopencv_legacy \
        -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_photo \
        -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video \
        -lopencv_videostab

#================================================================================

SOURCE_FILE:=testShowLena.cpp

# multifile 
# SOURCE_FILE:=testShowLena.cpp \
    # test2.cpp \
    # test3.cpp
TARGET = testShowLena

OBJS = ${SOURCE_FILE:%.cpp=%.o}

all: $(TARGET)

$(TARGET):${OBJS}
    $(CC) $(IFLAGS) -o $(TARGET) $(OBJS) $(LIBS)

.SUFFIXES:
.SUFFIXES:.cpp .o

.cpp.o:
    $(CC) $(CFLAGS) -o $@ -c $< $(IFLAGS)

clean:
    rm -f *.o *~ $(TARGET)

#================================================================================
3. 采用Cmake工具编译

把上述两种方式生成的Makefile换成其他名字,因为Cmake编译时会自动推导生成Makefile文件名,防止覆盖。
3.1 在testShowLena.cpp和lena.jpg文件夹下新建一个Cmake工具编译时所需要的文件CMakeList.txt。
3.2 编辑CMakeList.txt
PROJECT(testShowLena) 

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)

FIND_PACKAGE( OpenCV REQUIRED )

ADD_EXECUTABLE(testShowLena  testShowLena.cpp) 

TARGET_LINK_LIBRARIES(testShowLena ${OpenCV_LIBS})
第一行是建立一个工程项目名字;
第二行是设置Cmake工具版本的最低要求;
第三行是用来查找opencv包的;
第四行是用工程项目名和要编译的文件名;
第五行是链接到OpenCV库的环节。

3.3 运行cmake .
-- Configuring done
-- Generating done
-- Build files have been written to :/home/users/wangjie/OPENCV/test1
同时生成Makefile文件。
3.4 运行make 生成可执行文件 testShowLena
3.5 运行 ./testShowLena即可

[1] http://blog.csdn.net/jk198310/article/details/51344601
[2] http://blog.csdn.net/keith_bb/article/details/52864851
[3] http://blog.csdn.net/pbypby1987/article/details/18969441

你可能感兴趣的:(opencv学习笔记,opencv-图像处理,makefile)