RSS
 

Archive for August, 2009

Ubuntu: installing Opencv from source step-by-step

25 Aug

To install OpenCV library in Ubuntu from source, I tried these steps. The code of OpenCV is not the last but the one which is in the Ubuntu repository. If you want to install the last version just download it from the official SVN in sourceforge.
[ad#Amazon Opencv]
You can install opencv anywhere but its best you put in the propoer place

1) Sign as root

sudo su

2) Go to

cd /usr/local/src

3) prepare your environment

apt-get install build-essential

4) prepare your environment

apt-get install dpkg-dev

5) Download opencv packages

apt-get source opencv

6) Go to extracted opencv folder

cd opencv-1.0

7) List all dependency package informed in

more /debian/control 

8) Install all dependency package informed in /debian/control by invoking

apt-get install [package-1] package-2] [packga-3] ... [package-n]

9) Also install other necessary package

apt-get install fakeroot ffmpeg quilt

10) Build package by invoking

dpkg-buildpackage -us -uc -rfakeroot

11) Go to samples folder

cd /samples/c

12) Build samples

make -f  Makefile.debian

13) Plug in your webcam and run the samples

./facedetect

14) See you face detected in monitor

 
No Comments

Posted in Ubuntu

 

OpenCV: Otsu thresholding

25 Aug

I found this beautiful piece of code in the mailing list of OpenCV. I thought to share it with you and in the same time to archive it for future use.

[ad#AdBrite inline]

//----------------------------------------------------------
// binarisation otsu
//----------------------------------------------------------
#include "iostream"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"
#include "cxcore.h"
#include "cv.h"
#include "highgui.h"

int GRAYLEVEL = 256;
#define MAX_BRIGHTNESS 255
//const double INFO_THRESHOLD = 0.2;

using namespace std;

//----------------------------------------------------------
// binarization by Otsu's method
// based on maximization of inter-class variance
//----------------------------------------------------------
void binarize_otsu(IplImage* image, IplImage* imgBin )
{
int hist[GRAYLEVEL];
double prob[GRAYLEVEL],
omega[GRAYLEVEL]; // prob of graylevels
double myu[GRAYLEVEL]; // mean value for separation
double max_sigma,
sigma[GRAYLEVEL]; // inter-class variance

int i, x, y; /* Loop variable */
int threshold; /* threshold for binarization */

// Histogram generation
memset((int*) hist , 0, GRAYLEVEL * sizeof(int) );

CvSize size = cvGetSize(image);

for (int i = 0; i < size.height; ++i)
{
unsigned char* pData = (unsigned char*) (image->imageData + i *
image->widthStep);
for (int j = 0; j < size.width; ++j)
{
int k = (int)((unsigned char) *(pData+j));
hist[k]++;
}
}

int taille = size.width * size.height;

// calculation of probability density
for ( i = 0; i < GRAYLEVEL; ++i )
{
prob[i] = (double) ((double)hist[i] / (double)taille);
}

/*
int LEVEL = GRAYLEVEL;
double sumProb = 0.0;
for ( i = 0; i < GRAYLEVEL; ++i )
{
sumProb += prob[i];
if ( sumProb > INFO_THRESHOLD)
{
LEVEL = i;
break;
}
}
GRAYLEVEL = LEVEL;
*/

// omega & myu generation
omega[0] = prob[0];
myu[0] = 0.0;
for (i = 1; i < GRAYLEVEL; i++)
{
omega[i] = omega[i-1] + prob[i];
myu[i] = myu[i-1] + (i*prob[i]);
}

//----------------------------------------------------------
// sigma maximization
// sigma stands for inter-class variance
// and determines optimal threshold value
//----------------------------------------------------------
threshold = 0;
max_sigma = 0.0;
for (i = 0; i < GRAYLEVEL-1; i++)
{
if (omega[i] != 0.0 && omega[i] != 1.0)
{
//sigma[i] = (omega[i]*(1.0 - omega[i])) * ((myu[GRAYLEVEL-1] - 2*myu[i]) *
(myu[GRAYLEVEL-1] - 2*myu[i]));
sigma[i] = ((myu[GRAYLEVEL-1]*omega[i] - myu[i]) *
(myu[GRAYLEVEL-1]*omega[i] - myu[i])) / (omega[i]*(1.0 - omega[i]));
}
else
{
sigma[i] = 0.0;
}
if (sigma[i] > max_sigma)
{
max_sigma = sigma[i];
threshold = i;
}
}

printf("threshold = %d\n", threshold);

// binarization output into imgBin
for (y = 0; y < size.height; ++y)
{
unsigned char* pData = (unsigned char*) (image->imageData + (y *
image->widthStep));
unsigned char* pDataBin = (unsigned char*) (imgBin->imageData + (y *
imgBin->widthStep));
for (x = 0; x < size.width; ++x)
{
if ( *(pData+x) > threshold)
{
*(pDataBin+x) = MAX_BRIGHTNESS;
}
else
{
*(pDataBin+x) = 0;
}
}
}

}

int main(int argc, char* argv[] )
{
IplImage* image = 0;
IplImage* imgBin = 0;
clock_t start = clock();
(argc == 3) ? image = cvLoadImage(argv[1], CV_LOAD_IMAGE_GRAYSCALE) :
cout < < "Usage :\n" << argv[0] << " [srcImagePath] [dstimagepath]" < < endl;

if ( image != NULL )
{
imgBin = cvCloneImage(image);
binarize_otsu(image,imgBin );
cvSaveImage(argv[2], imgBin);
cvReleaseImage(&image);
cvReleaseImage(&imgBin);
clock_t end = clock();
double delay = double((double)(end - start) / CLOCKS_PER_SEC);
printf("processing time : %lf seconds\n",delay);
return EXIT_SUCCESS;
}

return EXIT_FAILURE;
}

[ad#AdBrite inline]

 
1 Comment

Posted in OpenCV

 

HowTo: Install Squid proxy server in Ubuntu

22 Aug

Squid is a proxy http server that speeds up getting pages from the internet by keeping copies of commonly accessed pages or graphics instead of downloading them each time. To install it:-

1. From a root terminal type

sudo apt-get install squid

2. Open

sudo gedit /etc/squid/squid.conf

3. Find the TAG: visible_hostname and after the comments section add visible_hostname where is your machine’s hostname.

4. Check http_port is either set to 3128 or a port number that you can remember for configuring your browser.

5. Close and save

6. Type adduser squid and specify a password

7. Restart squid by typing:

sudo /etc/init.d/squid restart

8. Stop the service by typing

sudo /etc/init.d/squid stop

9. Test it in debug mode by typing

sudo squid -z

(which creates the cache files)

10. Type

sudo squid -NCd10

to test squid in debug mode and leave it running.

11. Open Firefox and type the URL localhost:3128 or whatever port you chose. It will fail to retrieve a page, but at the bottom it will confirm that the error is generated by squid.

12. Back at the Terminal type CTRL-C to cancel the debug mode

13. Start squid for real with

sudo /etc/init.d/squid start

It will start automatically from now on.

14. To configure Firefox to use squid, go to Edit>Preferences and click Advanced.

15. Click Network>Settings and then Manual Proxy Configuration. For http proxy, enter localhost and for port 3128 (or whichever port you chose).

16. Then click OK and close the Preferences dialogue.

17. Now go to any webpage. If you get the page, it’s working!

[ad#Ubuntu 9.04]

 
2 Comments

Posted in HowTo

 

HowTo: Redirect the all HTTP traffic

22 Aug

If you would like to redirect the all HTTP traffic through the proxy without needing to set up a proxy manually in all your applications you will need to add some rules

iptables -t nat -A PREROUTING -i eth1 -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.0.1:3128
iptables -t nat -A PREROUTING -i eth0 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128

Where eth1,eth0 are the LAN, WAN devices and 192.168.0.1 is the IP address of your LAN device.

[ad#AdBrite inline]

 
No Comments

Posted in HowTo

 

HowTo: Install MySQL Server 5 on Ubuntu

22 Aug

Installing MySQL 5 Server on Ubuntu is a quick and easy process. It almost feels like it should be more difficult.

Open a terminal window, and use the following command:

sudo apt-get install mysql-server

During the installation of MySQL the installer will ask you to enter the password of the root or the main admin of your database server.

If you are running PHP you will also need to install the php module for mysql 5:

sudo apt-get install php5-mysql

To create a new database, use the mysqladmin command:

mysqladmin create [databasename]

[ad#Ubuntu 9.04]

 
No Comments

Posted in HowTo