OpenCV 概述



OpenCV概述

  • 什麼是OpenCV 
    • 開源C/C++計算機視覺庫.
    • 面向實時應用進行優化.
    • 跨操作系統/硬件/窗口管理器.
    • 通用圖像/視頻載入、存儲和獲取.
    • 由中、高層API構成.
    • Intel®公司的 Integrated Performance Primitives (IPP) 提供了透明接口.
  • 特性:
    • 圖像數據操作 (分配,釋放, 複製, 設定, 轉換).
    • 圖像與視頻 I/O (基於文件/攝像頭輸入, 圖像/視頻文件輸出).
    • 矩陣與向量操作與線性代數計算(相乘, 求解, 特徵值, 奇異值分解SVD).
    • 各種動態數據結構(列表, 隊列, , , ).
    • 基本圖像處理(濾波, 邊緣檢測, 角點檢測, 採樣與插值, 色彩轉換, 形態操作, 直方圖, 圖像金字塔).
    • 結構分析(連接成分, 輪廓處理, 距離轉換, 模板匹配, Hough轉換, 多邊形近似, 線性擬合, 橢圓擬合, Delaunay三角化).
    • 攝像頭標定 (尋找並跟蹤標定模板, 標定, 基礎矩陣估計, homography估計, 立體匹配).
    • 動作分析(光流, 動作分割, 跟蹤).
    • 對象辨識 (特徵方法, 隱馬可夫鏈模型HMM).
    • 基本GUI(顯示圖像/視頻鍵盤鼠標操作, 滾動條).
    • 圖像標識 (直線, 圓錐, 多邊形, 文本繪圖)
  • OpenCV 模塊:
    • cv - OpenCV 主要函數.
    • cvaux 輔助 (實驗性) OpenCV 函數.
    • cxcore - 數據結構與線性代數算法.
    • highgui - GUI函數.

資料鏈接

  • 參考手冊:
    • <opencv-root>/docs/index.htm
  • 網絡資源:
    • 官方網頁: http://www.intel.com/technology/computing/opencv/
    • 軟件下載: http://sourceforge.net/projects/opencvlibrary/
  • 書籍:
    • Open Source Computer Vision Library by Gary R. Bradski, Vadim Pisarevsky, and Jean-Yves Bouguet, Springer, 1st ed. (June, 2006).
  • 視頻處理例程 (位於 <opencv-root>/samples/c/目錄中):
    • 色彩跟蹤: camshiftdemo
    • 點跟蹤: lkdemo
    • 動作分割: motempl
    • 邊緣檢測: laplace
  • 圖像處理例程(位於<opencv-root>/samples/c/目錄中):
    • 邊緣檢測: edge
    • 分割: pyramid_segmentation
    • 形態: morphology
    • 直方圖: demhist
    • 距離轉換: distrans
    • 橢圓擬合 fitellipse

OpenCV 命名約定

  • 函數命名:

    cvActionTarget[Mod](...)

    Action = 
核心功能(例如 設定set, 創建create)
    Target = 
操作目標 (例如 輪廓contour, 多邊形polygon)
    [Mod]  = 
可選修飾詞 (例如說明參數類型)

  • 矩陣數據類型:

    CV_<bit_depth>(S|U|F)C<number_of_channels>

    S = 
帶符號整數
    U = 
無符號整數
    F = 
浮點數 

    
:   CV_8UC1 表示一個8位無符號單通道矩陣, 
          CV_32FC2 
表示一個32位浮點雙通道矩陣.

  • 圖像數據類型:

    IPL_DEPTH_<bit_depth>(S|U|F)

    
:   IPL_DEPTH_8U 表示一個8位無符號圖像.
          IPL_DEPTH_32F 
表示一個32位浮點數圖像.

  • 頭文件:

    #include <cv.h>
    #include <cvaux.h>
    #include <highgui.h>  
    #include <cxcore.h>   // 
不必要 - 該頭文件已在 cv.h 文件中包含

編譯命令

  • Linux系統:

g++ hello-world.cpp -o hello-world /
    -I /usr/local/include/opencv -L /usr/local/lib  /
    -lm -lcv -lhighgui -lcvaux

  • Windows系統:

注意在項目屬性中設好OpenCV頭文件以及庫文件的路徑.

C程序實例

////////////////////////////////////////////////////////////////////////
//
// hello-world.cpp
//
// 
一個簡單的OpenCV程序
// 
它從一個文件中讀取圖像,將色彩值顛倒,並顯示結果. 
//
////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>


int main(int argc, char *argv[])
{
  IplImage* img = 0; 
  int height,width,step,channels;
  uchar *data;
  int i,j,k;

  if(argc<2){
    printf("Usage: main <image-file-name>/n/7");
    exit(0);
  }

  // 
載入圖像  
  img=cvLoadImage(argv[1]);
  if(!img){
    printf("Could not load image file: %s/n",argv[1]);
    exit(0);
  }

  // 
獲取圖像數據
  height    = img->height;
  width     = img->width;
  step      = img->widthStep;
  channels  = img->nChannels;
  data      = (uchar *)img->imageData;
  printf("Processing a %dx%d image with %d channels/n",height,width,channels); 

  // 
創建窗口
  cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("mainWin", 100, 100);

  // 
反色圖像
  for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)
    data[i*step+j*channels+k]=255-data[i*step+j*channels+k];

  // 
顯示圖像
  cvShowImage("mainWin", img );

  // wait for a key
  cvWaitKey(0);

  // release the image
  cvReleaseImage(&img );
  return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章