2012-04-18 359 views
6

我想使用openCV在图像上绘制正弦波。我公司开发了下面的代码,但输出不来预期:使用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绘图正在出现一个正弦波。你能告诉我为什么我没有使用上面的代码得到正确的阴谋。

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

enter image description here

解决:下面的答案后,我得到了下面的图片:

enter image description here 感谢

回答

6

sin函数返回你一个值[-1:1],因此要设置图像上第0行和第1行之间的坐标。 你需要增加你的sin的幅度,转向它的零值,降低正弦波的频率(如罪接受弧度的角度,所以你会通过4个步骤一期):

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

所以,你将能够看到正弦波。

+0

完美!感谢您的快速帮助 – geeta 2012-04-18 13:48:20

+0

@geeta欢迎您! – Alex 2012-04-18 13:58:44

+0

代替你的建议,y [(int)floor(x)] = 100 + 20 * sin(0.4 * .1 * PI * x)情节在上面。谢谢 – geeta 2012-04-18 13:59:31

0
  • 正确设置像素值以显示正弦波的位置,添加偏移量。由于图像是200x200,请尝试向x,y添加100,100。这应该会给你一个自中心起波