Sabtu, 23 Oktober 2010

Combining Intel OpenCV and NI Labview

1 Introduction

NI Labview is a powerful software for instrumentation and data acquisition. It also has complete library for Vision and Image processing (Vision Development Module).

Intel OpenCV is an open source C/C++ based Computer Vision and Image processing library developed by Intel. Despite it’s simplicity, NI Vision Development Module lacks of flexibility and expandability of Intel OpenCV, For example if we want to modify complex image processing algorithm, it is easier to use OpenCV. Due to it’s open source nature, OpenCV also has more advanced and up to date vision algorithms. Below is the set of application areas of the OpenCV :
• 2D and 3D feature toolkits
• Egomotion estimation
• Facial recognition system
• Gesture recognition
• Human-Computer Interface (HCI)
• Mobile robotics
• Motion understanding
• Object Identification
• Segmentation and Recognition
• Stereopsis Stereo vision: depth perception from 2 cameras
• Structure from motion (SFM)
• Motion tracking

In addition, OpenCV also has the following statistical machine learning libraries :
• Boosting
• Decision tree learning
• Expectation-maximization algorithm
• k-nearest neighbor algorithm
• Naive Bayes classifier
• Artificial neural networks
• Random forest
• Support vector machine (SVM)


The main power of OpenCV other than it’s completeness is its speed. Compared to other image processing library such as Mathworks Matlab Image Processing tool box, OpenCV which is based on C/C++ is faster.


In this tutorial, a method to combine Intel OpenCV and NI Labview using Dynamic Link Library is presented. The interfacing between Intel OpenCV and Labview is not a trivial task because it involves a few tricks on pointers and memory management.

The objective is to be able to use any OpenCV library in any Labview project hence we will have the power of both.


This tutorial will be divided into two series, first method using IMAQ array handle, and the second using Array data pointer, the second method is more complex but more general, in this series, only the first method is covered.



2 Interfacing Intel OpenCV and NI Labview with Dynamic Link Library

Dynamic Link Library (DLL) is a file of code, containing functions that can be called from other executable code. (Either an application or another DLL). DLL is a file which does a particular job and allows other program to use it to do that particular job as well. Microsoft Windows systems use DLL, for example comctl32.dll which does all the user interface jobs so other programs do not have to create their own interface.


2.1 Anatomy of DLL


Dynamic linking is mechanism that links applications to libraries at run time. The libraries remain in their own files and are not copied into executable files of the applications. DLL link to an application when the application is run, rather then when it is created. DLLs may contain links to other DLLs.

Below is the the basic structure of a DLL for Microsoft Visual C++ environment:



//==============================================================================
// DLL main entry-point functions

#include <windows.h>

BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved ) // Reserved
{
switch ( ul_reason_for_call )
{
case DLL_PROCESS_ATTACH:
// A process is loading the DLL.
break;
case DLL_THREAD_ATTACH:
// A process is creating a new thread.
break;
case DLL_THREAD_DETACH:
// A thread exits normally.
break;
case DLL_PROCESS_DETACH:
// A process unloads the DLL.
break;
}
return TRUE;
}



In Win32 the DllMain function is the main DLL entry point.

The DllMain function is called when a DLL is loaded or unloaded. The DllMain function is also called when a new thread is being created in a process already attached to the DLL; or when a thread has exited cleanly.

A DLL also contains functions that perform the activities the DLL expects to accomplish. Additionally the EXPORTS section header file (definition files) must be used to export these functions. For example :



__declspec(dllexport) float bcv_calculate(IMAQ_Image *ImageSrc);


2.2 Example – Writing a DLL with Visual C++ (v6.0, .Net 2003)


Here the DLL that communicates between Intel OpenCV and Labview is written. The OpenCV library is compiled with Microsoft Visual C++ as DLL.

To create a DLL the following four files are needed :
• a C/C++ Language source file
• a header file
• a project file
2.2.1 C language Source File

The code on the following page is the C language source file for the DLL using OpenCV classes.

The example DLL defines two simple functions using OpenCV library using two different methods:

First Method : using IMAQ Image pointer (from Labview) which will be converted to IplImage (from OpenCV).

• Calculate_BCV, calculate normalized Between Classes Variance, input:image, output:double values

• Equalization_OpenCV, Perform histogram equalization on the image. input:image, output:image


Second Method : Using Char pointer which will be converted to IplImage (from OpenCV), this is more complex method and will be covered in the next series of this tutorial.

• bcv_calculate_NI, calculate normalized Between Classes Variance, input:image, Line Width, Image Width, Image Height, output:double values.

• Equalization_OpenCV, Perform histogram equalization on the image. input:image, Line Width, Image Width, Image Height, output:image



The key trick is by utilizing the header from National Instruments, called NIVision.h as it has class that has access to the IMAQ classes called Image *. This class has pointer to address member that can be casted to OpenCV classes using ImageInfo *.

In this example, we will create a VI that calls one of these functions.
The source code for the DLL is as follows :

First method :






#include "stdafx.h"
#include <NIVision.h>
#include <CV.h>
#include "opencv_labview.h"
#include <iostream.h>
#include <windows.h>


float bcv_calculate(IMAQ_Image *ImageSrc)

{

ImageInfo *src;
IplImage* imgHistogram = 0;
IplImage* gray= 0;
int maxThreshold;
CvHistogram* hist;
float alpha[256],PT[256],varMax=0,varBetween;
int height,width,step,channels,heightgray,widthgray,stepgray,channelsgray;
uchar *data;
uchar *datagray;
int numberofgreyLevel=256;
float sum,sumB;
int i,j,k,begin=0;
float wB,wF,mB,mF,mT=0,varTotal=0,bcv,mU;
int t,Threshold;
//size of the histogram -1D histogram
int bins = numberofgreyLevel;
int hsize[] = {bins};

//max and min value of the histogram
float max_value = 0, min_value = 0;

//value and normalized value
float value;
int normalized;
int totalnumberPixel;


//ranges - grayscale 0 to 256
float xranges[] = { begin, numberofgreyLevel };
float* ranges[] = { xranges };

if (!ImageSrc) return ERR_NOT_IMAGE;

src = (ImageInfo *) ImageSrc->address;
imaqGetImageSize((Image *)src, &width, &height);
gray = cvCreateImageHeader( cvSize(width, height), IPL_DEPTH_8U, 1);
gray->imageData = (char *) src->imageStart;
gray->widthStep = src->pixelsPerLine;

// get the image data
height = gray->height;
width = gray->width;
step = gray->widthStep;
channels = gray->nChannels;
data = (uchar *)gray->imageData;


//planes to obtain the histogram, in this case just one
IplImage* planes[] = { gray };

//get the histogram and some info about it
hist = cvCreateHist( 1, hsize, CV_HIST_ARRAY, ranges,1);
cvCalcHist( planes, hist, 0, NULL);
cvGetMinMaxHistValue( hist, &min_value, &max_value);

//create an 8 bits single channel image to hold the histogram
//paint it white
imgHistogram = cvCreateImage(cvSize(bins, 50),8,1);
cvRectangle(imgHistogram, cvPoint(0,0), cvPoint(256,50), CV_RGB(255,255,255),-1);

// calculate total number of pixel
totalnumberPixel=0;
for(i=begin;i sum=0;

for(t=begin; t < numberofgreyLevel; t++) sum += t * cvQueryHistValue_1D( hist, t);
//calculate total variance
for(i=begin;i sumB=0;
wB=0;
wF=0;
varMax=0;
Threshold=0;


for(t=begin;t {

wB+=cvQueryHistValue_1D( hist, t);
if (wB==0) continue;
wF=(totalnumberPixel-wB);
if(wF==0)break;
sumB+=(float) (t*cvQueryHistValue_1D( hist, t));
mB=sumB/wB;
mF=(sum-sumB)/wF;




// Calculate Between Class Variance
varBetween = wB * wF * (mB - mF) * (mB - mF);

// Check if new maximum found
if (varBetween > varMax) {
varMax = varBetween;
Threshold = t; }

}

// calculate BCV metric
bcv=varMax/varTotal;

return bcv;
}

// histogram equalization
int Equalization_OpenCV (IMAQ_Image *ImageSrc, IMAQ_Image *ImageDst)
{

ImageInfo *src, *dst;
int width, height;
IplImage *CVImageSrc, *CVImageDst;
if (!ImageSrc) return ERR_NOT_IMAGE;
if (!ImageDst) return ERR_NOT_IMAGE;
src = (ImageInfo *) ImageSrc->address;
dst = (ImageInfo *) ImageDst->address;
imaqGetImageSize((Image *)src, &width, &height);
imaqSetImageSize((Image *)dst, width, height);
CVImageSrc = cvCreateImageHeader( cvSize(width, height), IPL_DEPTH_8U, 1);
CVImageSrc->imageData =(char *) src->imageStart;
VImageSrc->widthStep = src->pixelsPerLine;
CVImageDst = cvCreateImageHeader( cvSize(width, height), IPL_DEPTH_8U, 1);
CVImageDst->imageData =(char *) dst->imageStart;
CVImageDst->widthStep = dst->pixelsPerLine;


// Perform histogram equalization
cvEqualizeHist( CVImageSrc, CVImageDst );


return 0;

}





We need to to declare the function with the __declspec (dllexport) keyword in the header file, otherwise the function will be exported with the mangled name and the actual function name will be unavailable to applications that call the DLL. We also enclose the declarations of the functions to be exported in header file with extern “C” statement.

The header file is defined as :






//==============================================================================
//
// Title: bcv DLL
// Purpose: A header to bcv dll .
//
// Created by : Adhiguna Mahendra.
// Copyright: -. All Rights Reserved.
//
//==============================================================================

#ifndef __bcv_opencv_H__
#define __bcv_opencv_H__

#ifdef __cplusplus
extern "C" {
#endif

//==============================================================================

#include


//==============================================================================
// Constants

//==============================================================================
// Types

typedef struct
{
char *name;
Image *address;
} IMAQ_Image;

//==============================================================================
// External variables

//==============================================================================
// Global functions

// export functions
__declspec(dllexport) float bcv_calculate(IMAQ_Image *ImageSrc);
*ImageDst);
__declspec(dllexport) int Equalization_OpenCV (IMAQ_Image *ImageSrc, IMAQ_Image *ImageDst);


#ifdef __cplusplus
}
#endif

#endif /* ndef __bcv_opencv_H__ */





2.3 building the project


First we need to create the project, add the C and header source code then compile it into DLL.

To compile the DLL successfully, we have to make sure that the precompiled header is deactivated (Project->Settings->All Configurations->C/C++->precompiled headers).




For example we compiled the above codes with the name bcv_opencv.dll.





1. Calling the DLL from Labview.

The DLL can now be called using Labview Call Library Function. We create a VI that calls the bcv_opencv.dll.





We set the library to the DLL we have just created. Then put the correct parameters. The input is void *ImaqImage and the output is double.




The VI is connected as follows :





3 Remarks and conclusion


In this tutorial, a tricky interfacing between Intel OpenCV and NI Labview is performed. In the future tutorial we will try to combine Matlab image processing library with NI Labview.


If you want the example VI files and Visual C/C++ project files, just contact me :
adhiguna.mahendra@yahoo.com




















View Adhiguna Mahendra,Ph.D's profile on LinkedIn

Senin, 20 April 2009

MVtec Halcon, the champion in Machine Vision software




HALCON defines the state of the art in machine vision software. It provides a comprehensive vision library and is always based on the latest and most advanced technology. Whatever your task, HALCON will solve it, fast and with highest accuracy.
Vision Development Environment A professional image processing tool must be more than just a library of image processing operators.

Solving image processing tasks is just one part of a complete solution, which comprises other software components like process control or database access, and hardware components from illumination to image acquisition devices and many other mechanical components. Therefore, it is important that the image processing system is easy to use and can be integrated into the development cycle in a flexible
manner.

To achieve this, HALCON takes care of all important aspects:

• The software development is supported by HALCON’s IDE (integrated development environment),consisting of HDevelop and HDevEngine. HDevelop is a highly interactive development tool that enables a quick development of image processing tasks. Via HDevEngine, you can directly execute HDevelop programs and procedures from your C++, C#, Visual Basic, or C application. As an alternative, HDevelop can also export programs and procedures in your programming language.



• The problem-oriented documentation covers all levels from a quick access to important information up to a detailed discussion of advanced topics.

• These descriptions are combined with hundreds of examples for an intuitive understanding of the solutions, which can serve as templates to shorten the development time.

• Last but not least, HALCON provides open interfaces for efficient data exchange, to integrate own operators, or to access specialized hardware round off the system.

HALCON fulfills all requirements of a professional vision library:

• It comprises methods for all standard and advanced types of image processing from image acquisition from many different devices up to the advanced shape-based matching.

• Apart from image processing functionality, HALCON provides tools that are typically needed in the context of machine vision applications, e.g., for the communication via sockets or the serial interface, file handling, data analysis, arithmetic operations, or classification.

• HALCON offers flexible ways of parallelization to exploit multi-processor or multi-core hardware to speed up an application.

• The HALCON library that is used in an application will not be visible to the end user and requires only minimum resources in an installation, which makes it perfect for OEM developments.

Key Features

Leading-Edge Technologies In addition to the full set of standard machine vision methods, HALCON offers functionality that is outstanding in the field of machine vision libraries, e.g., 3D camera calibration, shape-based and componentbased
matching, subpixel-precise edge and line extraction, subpixel contour processing, reconstruction via binocular stereo, arbitrary regions of interest, and much more.
Apart from this, many methods that are known from other libraries are offered with a much better performance.

An example for this is the morphology, which is up to 100 times faster than in other products, and at the same time offers much more flexibility.

One Software for All Applications

Thanks to its more than 1300 operators, HALCON is at home in all areas of research, development, and production where images are processed and analyzed. Numerous customers all over the world already use HALCON to solve their machine vision tasks.
Protection of Investment By choosing HALCON, you choose independence: Switch to another operating system? HALCON supports a wide range of Windows, Linux, and UNIX platforms, including x64 systems. Migrate your applications from C++ to C#? HALCON can be used within various programming languages and environments.




Your application grows and needs more computing power? Switch to a multi-processor
or multi-core computer and HALCON will automatically parallelize its execution.

Last but not least, you are free to choose the image acquisition hardware that fulfills your requirements, because HALCON provides ready-to-use interfaces to a large number of image acquisition devices (analog, digital, IEEE 1394, CameraLink).


Rapid Prototyping


In many cases it is important to determine quickly if and how a problem can be solved. With HDevelop, HALCON’s interactive development environment, you can rapidly develop machine vision applications.

Besides being a fully-fledged program interpreter with debug functions, HDevelop assists you actively,e.g., by suggesting operators and by automatically visualizing the result of an operation.

With the help of integrated tools you can inspect images and results and quickly find suitable parameter values that solve your vision task.

Open Architecture


HALCON offers a comprehensive vision library but does not claim to be all-encompassing. Therefore, it is based on an open architecture. Thus, you can extend HALCON by integrating your own vision functionality in form of new operators.

And if you want to use an image acquisition device that HALCON does not yet support you can use the images directly or create an image
acquisition interface for it.

View Adhiguna Mahendra,Ph.D's profile on LinkedIn

Kamis, 16 April 2009

OpenCL, the opensource parallel computing platform


OpenCL (Open Computing Language) is a framework for writing programs that execute across heterogeneous platforms consisting of CPUs, GPUs, and other processors. OpenCL includes a language (based on C99) for writing kernels (functions that execute on OpenCL devices), plus APIs that are used to define and then control the heterogeneous platform. OpenCL provides parallel programming using both task-based and data-based parallelism.

The purpose of OpenCL is analogous to that of OpenGL and OpenAL, which are open industry standards for 3D graphics and computer audio respectively. OpenCL extends the power of the GPU beyond graphics (GPGPU). OpenCL is managed by the non-profit technology consortium Khronos Group.
OpenCL was initially developed by Apple Inc., which holds trademark rights, and refined into an initial proposal in collaboration with technical teams at AMD, Intel and Nvidia. Apple submitted this initial proposal to the Khronos Group. On June 16, 2008 the Khronos Compute Working Group was formed with representatives from CPU, GPU, embedded-processor, and software companies. This group worked for five months to finish the technical details of the specification for OpenCL 1.0 by November 18, 2008. This technical specification was reviewed by the Khronos members and approved for public release on December 8, 2008.

OpenCL 1.0 is scheduled to be introduced in Mac OS X v10.6 ('Snow Leopard'). According to an Apple press release:

Snow Leopard further extends support for modern hardware with Open Computing Language (OpenCL), which lets any application tap into the vast gigaflops of GPU computing power previously available only to graphics applications. OpenCL is based on the C programming language and has been proposed as an open standard.

AMD has decided to support OpenCL (and DirectX 11) instead of the now deprecated Close to Metal in its Stream framework.

RapidMind announced their adoption of OpenCL underneath their development platform, in order to support GPUs from multiple vendors with one interface. NVIDIA announced on December 9, 2008 to add full support for the OpenCL 1.0 specification to its GPU Computing Toolkit.

The OpenCL specification is under active development at Khronos - which open to any interested company to join.

The interesting thing about OpenCL for me is it's interoperability with OpenGL. Both APIs may handle the the same type of workloads and share the textures, Buffer Objects and Renderbuffers. OpenCL objects are created from OpenGL objects, for example Vertex and image data generated with OpenCL may the rendered with OpenGL, on the other hand, images rendered with OpenGL may be post-processed with OpenCL kernels.




View Adhiguna Mahendra,Ph.D's profile on LinkedIn

Sabtu, 13 Desember 2008

[ebook] L’essentiel de l’informatique et de la Programmation



Ce livre est le premier en son genre dans la literature informatique francophone. Il rassemble toutes les notions essentielles connaitre sur l’informatique et la programmation.

Cet ouvrage est directement issu d’enseignements dispenses depuis plusieurs annees par l’auteur un public d’etudiants de premier cycle universitaire, et d’etudiants en formation complementaire informatique. La presentation privilegie l’aspect didactique fonde sur le couplage approche th?orique-approche pratique en mettant systematiquement l’accent sur les programmes implementant les mecanismes etudies.

Telecharge ici :





View Adhiguna Mahendra,Ph.D's profile on LinkedIn

[ebook] Programmation système en C sous Linux, Signaux, processus, threads, IPC et sockets



Tirer le meilleur parti de l'environnement Linux

La possibilité de consulter les sources du système, de la bibliothèque glibc et de la plupart des applications qui tournent sur cet environnement représente une richesse inestimable aussi bien pour les passionnés qui désirent intervenir sur le noyau, que pour les développeurs curieux de comprendre comment fonctionnent les programmes qu'ils utilisent quotidiennement.

Nombreuses sont les entreprises qui ont compris aujourd'hui tout le parti qu'elles pouvaient tirer de cette ouverture des sources, gage de fiabilité et de pérennité, sans parler de l'extraordinaire niveau de compétences disponible au sein d'une communauté de programmeurs aguerris au contact du code des meilleurs développeurs OpenSource.

Un ouvrage conçu pour les programmeurs Linux et Unix les plus exigeants.
Sans équivalent en langue française, l'ouvrage de Christophe Blaess constitue une référence complète de la programmation système sous Linux, y compris dans les aspects les plus avancés de la gestion des processus, des threads ou de la mémoire. Les programmeurs travaillant sous d'autres environnements Unix apprécieront tout particulièrement l'attachement de l'auteur au respect des standards (C Ansi, glibc, Posix...), garant d'une bonne portabilité des applications. La deuxième édition de ce livre a été entièrement actualisée en fonction du noyau Linux 2.6 et des nombreuses évolutions de la bibliothèque C.


À qui s'adresse cet ouvrage ?

* Aux programmeurs et développeurs intéressés par les aspects système de la programmation sous Linux et Unix.
* Aux administrateurs système en charge de la gestion d'un parc Linux et/ou Unix.
* Aux étudiants en informatique (1e et 2e cycle universitaire, écoles d'ingénieurs, etc.).

Lien direct:



View Adhiguna Mahendra,Ph.D's profile on LinkedIn

Kamis, 04 Desember 2008

Industrial PhD atau Conventional PhD ?

Banyak pertanyaan sebagai berikut, lulusan PhD bisa nggak sih berkarir di dunia profesional dan bukannya akademia?

Jawabannya BISA. Tapi sebaiknya anda mengikuti industrial PhD dan bukannya conventional PhD.

Pada conventional PhD, anda akan menghabiskan waktu 3-5 tahun melakukan riset di lab universitas dan memang dipersiapkan untuk menjadi seorang akademisi yang handal dan diharapkan dapat mempublikasikan beberapa paper ilmiah internasional.

Pada industrial PhD, anda melakukan riset di industri berdasar project-project komersial yang sedang dikerjakan disana dibawah bimbingan engineer senior di industri dan professor di universitas, anda diharapkan menjadi seorang engineer yang mengetahui metodologi riset dan pengembangan produk secara mendalam.

Saya saat ini mengambil PhD di Perancis sambil bekerja sebagai R&D engineer di industri. Luluspun nanti saya berminat untuk tetap meneruskan karir di industri dan bukan di akademia.

Tidak enaknya industrial PhD adalah semua riset dan pekerjaan anda, termasuk paten adalah milik perusahaan dan tidak boleh dipublikasikan sampai batas waktu tertentu. Jadi anda benar-benar miskin publikasi. Kemudian karena terlalu sibuknya anda di industri, sering kali harus menyediakan waktu khusus untuk memikirkan riset dan menulis thesis. Juga karena benar-benar harus applicable di industri dengan berbagai deadline proyek yang mepet, biasanya anda harus mengorbankan kualitas thesis doktoral anda. Kualitas thesis doktoral konvensional di Universitas jelas lebih baik karena mereka sehari-hari nongkrong di lab riset dan membaca jurnal ilmiah, sementara industrial PhD student sehari-hari nongkrong di pabrik. Membaca jurnal ilmiah hanya di WC saja pada malam hari kalau sempat. Perusahaan hanya memperbolehkan industrial PhD student untuk konsultasi dan riset ke Universitas 3-4 hari perbulan (walaupun biasanya kami, eh saya menggunakan waktu ini untuk bermalas-malasan).

Enaknya adalah sebagai engineer yang mengerjakan riset, anda memiliki pengalaman kerja di industri sekaligus pengalaman riset di Universitas. Gajinya juga sama dengan engineer biasa, jadi jauh diatas rate PhD student di Universitas, sehabis lulus PhD, posisi anda juga akan diatas engineer-engineer lain, misal menjadi project manager.

Kesibukan anda sangat banyak mulai dari ketemu dan meyakinkan klien, meeting, presentasi, mengatur budget, mengatur engineer junior, project management, programming, instalasi alat DITAMBAH riset dan menulis thesis.
Di Eropa, terutama di Perancis, Denmark dan Jerman, ada skema khusus yang dinamakan Industrial PhD, yaitu mengerjakan topik PhD yang diaplikasikan di industri.

Di Perancis namanya Cifre (Les Conventions Industrielles de Formation par la REcherche) yaitu skema yang memungkinkan engineer melakukan riset doktoral di
Universitas sambil menerapkannya di industry dan mendapatkan gelar PhD.

Saya bukan satu-satunya engineer yang mengambil PhD di perusahaan saya, ada beberapa orang. Di divisi saya saja, pemegang gelar PhD ada sekitar 50%, di divisi lain juga ada.

Di Jerman dan Perancis banyak sekali pemegang gelar PhD yang memegang posisi di managerial ataupun engineering so tidak ada masalah PhD overqualified di Eropa. Namun biasanya memang riset PhD mereka menunjang pekerjaan dan karir mereka.

Untuk bidang bisnis, anda bisa mengambil Doctor of Business Administration.

View Adhiguna Mahendra,Ph.D's profile on LinkedIn