文章標題

一、簡介

1、OpenCV的特點

(1) 總體描述

  • OpenCV是一個基於C/C++語言的開源圖像處理函數庫
  • 其代碼都經過優化,可用於實時處理圖像
  • 具有良好的可移植性
  • 可以進行圖像/視頻載入、保存和採集的常規操作
  • 具有低級和高級的應用程序接口(API)
  • 提供了面向Intel IPP高效多媒體函數庫的接口,可針對你使用的Intel CPU優化代碼,提高程序性能(譯註:OpenCV 2.0版的代碼已顯着優化,無需IPP來提升性能,故2.0版不再提供IPP接口)

(2) 功能

  • 圖像數據操作(內存分配與釋放,圖像複製、設定和轉換)
    Image data manipulation (allocation, release, copying, setting, conversion).
  • 圖像/視頻的輸入輸出(支持文件或攝像頭的輸入,圖像/視頻文件的輸出)
    Image and video I/O (file and camera based input, image/video file output).
  • 矩陣/向量數據操作及線性代數運算(矩陣乘積、矩陣方程求解、特徵值、奇異值分解)
    Matrix and vector manipulation and linear algebra routines (products, solvers, eigenvalues, SVD).
  • 支持多種動態數據結構(鏈表、隊列、數據集、樹、圖)
    Various dynamic data structures (lists, queues, sets, trees, graphs). - 基本圖像處理(去噪、邊緣檢測、角點檢測、採樣與插值、色彩變換、形態學處理、直方圖、圖像金字塔結構)
    Basic image processing (filtering, edge detection, corner detection, sampling and interpolation, color conversion, morphological operations, histograms, image pyramids).
  • 結構分析(連通域/分支、輪廓處理、距離轉換、圖像矩、模板匹配、霍夫變換、多項式逼近、曲線擬合、橢圓擬合、狄勞尼三角化)
    Structural analysis (connected components, contour processing, distance transform, various moments, template matching, Hough transform, polygonal approximation, line fitting, ellipse fitting, Delaunay triangulation).
  • 攝像頭定標(尋找和跟蹤定標模式、參數定標、基本矩陣估計、單應矩陣估計、立體視覺匹配)
    Camera calibration (finding and tracking calibration patterns, calibration, fundamental matrix estimation, homography estimation, stereo correspondence).
  • 運動分析(光流、動作分割、目標跟蹤)
    Motion analysis (optical flow, motion segmentation, tracking). ##目標識別(特徵方法、HMM模型)
    Object recognition (eigen-methods, HMM).
  • 基本的GUI(顯示圖像/視頻、鍵盤/鼠標操作、滑動條)
    Basic GUI (display image/video, keyboard and mouse handling, scroll-bars). ##圖像標註(直線、曲線、多邊形、文本標註)
    Image labeling (line, conic, polygon, text drawing)

(3) OpenCV模塊

  • cv – 核心函數庫
  • cvaux – 輔助函數庫
  • cxcore – 數據結構與線性代數庫
  • highgui – GUI函數庫 graphical user interface
  • ml – 機器學習函數庫 machine learing

2、有用的學習資源

(1) 參考手冊:

  • < opencv-root >/docs/index.htm (譯註:在你的OpenCV安裝目錄< opencv-root >內)

(2) 網絡資源:

(3) 書籍:

  • Open Source Computer Vision Library
    by Gary R. Bradski, Vadim Pisarevsky, and Jean-Yves Bouguet, Springer, 1st ed. (June, 2006). chenyusiyuan: 補充以下書籍
  • Learning OpenCV - Computer Vision with the OpenCV Library
    by Gary Bradski & Adrian Kaehler, O’Reilly Media, 1 st ed. (September, 2008).
  • OpenCV教程——基礎篇
    作者:劉瑞禎 於仕琪,北京航空航天大學出版社,出版日期:200706

(4) 視頻處理例程(在 < opencv-root >/samples/c/):

  • 顏色跟蹤: camshiftdemo
  • 點跟蹤: lkdemo
  • 動作分割: motempl
  • 邊緣檢測: laplace

(5) 圖像處理例程 (在 < opencv-root>/samples/c/):

  • 邊緣檢測: edge
  • 圖像分割: pyramid_segmentation
  • 形態學: morphology
  • 直方圖: demhist
  • 距離變換: distrans
  • 橢圓擬合: fitellipse

3、C++例程

////////////////////////////////////////////////////////////////////////
//
// hello-world.cpp
//
// 該程序從文件中讀入一幅圖像,將之反色,然後顯示出來. 
//////////////////////////////////////////////////////////////////////////
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);
  } 

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

  // get the image data
  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); 

  // create a window
  cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("mainWin", 100, 100); 

  // invert the image
  // 相當於 cvNot(img);
  // IplImage *pDstImg = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
  // cvNot(img, pDstImg);
  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]; 

  // show the image
  cvShowImage("mainWin", img ); 

  // wait for a key
  cvWaitKey(0); 

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

二、GUI 指令

1、窗口管理

(1) 創建和定位一個新窗口:

cvNamedWindow("win1", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("win1", 100, 100); // offset from the UL corner of the screen

(2) 載入圖像:

IplImage* img=0; 
  img=cvLoadImage(fileName, CV_LOAD_IMAGE_COLOR);
  if(!img) printf("Could not load image file: %s\n",fileName);

(3) 顯示圖像:

cvShowImage("win1",img);

(4) 關閉窗口:

cvDestroyWindow("win1");

(5) 改變窗口大小:

cvResizeWindow("win1",100,100); // new width/heigh in pixels

2、輸入處理

(1) 處理鼠標事件:

  • 定義一個鼠標處理程序:

    void mouseHandler(int event, int x, int y, int flags, void* param)
      {
        switch(event){
          case CV_EVENT_LBUTTONDOWN:
            if(flags & CV_EVENT_FLAG_CTRLKEY) 
              printf("Left button down with CTRL pressed\n");
            break; 
          case CV_EVENT_LBUTTONUP:
        printf("Left button up\n");
        break;
        }
    }       
    

    x,y: 相對於左上角的像素座標
    event: CV_EVENT_LBUTTONDOWN, CV_EVENT_RBUTTONDOWN, CV_EVENT_MBUTTONDOWN,
    CV_EVENT_LBUTTONUP, CV_EVENT_RBUTTONUP, CV_EVENT_MBUTTONUP,
    CV_EVENT_LBUTTONDBLCLK, CV_EVENT_RBUTTONDBLCLK, CV_EVENT_MBUTTONDBLCLK,
    CV_EVENT_MOUSEMOVE:

    flags: CV_EVENT_FLAG_CTRLKEY, CV_EVENT_FLAG_SHIFTKEY, CV_EVENT_FLAG_ALTKEY,
    CV_EVENT_FLAG_LBUTTON, CV_EVENT_FLAG_RBUTTON, CV_EVENT_FLAG_MBUTTON

註冊該事件處理程序:
mouseParam=5;
cvSetMouseCallback(“win1”,mouseHandler,&mouseParam); //第三個參數可以設置爲NULL
(未完待續)

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