OPENCV:我想在循环中绘制基本形状,每次绘制新形状时删除旧形状

时间:2011-07-12 06:25:37

标签: c opencv

我从opencv库中给出的shape.c中获取了代码并进行了一些修改。

#include <stdio.h>
#include "cv.h"
#include "highgui.h"

int main(int argc, char** argv)
{
    int i = 0;
    for(i=0;i<3 ;i++)
   { 
    /* create an image */
    IplImage *img = cvCreateImage(cvSize(200, 100), IPL_DEPTH_8U, 3);

    /* draw a green line */
    cvLine(img,                         /* the dest image */
           cvPoint(10 +i*10, 10),             /* start point */
           cvPoint(150, 80),            /* end point */
           cvScalar(0, 255, 0, 0),      /* the color; green */
           1, 8, 0);                    /* thickness, line type, shift */

    /* draw a blue box */
    cvRectangle(img,                    /* the dest image */
                cvPoint(20, 15+i*10),        /* top left point */
                cvPoint(100, 70),       /* bottom right point */
                cvScalar(255, 0, 0, 0), /* the color; blue */
                1, 8, 0);               /* thickness, line type, shift */

    /* draw a red circle */
    cvCircle(img,                       /* the dest image */
             cvPoint(110, 60), 35+i*10,      /* center point and radius */
             cvScalar(0, 0, 255, 0),    /* the color; red */
             1, 8, 0);                  /* thickness, line type, shift */

    /* display the image */
    cvNamedWindow("img", CV_WINDOW_AUTOSIZE);
    cvShowImage("img", img);
    cvWaitKey(0);
    cvDestroyWindow("img");
    cvReleaseImage(&img);
    }     
    return 0;
}

我希望每当我增加时,旧的数字应该被删除,只有新的数字应该被绘制。但我得到的是,随着新的数字旧的数字也出现。你能帮我吗?。

2 个答案:

答案 0 :(得分:1)

没有直接的方法

  1. 初始化背景图片
  2. 在新克隆的背景图像上绘制前景形状
  3. 在克隆的背景图像上绘制另一个形状
  4. 逐一展示

答案 1 :(得分:1)

创建(空白)图像时,确保 imageData 清洁的唯一方法是通过设置纯色来自行完成。在cvCreateImage()致电cvSet()之后。

IplImage *img = cvCreateImage(cvSize(200, 100), IPL_DEPTH_8U, 3); 
cvSet(img, CV_RGB(0, 0, 0));

如果取出循环中的取出窗口创建/销毁,则可以提高应用程序的性能。无需为每个新图像创建新窗口:

int i = 0;
cvNamedWindow("img", CV_WINDOW_AUTOSIZE);
for(i=0;i<3 ;i++)
{
    // code
}
cvDestroyWindow("img");
相关问题