2012-04-18 89 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。這應該會給你一個自中心起波