OpenCV

From ImageWiki

(Difference between revisions)
Jump to: navigation, search
(Strukturen af OpenCV)
 
(9 intermediate revisions not shown)
Line 1: Line 1:
-
Denne side giver en bl?d introduktion til 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]. Projektet har desuden en god [http://opencvlibrary.sourceforge.net/ wiki].
+
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, s denne side giver kun et par simple eksempler p anvendelse. Et problem ved OpenCV's dokumentation er dog at der er s meget af den, hvilket kan g?re det hele noget uoverskueligt. Man skal derfor vide hvad man leder efter -- men bare rolig, det l?rer man med tiden.
+
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 består af tre dele
+
OpenCV consists of three core parts
-
* '''cxcore''' stiller diverse basale strukturer til rådighed. Dette inkluderer strukturer til billeder, matricer, punkter, o.lign. Desuden indeholder '''cxcore''' basale grafiske funktioner, såsom tegning af linier, 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''' indeholder funktioner til billedbehandling, Computer Vision og mønstergenkendelse. Det vil sige funktioner til kantdetektion, kamerakalibrering, beregning af motion flow, klyngeanalyse og så videre.
+
* '''cv''' contains functions for image processing and computer vision. That is, functions for edge detection, camera calibration, optical flow, clustering and so forth.
-
* '''highgui''' indeholder funktioner til opbygning af simple grafiske brugergrænseflader og til indlæsning af billeder. Dvs. at '''highgui''' primært bruges til at hente billeder (fra kamera og filer) og derefter vise dem.
+
* '''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 p Linux? ==
+
== 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 header-fil til r?dighed der blot skal inkluderes i dit C/C  program. Dvs. f?lgende tre header-filer er interessante: "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".
-
For at overs?tte programmer der benytter OpenCV, benyttes programmet ''pkg-config''. Dette program fort?ller blot hvilke argumenter der skal gives til overs?tteren for at OpenCV kan benyttes. 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
-
udskriver de argumenter der skal gives til overs?tteren for at den kan finde 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
-
udskriver de argumenter der skal gives til overs?tteren for at den kan linke til OpenCV. Dette g?r det yderst nemt at skrive en passende Makefile. Her gives et mindre eksempel, som du selv m tilpasse til dit eget projekt.
+
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 f?rste eksempel vi ser p tager billeder fra et kamera og viser dem i et vindue. Det er formentlig det mest simple og alligevel nyttige program der kan skrives i OpenCV. Programmet kan findes i Robot Eksperimentariums [[Robot_Eksperimentarium_2006#The_SVN_Repository|SVN]] under ''Examples/opencv/Eksempel1'' og best?r af filen ''eksempel1.cc'' og en tilh?rende ''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 60: Line 62:
     // Clean up
     // Clean up
     cvDestroyAllWindows();
     cvDestroyAllWindows();
-
     cvReleaseCapture(
+
     cvReleaseCapture(&cam);
 +
 +
    return 0;
 +
}

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