使用opencv绘制正弦波

gee*_*eta 6 c++ opencv graph visual-studio-2010

我想使用openCV在图像上绘制正弦波.我开发了以下代码,但输出未达到预期:

#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);
}
Run Code Online (Sandbox Code Playgroud)

我已经验证了y数组中的值是否正确,并使用MATLAB绘制了这些y值.MATLAB图正在形成正弦波.你能告诉我为什么我没有使用上面的代码得到正确的情节.

我的正弦波输出图如下:你可以看到我只是得到一条水平线而不是正弦波.对此有何帮助?

在此输入图像描述

已解决:在得到答案后,我得到了以下图像:

在此输入图像描述 谢谢

Ale*_*lex 6

sin函数返回值[-1:1],因此您在图像上设置第0行和第1行之间的坐标.你需要增加你的振幅sin,移动它的零值并降低正弦波的频率(因为sin接受弧度的角度,所以你将以4个步骤通过一段时间):

y[(int)floor(x)]=10 + 10*sin(2*.1*PI*x);
Run Code Online (Sandbox Code Playgroud)

所以,你将能够看到一个正弦波.