OpenCV学习——各个环境的安装总结(汇总)

本文详细介绍了如何在Windows环境下安装并配置OpenCV,包括使用MS Visual C++和MinGW进行配置的方法。此外,还提供了Linux和Mac OS X系统的安装步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Windows

MS Visual C++

Install OpenCV

Download OpenCV installer and install it. I assume you have installed onto C:/Program Files/OpenCV.

Setup MS Visual C++

I write details based on MS Visual Studio 2005. Follow menus of MS Visual C++ window as:

  • Tools > Options > Projects and Solutions > VC++ directories >
  • Show Directory for: > Include Files. Now add
    C:/Program Files/OpenCV/cv/include
    
    C:/Program Files/OpenCV/cvaux/include
    
    C:/Program Files/OpenCV/cxcore/include
    
    C:/Program Files/OpenCV/otherlibs/highgui
    
    C:/Program Files/OpenCV/ml/include
    
    C:/Program Files/OpenCV/otherlibs/cvcam/include
    
    
  • Show Directory for: > Library Files. Now add
    C:/Program Files/OpenCV/lib
    
    
  • (Option) Show Directory for: > Source Files. Now add
    C:/Program Files/OpenCV/cv/src
    
    C:/Program Files/OpenCV/cvaux/src
    
    C:/Program Files/OpenCV/cxcore/src
    
    C:/Program Files/OpenCV/otherlibs/highgui
    
    C:/Program Files/OpenCV/otherlibs/cvcam/src/windows
    
    
Setup MS Visual C++ for each project
  • Right Click Project > Property >
  • Configuration Proeperties > Linker > Input > Additional Dependencies. Now add
    • cv.lib cxcore.lib cvaux.lib highgui.lib ml.lib cvcam.lib
  • Change 'Configuration' drop down menu from Release (Debug) to Debug (Release).
  • Do the same thing (Add .libs to Input)

Or, add below codes into your source codes above #include(s).

#ifdef _MSC_VER

#pragma comment(lib, "cv.lib")

#pragma comment(lib, "cxcore.lib")

#pragma comment(lib, "cvaux.lib")

#pragma comment(lib, "highgui.lib")

#pragma comment(lib, "ml.lib")

#pragma comment(lib, "cvcam.lib")

#endif

MinGW gcc/g++

Install MinGW

MinGWis a collection of freely available and freely distributable Windowsspecific header files and import libraries, augmenting the GNU CompilerCollection, (GCC), and its associated tools, (GNU binutils). MinGWprovides a complete Open Source programming tool set which is suitablefor the development of native Windows programs that do not depend onany 3rd-party C runtime DLLs.

  • Download Automated MinGW Installer from MinGW project page and install.
  • Install gcc and g77
  • Assume MinGW is installed on C:/MinGW
  • MinGW originally does not include gdb debugger. If you want it, download .tar.bz2 of GNU Source-Level Debugger from MinGW project page and unarchive it.
  • Copy files into C:/MinGW
How To Compile
g++ test.cpp -I"C:/Program Files/OpenCV/cv/include" -I"C:/Program Files/OpenCV/cxcore/include" -I"C:/Program Files/OpenCV/otherlibs/highgui" -L"C:/Program Files/OpenCV/lib" -lcxcore -lcv -lhighgui -lcvaux -lml
How to Use MinGW on cygwin

Cygwinis a Linux-like environment for Windows. It consists of two parts: ADLL (cygwin1.dll) which acts as a Linux API emulation layer providingsubstantial Linux API functionality. A collection of tools whichprovide Linux look and feel.

http://www.cygwin.com/

Setup to use MinGW g++ instead of cygwin g++

$ export PATH=/cygdrive/c/MinGW/bin:$PATH

Linux

gcc/g++

Download OpenCV source codes (.tar.gz)

Root user

tar zxvf opencv-1.0.0.tar.gz; cd opencv-1.0.0

./configure

make

make install

General user

tar zxvf opencv-1.0.0.tar.gz; cd opencv-1.0.0

./configure --prefix=$HOME/usr

make

make install

# configuration

export PATH=$HOME/usr/bin/:$PATH

export LD_LIBRARY_PATH=$HOME/usr/lib:$LD_LIBRARY_PATH

export PKG_CONFIG_PATH=$HOME/usr/lib/pkgconfig:$PKG_CONFIG_PATH

export MANPATH=$HOME/usr/man:$MANPATH

Compilation is as

g++ `pkg-config --cflags opencv` foo.c -o foo `pkg-config --libs opencv`

It must be equivalent with

g++ -I$HOME/usr/include/opencv foo.c -o foo -L$HOME/usr/lib -lcxcore -lcv -lhighgui -lcvaux -lml

where $HOME/usr is the prefix specified at the ./configure step.

MacOS X

I tried long time ago, there should exist easier ways nowadays.

  • Install Xcode from OS CD, 'custome' to install everything.
  • Get OpenCV and http://lestang.org/article45.html.
  • Copy these files into OpenCV.
  • ./configure to create cvconfig.h.
  • Double click Xcode project in _make, add path of the cvconfig.h to highgui.
  • Build, try projects of samples.
  • Toexecute builded program on command line, copy_make/build/Debug/*.framework into ~/Library/Frameworks. If Frameworksdoes not exist, create it.
    $ _make/build/Debug/kmeans.app/Contents/MacOS/kmeans

Sample Test code

#include 

#include 

#include 

#include 

#include 



int main(int argc, char *argv[])

{

 // Nothing but create a window

 cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);

 cvMoveWindow("mainWin", 100, 100);

 cvWaitKey(0);

 return 0;

}

### 开发停车场管理系统的概述 为了应对城市化进程中停车难的问题,智能化停车场管理系统成为一种有效的解决方案。该系统能够自动检测停车位状态并统计空闲车位数量,极大提高了停车场的运营效率和服务质量[^2]。 ### 图像采集 图像采集是整个系统的基础环节。通过安装在停车场各个位置的摄像头获取实时影像资料。这些摄像设备既可以采用固定的安装方式来覆盖特定区域,也可以选用具备一定灵活性的移动式装置以适应更复杂的环境需求[^3]。 ```python import cv2 def capture_image(camera_id=0): cap = cv2.VideoCapture(camera_id) while True: ret, frame = cap.read() if not ret: break yield frame cap.release() for image in capture_image(): # Process each captured image here. pass ``` ### 车位检测与计数 对于所获得的画面,需运用OpenCV提供的工具和技术对其进行分析处理。具体而言,在完成初步转换(如转为灰度图、应用高斯模糊等操作)之后,可借助形态学变换以及轮廓查找等功能定位出可能存在的停车位边界;随后再经由面积筛选等方式剔除不符合条件的对象,最终得到准确无误的结果集——即当前时刻下所有可用作停放汽车的空间数目及其确切坐标信息[^1]。 ```python import numpy as np def detect_parking_spaces(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) edged = cv2.Canny(blurred, 50, 150) dilated = cv2.dilate(edged, None, iterations=2) contours, _ = cv2.findContours(dilated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) spaces = [] for contour in contours: area = cv2.contourArea(contour) if min_space_area <= area <= max_space_area: # Define `min_space_area` and `max_space_area`. rect = cv2.boundingRect(contour) spaces.append(rect) return spaces ``` ### 车牌识别 当有车辆进入或离开某个指定地点时,则启动专门设计用来辨识车牌号段的功能模块。此过程涉及多个子任务:首先是针对目标对象实施局部放大裁剪动作以便更好地聚焦于待测部位;接着执行诸如直方图均衡化之类的增强措施提升对比度清晰度;最后依靠训练好的模型预测文字内容并将之转化为字符串形式保存起来供后续查询调用所需。 ```python from imutils import paths import pytesseract def recognize_license_plate(roi): roi_gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) roi_blur = cv2.medianBlur(roi_gray, 3) roi_thresh = cv2.threshold(roi_blur, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] config = "-c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 --psm 8" text = pytesseract.image_to_string(roi_thresh, config=config).strip().upper() return text ``` ### 数据库管理 考虑到长期稳定运行的要求,有必要建立一套完善的数据库体系结构用于持久化存储各类重要参数值。这不仅限于记录每辆车的具体出入场次时间戳,还包括关联其对应的牌照编号以及其他备注说明项等等。此外还应考虑提供便捷的数据访问接口给前端展示层或其他业务逻辑单元使用。 ```sql CREATE TABLE IF NOT EXISTS vehicles ( id INTEGER PRIMARY KEY AUTOINCREMENT, plate TEXT UNIQUE NOT NULL, entry_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, exit_time TIMESTAMP NULL ); ``` ### 用户界面 为了让管理人员能更加直观高效地掌握现场情况变化趋势,建议配套开发一款易于上手的操作面板。它应当包含但不限于如下几个核心组成部分: - **监控视窗**:显示来自不同角度拍摄所得最新画面; - **统计数据报表**:呈现一段时间区间内累计进出次数分布状况图表; - **事件日志列表**:罗列近期发生的异常报警提示消息汇总表单。 可以利用Python社区里流行的GUI框架之一(比如Tkinter 或 PyQt),快速搭建起满足上述功能特性的应用程序原型版本来进行初期验证测试工作。 ```python try: from tkinter import * except ImportError: from Tkinter import * root = Tk() label = Label(root, text="Parking Management System") label.pack() # Add more widgets... root.mainloop() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值