在Windows 7上开始使用OpenCV 2.4和MinGW [英] Getting started with OpenCV 2.4 and MinGW on Windows 7
问题描述
如何安装OpenCV 2.4并使用MinGW编译我的代码?
1。安装OpenCV 2.4.3
首先,获取,双击开始安装。只需按照向导并选择要安装的目录,例如 C:\ MinGW
。
选择要安装的C编译器和C ++编译器。
安装程序将从互联网下载一些软件包,因此您必须等待一段时间。安装完成后,使用上述步骤将 C:\MinGW\bin
添加到系统路径。
要测试您的MinGW安装是否成功,请打开命令行框并输入: gcc
。如果一切正常,它将显示此消息:
gcc:致命错误:没有输入文件
编译终止
这样就完成了MinGW的安装,现在是写Hello,World!程序。
3。编写示例代码
打开文本编辑器并键入以下代码并将该文件保存到 loadimg.cpp
。
#includeopencv2 / highgui / highgui.hpp
#include< iostream>
using namespace cv;
using namespace std;
int main(int argc,char ** argv)
{
Mat im = imread(argc == 2?argv [1]:lena.jpg,1 );
if(im.empty())
{
cout<< 无法打开图像! << endl;
return -1;
}
imshow(image,im);
waitKey(0);
return 0;
}
放置 lena.jpg
或您喜欢的任何图像在同一目录与上面的文件。打开命令行框并通过键入以下内容编译上面的代码:
g ++ -IC:\opencv\build \include-LC:\opencv\build\x86 \mingw\libloadimg.cpp -lopencv_core243 -lopencv_highgui243 -o loadimg
如果编译成功,它将创建一个名为 loadimg.exe
的可执行文件。
类型:
loadimg
执行程序。结果:
4。
现在您的OpenCV环境已准备就绪,接下来该做什么?
- 转到样品dir→
C:\opencv\samples\cpp
。 - 阅读并编译一些代码。 / li>
- 编写您自己的代码。
How do I install OpenCV 2.4 and compile my code with MinGW?
1. Installing OpenCV 2.4.3
First, get OpenCV 2.4.3 from sourceforge.net. Its a self-file-extracting so just double click the file to start installation. Install it in a directory, say C:\
.
Wait until all files get extracted. It will create a new
directory C:\opencv
which contains OpenCV header files, libraries, code samples, etc.
Now you need to add C:\opencv\build\x86\mingw\bin
directory to your system PATH. This directory contains OpenCV DLLs which is required for running your code.
Open Control Panel → System → Advanced system settings → Advanced Tab → Environment variables...
You will see a window like shown below:
On the System Variables section,
select Path (1), click Edit... (2), add C:\opencv\build\x86\mingw\bin
(3) then click Ok.
This will completes the OpenCV 2.4.3 installation on your computer.
2. Installing MinGW compiler suite
I highly recommend you to use gcc (GNU Compiler Collection) for compiling your code. gcc is the compiler suite widely available in Linux systems and MinGW is the native port for Windows.
Download the MinGW installer from Sourceforge.net and double click to start installation. Just follow the wizard and select the directory to be installed, say C:\MinGW
.
Select "C Compiler" and "C++ Compiler" to be installed.
The installer will download some packages from the internet so you have to wait for a while. After the installation finished, add C:\MinGW\bin
to your system path using the steps described before.
To test if your MinGW installation is success, open a command-line box and type: gcc
. If everything is ok, it will display this message:
gcc: fatal error: no input files
compilation terminated
This completes the MinGW installation, now is the time to write your "Hello, World!" program.
3. Write a sample code
Open your text editor and type the code below and save the file to loadimg.cpp
.
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat im = imread(argc == 2 ? argv[1] : "lena.jpg", 1);
if (im.empty())
{
cout << "Cannot open image!" << endl;
return -1;
}
imshow("image", im);
waitKey(0);
return 0;
}
Put lena.jpg
or any image you like in the same directory with the file above. Open a command-line box and compile the code above by typing:
g++ -I"C:\opencv\build\include" -L"C:\opencv\build\x86\mingw\lib" loadimg.cpp -lopencv_core243 -lopencv_highgui243 -o loadimg
If it compiles successfully, it will create an executable named loadimg.exe
.
Type:
loadimg
To execute the program. Result:
4. Where to go from here?
Now that your OpenCV environment is ready, what's next?
- Go to the samples dir →
C:\opencv\samples\cpp
. - Read and compile some code.
- Write your own code.
这篇关于在Windows 7上开始使用OpenCV 2.4和MinGW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!