Sine wave drawing using opencv

I want to draw a sine wave on an image using openCV. I developed the following code, but the result does not fit 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 checked that the values ​​coming into the y array are correct, and plotted these y values ​​using MATLAB. The MATLAB graph fits a sine wave. Can you tell me why I am not getting the correct plot using the code above.

My output sine wave graph goes like this: you can see that I am just getting a horizontal line instead of a sine wave. Any help on this?

enter image description here

SOLVED: after the answer, I got the following image:

enter image description here thanks

+6
source share
2 answers

The sin function returns you the value [-1: 1], so you set the coordinates between lines 0 and 1 in your image. You need to increase the amplitude of your sin , shift its zero value and reduce the frequency of the sine wave (since sin takes an angle in radians, so you will go through a period of 4 steps):

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

So you can see the sine wave.

+6
source
  • To the right, when you set the pixel value to display a sine wave, add an offset. Since the image is 200x200, try adding 100,100 to x, y. This should give you, starting from the wave, starting from the center.
0
source

Source: https://habr.com/ru/post/913528/


All Articles