2011-07-12 91 views
1

我已經從opencv庫中給出的shape.c中獲取了代碼並進行了一些修改。OPENCV:我想在循環中繪製基本形狀,每次繪製新形狀時刪除舊形狀

#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; 
} 

我想,只要我遞增,老數字應被刪除,只有新的數字應該drawn.But什麼,我得到的是,隨着新的數據舊數據也present.Can沿着你幫幫我好嗎? 。

+0

也許不是你的問題,但你每次打開一個新窗口。 – Blender

回答

1

沒有直接的辦法

  1. 初始化背景圖像
  2. 新克隆的背景圖像
  3. 上畫前景的形狀畫一個克隆的背景圖像上的另一種形狀
  4. 顯示他們一個接一個
1

當您創建(空白)圖片時,唯一的辦法就是確保im ageData乾淨是通過設置一個純色自己做。 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");