Using OpenMP with OpenCV
After installing OpenMP it is time to use it on OpenCV. Before going into depth I found a working example about this issue. Let’s try to use it…
Edge finding example from http://derindelimavi.blogspot.com:
Include files/libraries needed:
// OpenCV #include "cv.h" #include "highgui.h" #include <stdio.h> #include <stdlib.h> // OpenMP #include <omp.h>
The usage of OpenMP in code:
void EdgeOpenMP(IplImage *src,IplImage *dst,int thresh)
{
int height = src->height;
int width = src->width;
int step = src->widthStep;
uchar *data1 = (uchar *)src->imageData;
uchar *data2 = (uchar *)dst->imageData;
int i=step;
#pragma omp parallel for
for(i=step+1;i<height*width;i++){
if(abs(data1[i]-data1[i-1])>thresh || abs(data1[i]-data1[i-step])>thresh)
data2[i]=255;
else
data2[i]=0;
}
}
void Edge(IplImage *src,IplImage *dst,int thresh)
{
int height = src->height;
int width = src->width;
int step = src->widthStep;
uchar *data1 = (uchar *)src->imageData;
uchar *data2 = (uchar *)dst->imageData;
int i=step;
for(i=step+1;i<height*width;i++){
if(abs(data1[i]-data1[i-1])>thresh || abs(data1[i]-data1[i-step])>thresh)
data2[i]=255;
else
data2[i]=0;
}
}
Compile:
“g++ -I/usr/include/opencv -o openmp_test openmp_test.cpp-lcxcore -lcv -lhighgui -lcvaux -fopenmp”
./openmp_test
Name of picture to process :
baboon.jpg
Time for finding edges = 1680.77 ms
Time for finding edges with OpenMP = 2011.76 ms
Performance (%) = % 83.5
Output:

Important note:
In my case you can say that OpenMP takes longer time for finding edges infact it is not the case since my computer has only one CPU we cannot see the real performance of OpenMP…But if you have multi core CPU you can observe the real results…
This post’s example was taken from a really good blog about OpenCV which is http://derindelimavi.blogspot.com/.
Code used in my example:
http://derin-deli-mavi.googlecode.com/files/OpenMP_OpenCv.zip
References:
[1.] http://derindelimavi.blogspot.com/2008/09/openmp-ve-opencv-ile-kenar-bulma.html


Erkan 6:34 am on January 7, 2012 Permalink |
Selamlar,
Opencv ile birden fazla kamerayı izleyebilmek için ne yapabilirim diye düşünürken OPENMP ile karşılaştım. Bu konuda nasıl uygulamalar var diye bakarken sizin makaleyi gördüm. Her iki kütüphaneyi kullanmak için desteğe ihtyacım var. Herhangi bir kaynak kod bulabilir miyim.