利用OpenCV实现模拟绳线运动

模拟绳线的运动,某一时刻的效果如下:

利用OpenCV实现模拟绳线运动_第1张图片

参考代码:

void DrawString(IplImage *image, double t)
{
    if(!image)
        return;
    cvSet(image, cvScalarAll(250));

    int sampleCount = image->width/2;
    int marginalX   = (image->width - sampleCount)/2;
    int marginalY   = image->height/2;
    int scale       = 100;
    int lineWidth = 2;

    // draw ends
    double y = sin(CV_PI*0/sampleCount)*cos(CV_PI*t)*scale + marginalY;
    cvCircle(image, cvPoint(0+marginalX, y), lineWidth, CV_RGB(0, 250, 0),
        3, CV_AA);
    y = sin(CV_PI*sampleCount/sampleCount)*cos(CV_PI*t)*scale + marginalY;
    cvCircle(image, cvPoint(sampleCount+marginalX, y), 3, CV_RGB(0, 250, 0),
        lineWidth, CV_AA);

    for(int x = 0; x <= sampleCount; ++x){
        y = ( sin(CV_PI*x/sampleCount)*cos(CV_PI*t)       - 
            0.5*sin(2*CV_PI*x/sampleCount)*cos(2*CV_PI*t) + 
            0.33333333*sin(3*CV_PI*x/sampleCount)*cos(3*CV_PI*t) )*scale + marginalY;

        cvLine(image, cvPoint(x+marginalX, y), cvPoint(x+marginalX, y), 
            CV_RGB(250, 0, 0), 2, CV_AA);                
    }
}

关于Image Engineering & Computer Vision的更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.

你可能感兴趣的:(利用OpenCV实现模拟绳线运动)