【问题标题】:drawing sine wave using opencv使用opencv绘制正弦波
【发布时间】:2012-04-18 13:02:13
【问题描述】:

我想使用 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);
}

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

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

已解决:按照答案后,我得到以下图像:

谢谢

【问题讨论】:

    标签: c++ visual-studio-2010 opencv graph


    【解决方案1】:

    sin 函数返回一个值 [-1:1],因此您在图像的第 0 行和第 1 行之间设置坐标。 您需要增加sin 的幅度,将其移至零值并降低正弦波的频率(因为 sin 接受以弧度表示的角度,因此您将分 4 步通过一个周期):

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

    因此,您将能够看到正弦波。

    【讨论】:

    • 改用了这个,y[(int)floor(x)]=100 + 20*sin(0.4* .1 * PI *x) 按照你的建议;情节在上面。谢谢
    • @geeta 这只是一个例子,具体数字取决于您的需要
    【解决方案2】:
    • 在您设置像素值以显示正弦波的位置,添加偏移量。由于图像是 200x200,请尝试将 100,100 添加到 x,y。这应该会给你一个从中心开始的自波

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 2012-12-05
      相关资源
      最近更新 更多