OpenCV

From ImageWiki

(Difference between revisions)
Jump to: navigation, search
(Eksempel 1)
 
(26 intermediate revisions not shown)
Line 1: Line 1:
-
OpenCV, som er et kraftigt bibliotek til billedbehandling og Computer Vision udviklet i C. OpenCV's hjemmeside kan ses [http://www.intel.com/technology/computing/opencv/index.htm her].
+
This page contains a very soft and gentle introduction to OpenCV version 1.0.0. The OpenCV library is a powerful Image Processing and Computer Vision library written in C. The OpenCV homepage is [http://www.intel.com/technology/computing/opencv/index.htm here], and the project had a nice [http://opencvlibrary.sourceforge.net/ wiki] as well.
-
Generelt er OpenCV yderst veldokumenteret, simple . Et problem ved OpenCV, . Man skal derfor vide hvad man leder efter -- men bare rolig, .
+
In general, OpenCV is quite well-documented, so this page only provides some very simple examples to get you started. Due to the size of the OpenCV library, the documentation can be somewhat convoluted so you have to know what you are looking for. But don't worry, you'll quickly pick that up.
-
== Strukturen af OpenCV ==
+
== The OpenCV Structure ==
-
OpenCV
+
OpenCV consists of three core parts
-
* '''cxcore''' . Dette inkluderer strukturer til billeder, matricer, punkter, o.lign. Desuden indeholder '''cxcore''' basale grafiske funktioner, , cirkler, firkanter, osv.
+
* '''cxcore''' provides a set of basic data structures. This includes 'images', 'matrices', 'points', and so forth. Besides this, '''cxcore''' also contains basic drawing functionality that allows you to draw lines, circles, etc, in images.
-
* '''cv''' . Det vil sige funktioner til kantdetektion, kamerakalibrering, beregning af motion flow, .
+
* '''cv''' contains functions for image processing and computer vision. That is, functions for edge detection, camera calibration, optical flow, clustering and so forth.
-
* '''highgui''' . .
+
* '''highgui''' contains functions for building graphical user interfaces and for reading and writing images. That is, it contains functions that allows you to show images, and acquite them from either the disc or from a camera.
-
== Hvordan benyttes OpenCV? ==
+
== How to use OpenCV on Linux ==
-
En af OpenCV's styrker er at det er let at benytte. Hver af de tre dele af OpenCV stiller en headerC/C++ program. : "cxcore.h", "cv.h" og "highgui.h".
+
One of the major strengths of OpenCV is its ease of use. Each of the three core parts of OpenCV has one header file that you should include in your C program. These are called: "cxcore.h", "cv.h" and "highgui.h".
-
OpenCV, benyttes programmet ''pkg-config''. . Kommandoen
+
To compile OpenCV programs you can use the ''pkg-config'' program. This simple provides you with the necesary compiler and linker flags. The command
  pkg-config --cflags opencv
  pkg-config --cflags opencv
-
, mens kommandoen
+
prints the list of arguments needed to allow the compiler to find the header files, while
  pkg-config --libs opencv
  pkg-config --libs opencv
-
. Makefile. .
+
prints the commands needed for linking. This makes it very easy to write Makefile's for your project. The following simple example shows how (you'll need to adapt it to your own programs).
  # Compile and link flags
  # Compile and link flags
-
  CXX     =  g++
+
  CXX         =  g++
-
  CXXFLAGS = `pkg-config opencv --cflags` -Wall
+
  CFLAGS      = -Wall -g
-
LIBS    = `pkg-config opencv --libs`
+
   
   
-
  # Name of executable
+
  # Compilation (add flags as needed)
-
  EXECUTABLE = myprogram
+
  CXXFLAGS    += `pkg-config opencv --cflags`
   
   
-
  # Files
+
  # Linking (add flags as needed)
-
  SOURCES = myprogram.cc myauxilaryfunction.cc
+
  LDFLAGS    += `pkg-config opencv --libs`
-
OBJECTS = $(patsubst %.cc, %.o, $(SOURCES))
+
   
   
-
  # Linking
+
  # Name your target executables here
-
  all: $(OBJECTS)
+
  all         = test
-
$(CXX) $(LIBS) $(OBJECTS) -o $(EXECUTABLE)
+
   
   
-
  # Compilation
+
  # Default target is the first one - so we will have it make everything :-)
-
  %.o : %.cc
+
  all: $(all)
-
$(CXX) $(CXXFLAGS) -c $^
+
 +
clean:
 +
    rm -f $(all) *.o
 +
 +
# Program dependencies (.o files will be compiles by implicit rules)
 +
test: test.o MyOtherClass.o
-
== Eksempel 1 ==
+
== Example 1 ==
-
. Det er formentlig det mest simple og alligevel nyttige program der kan skrives i OpenCV. Programmet kan findes i billedgruppens SVN ''eksempel1.cc'' ''Makefile''.
+
In the first example we'll grab images from a camera and show it on the screen. This is a very simple, yet quite usable, OpenCV program. The program can be found the Robot Eksperimentariums [[Robot_Eksperimentarium_2007#The_SVN_Repository|SVN]] under ''Examples/opencv/Eksempel1'' and consists of the file ''eksempel1.cc'' and a corrsponding ''Makefile''.
  #include "highgui.h"
  #include "highgui.h"
Line 64: Line 66:
     return 0;
     return 0;
  }
  }
-
 
-
 
-
# Compile and link flags
 
-
CXX      =  g++
 
-
CXXFLAGS = `pkg-config opencv --cflags` -Wall
 
-
LIBS    = `pkg-config opencv --libs`
 
-
 
-
# Name of executable
 
-
EXECUTABLE = eksempel1
 
-
 
-
# Files
 
-
SOURCES = eksempel1.cc
 
-
OBJECTS = $(patsubst %.cc, %.o, $(SOURCES))
 
-
 
-
# Linking
 
-
all: $(OBJECTS)
 
-
$(CXX) $(LIBS) $(OBJECTS) -o $(EXECUTABLE)
 
-
 
-
# Compilation
 
-
%.o : %.cc
 
-
$(CXX) $(CXXFLAGS) -c $^
 
-
 
-
 
-
 
-
== Eksempel 2 ==
 
-
Tag billeder, find kanter og vis dem.
 
-
 
-
== Tips og tricks ==
 
-
 
-
 
-
=== Tilgang til pixels ===
 
-
 
-
#define GRAY(IMAGE, X, Y) \
 
-
    ((uchar*)((IMAGE)->imageData  + (IMAGE)->widthStep*(Y)))[X]
 
-
#define RED(IMAGE, X, Y) \
 
-
    ((uchar*)((IMAGE)->imageData + (IMAGE)->widthStep*(Y)))[(X)*3+2]
 
-
#define GREEN(IMAGE, X, Y) \
 
-
    ((uchar*)((IMAGE)->imageData + (IMAGE)->widthStep*(Y)))[(X)*3+1]
 
-
#define BLUE(IMAGE, X, Y) \
 
-
    ((uchar*)((IMAGE)->imageData + (IMAGE)->widthStep*(Y)))[(X)*3]
 
-
 
-
 
-
int tmp = GRAY(I, x, y);
 
-
GRAY(I, x, y) = tmp + 1;
 
-
 
-
 
-
int red = RED(RGB, x, y);
 
-
BLUE(RGB, x, y) = red;
 
-
 
-
 
-
== Propaganda ==
 
-
 

Latest revision as of 18:56, 25 August 2015

This page contains a very soft and gentle introduction to OpenCV version 1.0.0. The OpenCV library is a powerful Image Processing and Computer Vision library written in C. The OpenCV homepage is here, and the project had a nice wiki as well.

In general, OpenCV is quite well-documented, so this page only provides some very simple examples to get you started. Due to the size of the OpenCV library, the documentation can be somewhat convoluted so you have to know what you are looking for. But don't worry, you'll quickly pick that up.

The OpenCV Structure

OpenCV consists of three core parts

  • cxcore provides a set of basic data structures. This includes 'images', 'matrices', 'points', and so forth. Besides this, cxcore also contains basic drawing functionality that allows you to draw lines, circles, etc, in images.
  • cv contains functions for image processing and computer vision. That is, functions for edge detection, camera calibration, optical flow, clustering and so forth.
  • highgui contains functions for building graphical user interfaces and for reading and writing images. That is, it contains functions that allows you to show images, and acquite them from either the disc or from a camera.

How to use OpenCV on Linux

One of the major strengths of OpenCV is its ease of use. Each of the three core parts of OpenCV has one header file that you should include in your C program. These are called: "cxcore.h", "cv.h" and "highgui.h".

To compile OpenCV programs you can use the pkg-config program. This simple provides you with the necesary compiler and linker flags. The command

pkg-config --cflags opencv

prints the list of arguments needed to allow the compiler to find the header files, while

pkg-config --libs opencv

prints the commands needed for linking. This makes it very easy to write Makefile's for your project. The following simple example shows how (you'll need to adapt it to your own programs).

# Compile and link flags
CXX          =  g++
CFLAGS       = -Wall -g

# Compilation (add flags as needed)
CXXFLAGS    += `pkg-config opencv --cflags`

# Linking (add flags as needed)
LDFLAGS     += `pkg-config opencv --libs`

# Name your target executables here
all         = test

# Default target is the first one - so we will have it make everything :-)
all: $(all)

clean:
    rm -f $(all) *.o

# Program dependencies (.o files will be compiles by implicit rules)
test: test.o MyOtherClass.o

Example 1

In the first example we'll grab images from a camera and show it on the screen. This is a very simple, yet quite usable, OpenCV program. The program can be found the Robot Eksperimentariums SVN under Examples/opencv/Eksempel1 and consists of the file eksempel1.cc and a corrsponding Makefile.

#include "highgui.h"

int main() 
{
    // Get an OpenCV camera handle
    CvCapture *cam = cvCaptureFromCAM(-1);

    // Initialise the GUI
    const char *window = "Eksempel 1";
    cvNamedWindow(window, CV_WINDOW_AUTOSIZE);

    // The main loop
    while (cvWaitKey(4) == -1) {
        IplImage *im = cvQueryFrame(cam);
        cvShowImage(window, im);
    }

    // Clean up
    cvDestroyAllWindows();
    cvReleaseCapture(&cam);

    return 0;
}
Personal tools