Wednesday, April 18, 2012

drawing sine wave using opencv

I want to draw a sine wave on a image using openCV. I developed the following code, but the output is not coming as expected:



#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "opencv/cv.h"
#include "opencv/highgui.h"
void main()
{
double y[100];
float x;


for(x=0;x<100;x++)
{
y[(int)floor(x)]=sin(x);

}

IplImage *grf = cvCreateImage( cvSize( 200, 200), IPL_DEPTH_8U, 1 );


for(int x=0;x<100;x++)
{
cvLine(grf , /* the dest image */
cvPoint(x, y[x]), /* start point */

cvPoint(x+1, y[x+1]), /* end point */
CV_RGB(255, 0, 0), /* the color; green */
2, 4, 0); /* thickness, line type, shift */

}


cvNamedWindow("img", CV_WINDOW_AUTOSIZE);
cvShowImage("img", grf);
cvWaitKey(0);
cvDestroyWindow("img");
cvReleaseImage(&grf);
}


I have verified the values coming in y array is correct, and plotted these y values using MATLAB. The MATLAB plot is coming a sine wave. Can you tell me why i am not getting correct plot using above code.



My output plot of sine wave is coming as below: you can see I am just getting a horizontal line instead of the sine wave. Any help regarding this?



enter image description here





No comments:

Post a Comment