欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OPENCV 在MINGW 环境下的编译 GCCLinuxPythonC++C# 

程序员文章站 2024-03-04 18:07:36
...
因为某种原因,需要在MINGW下编译OPENCV,本以为是一个比较简单的事情,孰知竟然也陆陆续续花了不少的精力,即使到现在这个地步,也还有部分不甚满意的地方。综合看,OPENCV的的正常运行需要很多库的支持。在MS的VC++和正常的LINUX发布版本下,这些支持都是默认的,而在MINGW 下,却需要一一提供,因此也颇费一番周折,先把过程列举如下,希望能够今后做同样事情的人有所裨益。

第1步:下载 MINGW 和 MSYS

* 下载的版本是:MSYS-1.0.10.exemsysDTK-1.0.1.exe 和 MinGW-5.1.4.exe。
* 按照下载软件的基本提示,分别安装到 c:\mingw 和 c:\msys\1.0运行
* 问题:直接运行 msys.bat 出现问题,控制台窗口无法启动。所有的命令都出现 " Couldn't reserve space for cygwin's heap"
上网查找问题,解决方法是用 rebase 进行处理。 具体方法是:
- 启动CMD命令行程序,在DOS窗口下,依次运行:
cd c:\msys\1.0\bin
rebase -b 0x30000000 msys-1.0.dll
* 编辑 /etc/fstab,写入下面的映射关系:
c:/mingw /mingw
c:/mingw /usr/local
* 编辑 /etc/profile,在文件末尾增加下面几行:
#进入的缺省目录是根目录
cd "/"
#列举目录时有颜色信息
alias ls='ls --color'
alias dir='ls --color'

第2步:下载 OPENCV V1.0 FOR LINUX

* 下载OPENCV1.0 for linux 解压到/home/opencv(就是 c:\msys\1.0\home目录下)
* 从第3步开始到开始配置OPENCV,均是做一些准备工作以及配置必要的支持库。

第3步:下载pkg-config

下载地址为:http://rt.openfoundry.org/Foundry/Project/Download/。将二进制的解压包解压拷贝到 c:\mingw目录下;

第4步:安装 GTK,保证对图像I/O的支持

* 下载下面的文件,解压后释放到 C:\MinGW 目录下
* pkg-config-0.23.zip
* libiconv-1.9.1.bin.woe32.zip
* gettext-0.14.5.zip
* gettext-dev-0.14.5.zip
* pango-1.16.4.zip
* pango-dev-1.16.4.zip
* cairo-1.4.14.zip
* cairo-dev-1.4.0.zip
* zlib-1.2.3.zip
* libpng-1.2.8.zip
* libpng-dev-1.2.8.zip
* atk-1.20.0.zip
* atk-dev-1.20.0.zip
* gtk+-2.12.8.zip
* gtk+-dev-2.12.8.zip
* libintl-0.14.4-lib.zip

第5步:安装编译FFMPEG

* 下载地址是:http://download.chinaunix.net。将下载的ffmpeg-0.4.9- p20060302.tar.bz2 解压到/home/ffmpeg 下,
* 编译
./configure --enable-memalign-hack
make
make test
make install
* 注意这个包直接安装到 c:/program files/ffmpeg 中,需要把该目录下的 ./lib 和 ./include 目录移动到 c:/mingw (也就是 /usr/local)下, ffmpeg.exe 移到 c:/mingw/bin 下。

第6步:编译JPEG/JASPER/TIFF 等图像库

* 下载 jpegsrc.v6b.tar.gz,jasper-1.900.1.zip,tiff-3.8.2-3-src.tar.bz2
* 依次安装说明编译、测试、安装。

第7步:拷贝GTK所需要的运行库

iconv.dll
intl.dll
libatk-1.0-0.dll
libgdk-win32-2.0-0.dll
libgdk_pixbuf-2.0-0.dll
libgtk-win32-2.0-0.dll
libiconv2.dll
libpango-1.0-0.dll
libpangowin32-1.0-0.dll

第8步:编译 OPENCV

* 运行配置文件:
./configure --without-carbon --without-quicktime --without-1394libs --without-python

--without-swig --enable-static --disable-shared --disable-apps
* 配置参数定义是
--without-gtk:忽略gtk+2.0 windows
--without-carbon: 不使用Mac OS上的X库
--without-quicktime
--without-1394libs
--without-python
--without-swig
--enable-static :生成静态库
--disable-shared:不生成动态库
* 配置的运行结果如下

$ ./configure --without-carbon --without-quicktime --without-1394libs --witho
ut-python --without-swig --enable-static --disable-shared --disable-apps
checking build system type... i686-pc-mingw32
checking host system type... i686-pc-mingw32
checking target system type... i686-pc-mingw32
checking for a BSD-compatible install... /bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make sets $(MAKE)... (cached) yes
checking for g++... g++
checking for C++ compiler default output file name... a.exe
checking whether the C++ compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... .exe
checking for suffix of object files... o
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking for style of include used by make... GNU
checking dependency style of g++... gcc3
checking for gcc... gcc
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking dependency style of gcc... gcc3
checking how to run the C preprocessor... gcc -E
checking for a sed that does not truncate output... /bin/sed
checking for egrep... grep -E
checking for ld used by gcc... c:/mingw/mingw32/bin/ld.exe
checking if the linker (c:/mingw/mingw32/bin/ld.exe) is GNU ld... yes
checking for c:/mingw/mingw32/bin/ld.exe option to reload object files... -r
checking for BSD-compatible nm... /usr/local/bin/nm
checking whether ln -s works... yes
checking how to recognise dependent libraries... file_magic file format pei*-i3
6(.*architecture: i386)?
checking for dlltool... dlltool
checking for as... as
checking for objdump... objdump
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking dlfcn.h usability... no
checking dlfcn.h presence... no
checking for dlfcn.h... no
checking how to run the C++ preprocessor... g++ -E
checking for g77... no
checking for f77... no
checking for xlf... no
checking for frt... no
checking for pgf77... no
checking for fort77... no
checking for fl32... no
checking for af77... no
checking for f90... no
checking for xlf90... no
checking for pgf90... no
checking for epcf90... no
checking for f95... no
checking for fort... no
checking for xlf95... no
checking for ifc... no
checking for efc... no
checking for pgf95... no
checking for lf95... no
checking for gfortran... no
checking whether we are using the GNU Fortran 77 compiler... no
checking whether accepts -g... no
checking the maximum length of command line arguments... 8192
checking command to parse /usr/local/bin/nm output from gcc object... ok
checking for objdir... .libs
checking for ar... ar
checking for ranlib... ranlib
checking for strip... strip
checking if gcc supports -fno-rtti -fno-exceptions... no
checking for gcc option to produce PIC... -DDLL_EXPORT
checking if gcc PIC flag -DDLL_EXPORT works... yes
checking if gcc static flag -static works... yes
checking if gcc supports -c -o file.o... yes
checking whether the gcc linker (c:/mingw/mingw32/bin/ld.exe) supports shared l
braries... yes
checking dynamic linker characteristics... Win32 ld.exe
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... no
checking whether to build static libraries... yes
configure: creating libtool
appending configuration tag "CXX" to libtool
checking for ld used by g++... c:/mingw/mingw32/bin/ld.exe
checking if the linker (c:/mingw/mingw32/bin/ld.exe) is GNU ld... yes
checking whether the g++ linker (c:/mingw/mingw32/bin/ld.exe) supports shared l
braries... yes
checking for g++ option to produce PIC... -DDLL_EXPORT
checking if g++ PIC flag -DDLL_EXPORT works... yes
checking if g++ static flag -static works... yes
checking if g++ supports -c -o file.o... yes
checking whether the g++ linker (c:/mingw/mingw32/bin/ld.exe) supports shared l
braries... yes
checking dynamic linker characteristics... Win32 ld.exe
checking how to hardcode library paths into programs... immediate
appending configuration tag "F77" to libtool
checking whether to build debug version (no optimization)... no
configure: TARGET=i686-pc-mingw32
checking for pow in -lm... yes
checking for dlopen in -ldl... no
checking for pthread_create in -lpthread... no
checking whether byte ordering is bigendian... no
checking for working alloca.h... no
checking for alloca... yes
checking malloc.h usability... yes
checking malloc.h presence... yes
checking for malloc.h... yes
checking for lrint... yes
checking for pkg-config... /usr/local/bin/pkg-config
checking for "gtk+-2.0 gdk-pixbuf-2.0"... yes
checking GTK_CFLAGS... -mms-bitfields -Ic:/mingw/include/gtk-2.0 -Ic:/mingw/lib
gtk-2.0/include -Ic:/mingw/include/atk-1.0 -Ic:/mingw/include/cairo -Ic:/mingw/
nclude/pango-1.0 -Ic:/mingw/include/glib-2.0 -Ic:/mingw/lib/glib-2.0/include
checking GTK_LIBS... -Lc:/mingw/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -
pangowin32-1.0 -lpangocairo-1.0 -lpango-1.0 -lcairo -lgdk_pixbuf-2.0 -lgobject-
.0 -lgmodule-2.0 -lglib-2.0 -lintl
checking for "gthread-2.0"... yes
checking GTHREAD_CFLAGS... -Ic:/mingw/include/glib-2.0 -Ic:/mingw/lib/glib-2.0/
nclude
checking GTHREAD_LIBS... -Lc:/mingw/lib -lgthread-2.0 -lglib-2.0 -lintl
checking ffmpeg/avcodec.h usability... yes
checking ffmpeg/avcodec.h presence... yes
checking for ffmpeg/avcodec.h... yes
checking for avcodec_decode_video in -lavcodec... no
checking linux/videodev.h usability... no
checking linux/videodev.h presence... no
checking for linux/videodev.h... no
checking for linux/videodev2.h... no
checking jpeglib.h usability... yes
checking jpeglib.h presence... yes
checking for jpeglib.h... yes
checking for jpeg_destroy_decompress in -ljpeg... yes
checking zlib.h usability... yes
checking zlib.h presence... yes
checking for zlib.h... yes
checking for gzopen in -lz... yes
checking png.h usability... yes
checking png.h presence... yes
checking for png.h... yes
checking libpng/png.h usability... no
checking libpng/png.h presence... no
checking for libpng/png.h... no
checking for png_read_image in -lpng12... yes
checking for png_get_valid... yes
checking for png_set_tRNS_to_alpha... yes
checking tiff.h usability... yes
checking tiff.h presence... yes
checking for tiff.h... yes
checking for TIFFReadRGBAStrip in -ltiff... yes
checking jasper/jasper.h usability... yes
checking jasper/jasper.h presence... yes
checking for jasper/jasper.h... yes
checking for jas_image_readcmpt in -ljasper... yes
checking ImfCRgbaFile.h usability... no
checking ImfCRgbaFile.h presence... no
checking for ImfCRgbaFile.h... no
configure: creating ./config.status
config.status: creating Makefile
config.status: creating opencv.pc
config.status: creating opencv.spec
config.status: creating docs/Makefile
config.status: creating data/Makefile
config.status: creating cxcore/Makefile
config.status: creating cxcore/include/Makefile
config.status: creating cxcore/src/Makefile
config.status: creating cv/Makefile
config.status: creating cv/include/Makefile
config.status: creating cv/src/Makefile
config.status: creating cvaux/Makefile
config.status: creating cvaux/include/Makefile
config.status: creating cvaux/src/Makefile
config.status: creating ml/Makefile
config.status: creating ml/include/Makefile
config.status: creating ml/src/Makefile
config.status: creating otherlibs/Makefile
config.status: creating otherlibs/highgui/Makefile
config.status: creating apps/Makefile
config.status: creating apps/haartraining/Makefile
config.status: creating apps/haartraining/include/Makefile
config.status: creating apps/haartraining/src/Makefile
config.status: creating interfaces/Makefile
config.status: creating interfaces/swig/Makefile
config.status: creating interfaces/swig/filtered/Makefile
config.status: creating interfaces/swig/general/Makefile
config.status: creating interfaces/swig/python/Makefile
config.status: creating tests/Makefile
config.status: creating tests/python/Makefile
config.status: creating tests/cv/Makefile
config.status: creating tests/cv/src/Makefile
config.status: creating tests/cxts/Makefile
config.status: creating tests/cxcore/Makefile
config.status: creating tests/cxcore/src/Makefile
config.status: creating utils/Makefile
config.status: creating samples/Makefile
config.status: creating samples/c/Makefile
config.status: creating samples/python/Makefile
config.status: creating cvconfig.h
config.status: cvconfig.h is unchanged
config.status: executing depfiles commands

General configuration ================================================
Compiler: g++
CXXFLAGS: -Wall -fno-rtti -pipe -O3 -g -march=i686 -ffast-m
th -fomit-frame-pointer

Install path: /usr/local

HighGUI configuration ================================================

Windowing system --------------
Use Carbon / Mac OS X: no
Use gtk+ 2.x: yes
Use gthread: yes

Image I/O ---------------------
Use libjpeg: yes
Use zlib: yes
Use libpng: yes
Use libtiff: yes
Use libjasper: yes
Use libIlmImf: no

Video I/O ---------------------
Use QuickTime / Mac OS X: no
Use xine: no
Use ffmpeg: no
Use dc1394 & raw1394: no
Use v4l: no
Use v4l2: no

Wrappers for other languages =========================================
SWIG
Python no

Additional build settings ============================================
Build demo apps no

Now run make ...

* 编译:
make
* 安装:
make install
* 检查:可以看到能够生产 cxcore 和 cv 的测试文件,并且测试通过。
make check
* 安装
make install
将库和头文件分别安装到 /usr/local/lib 和 /usr/local/include/opencv 下

第9步:测试

* 不需要图像I/O操作的,可以自己用下列方式编译:
g++ -o sample.exe sample.c -I/usr/local/include/opencv -L/usr/local/lib -lcv -lcvaux
-lcxcore -lml -lhighgui
* 需要图像I/O操作的,用下面方式编译:
g++ -o sample.exe sample.c -I/usr/local/include/opencv
-L/usr/local/lib -lhighgui -lcv -lcxcore -lcvaux -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0
-lpangowin32-1.0 -lpangocairo-1.0 -lpango-1.0 -lcairo -lgdk_pixbuf-2.0 -lgobject-2.0 -lgmodule-2.0 -lglib-2.0 -lintl -ljpeg -lpng -ltiff -ljasper -lgthread-2.0

第10步:需要解决的几个问题

* HIGHGUI/window_gtk.cpp 不编译的问题;解决办法是重新改写 window_gtk.cpp 程序,将
#ifdef HAVE_GTK
...
#endif
有关定义GTK的预编译指令注释掉
* 解决图像I/O问题:如果图像的I/O函数无法使用,在编译时候总是出现如下错误:
c:/mingw/lib/libhighgui.a(loadsave.o): In function `Z41__static_initialization_a
nd_destruction_0ii':
c:/msys/1.0/home/opencv/otherlibs/highgui/loadsave.cpp:337: undefined reference
to `cvShowImage'
collect2: ld returned 1 exit status
* 修改 /usr/local/include/jmorecfg.h 中定义 boolean 的地方,解决 boolean 的定义冲突,注释掉 boolean 的定义
#ifndef HAVE_BOOLEAN
//typedef int boolean;
#endif


第11步:遗留的几个问题

(1)JPEG文件的读取有问题,就是说 cvLoadImage 不能正确读取 jpeg 文件;
(2)cvSaveImage 有问题,无法存图片;
(3)FFMPEG无法使用,故视频不能做处理;

第12步:编译SAMPLES/C下的样例程序

编辑 makefile.gnu
LINKFLAGS = -L"/usr/local/lib" -lhighgui -lcv -lcxcore -lcvaux -lgtk-win32-2.0

-lgdk-win32-2.0 -latk-1.0 -lpangowin32-1.0 -lpangocairo-1.0 -lpango-1.0 -lcairo

-lgdk_pixbuf-2.0 -lgobject-2.0 -lgmodule-2.0 -lglib-2.0 -lintl -ljpeg -lpng -ltiff -ljasper

-lgthread-2.0

替换原有的 LINKFLAGS 变量

输入指令:
make -f makefile.gnu
编译,然后运行