Mario.Tapilouw

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: , , , ,

4 Comments:

  • hello
    there are many unknown in ths code i,e,
    how height,icols,textoffset,zeropos can be extracted.furthermore avtImgProfile and avtLineMat. How they can be extracted from
    avtImgProfile =cvLoadImage(imagefile);
    Mat avtLineMat = imread(imagefile);
    and also in the loop which should be read as
    for(int i=0;i<icols;i++) instead of for(int i=0;icols;i++)

    could you ease a little more your program? thank you

    By Anonymous fferrieu, at August 4, 2013 at 2:35 AM  

  • Hello Fferrieu,

    Thank you for your comments, here they are

    - height is the value that we want to draw on the chart, it represents the height from scale of 0-255 in this case;
    - icols is number of collumns, in practical it is equal to the number of data we want to show on the chart
    - textoffset is the offset value from the left side where we want to show the text
    - zeropos is the position of zero point
    - avtImgProfile is the iplimage for drawing the image
    - avtLineMat is the matrix which stores the data to be drawn on the chart

    Thanks for correction of the loop, blogger cuts the the text
    for(int i=0;i<icols;i++)

    By Blogger mario, at September 28, 2013 at 3:28 PM  

  • Can you share your full code to me? Thanks

    By Blogger tan kaiyang, at May 7, 2016 at 1:17 AM  

  • Hi Tan Kaiyang,
    It's been a while since I wrote the code. I don't have the full source code at the moment. What kind of problem do you have?

    By Blogger mario, at June 14, 2016 at 4:10 PM  

Post a Comment

<< Home