Wednesday, September 16, 2009

Grabbing image from webcam using OpenCV

This is fun, I have a couple of unused webcam and trying to have some fun with them. This website shows a good tutorial about grabbing image from the webcam and I followed the tutorial. It is based on OpenCV and I used Visual C++ 2005 to write the program.

Here's what I got:

Hehehe.. this is just the beginning, wait for my other projects :D

Labels: , ,

Friday, September 11, 2009

Real Time chart using OpenCV

If you need a chart that is fast and need to be updated in real time, this article might be useful for you. I have been searching for real time charting but I couldn't find one. But it happened to be creating your own is not that difficult, so why not creating your own charting tools.

What you will need is OpenCV Library that has been installed and configured properly in your computer. If you don't know how to install and configure one, you can start by searching a tutorial using "OpenCV Installation" as the keywords in Google and you will find plenty of them.

Start by creating an object of IplImage, something like this:
IplImage *avtImgProfile;

Then, you might need to define some color lines and font for descriptions, like this:
CvScalar greenLine;
CvScalar redLine;
CvScalar blueLine;
CvScalar whiteLine;
CvFont font;

These color lines and fonts need to be initialized prior to use, the color can be initialize as follow:
greenLine = cvScalar(0, 255, 0);
redLine = cvScalar(0, 0, 255);
blueLine = cvScalar(255, 0, 0);
whiteLine = cvScalar(255, 255, 255);

The font can be initialized as follow:
cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1.0, 1.0, 0, 1, 8);

Then, if you have an array of data, for example from a matrix, you can iterate the data and draw it as a circle, or anything you need. I use circle for simplicity reason.

for(int i=0;icols;i++)
int height = avtLineMat->data.ptr[i];
int posx = textOffset + i;
int posy = zeroPos - height;
cvCircle(avtImgProfile, cvPoint(posx, posy), 1, whiteLine, 1);

You might need some description about the Chart, the axis and the Title, simply by adding these lines:

cvPutText(avtImgProfile, "Cross Section Profile", cvPoint(200, 15), &font, whiteLine);
cvPutText(avtImgProfile, "Pos.(pixel)", cvPoint(550, 290), &font, whiteLine);
cvPutText(avtImgProfile, "Int.(gray)", cvPoint(10, 15), &font, whiteLine);

And also you might need to add horizontal and vertical axis, such as:
cvLine(avtImgProfile, cvPoint(textOffset , textOffset ), cvPoint(textOffset ,300), whiteLine, 1);
cvLine(avtImgProfile, cvPoint(0, zeroPos ), cvPoint(669, zeroPos ), whiteLine, 1);

Then after finished iterating the data, you can show it using OpenCV display, like this:
cvShowImage("cross section profile", avtImgProfile);

And as a result, you will got is something like this:

So far, this chart can be updated in real time for a 200 fps camera by embedding this function in the callback.

I haven't tried it with a higher speed camera, and I might need to measure the speed of the display and try to make it faster.

Labels: , , , ,