drawing sine wave using opencv

c++, graph, opencv, visual-studio-2010

Solution

The `sin` function returns you a value [-1:1], thus you are setting coordinates between line 0 and 1 on your image. You need to increase amplitude of your `sin`, shift it's zero value and reduce a frequency of the sine wave (as sin accepts angle in radians, so you'll pass a period in 4 steps):

y[(int)floor(x)]=10 + 10*sin(2*.1*PI*x);

So, you will be able to see a sine wave.

Problem

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? SOLVED: After following the answer I got the following image: Thanks

Original source