0

我对 OpenCV (c++) 很陌生,我的讲师让我制作一个简单的滑块,滑块中的每个位置在窗口中都有不同的位置。我下面的代码可以根据滑块位置在窗口中移动对象,但是当我移动滑块时,旧位置仍然存在。所以它看起来像一个重复的不动。谁能帮我解决这个问题??有没有办法解决这个问题或者我必须完全改变代码?

#include "stdafx.h"
#include "cv.h" 
#include "ml.h" 
#include "cxcore.h" 
#include "highgui.h" 


int g_switch_value = 0; 
int colorInt = 0; 

void switch_callback( int position ){ 
    if( position == 0 ){ 
        colorInt = 0; 
    }else{ 
        colorInt = 1; 
    } 
} 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
const char* name = "Change the color of circle in the picture"; 
int radius = 30; 
int thickness = 12; 
int connectivity = 8; 
cvscalar red = cv_RGB(0,0,255); 

IplImage* src1 = cvLoadImage( "E:/2.jpg" ); 

CvPoint pt1 = cvPoint(405,195); 
CvPoint pt2 = cvPoint(620,400);

cvNamedWindow( name, 1 ); 
cvShowImage(name, src1); 

cvCreateTrackbar( "Change", name, &g_switch_value, 1, switch_callback ); 

while( 1 ) { 
    if( colorInt == 0) { 
        cvCircle(src1,pt1,radius,red,thickness,connectivity);}
    else { 
        cvCircle(src1,pt2,radius,red,thickness,connectivity); }
        colorInt == 1;
        cvShowImage(name, src1); 
    if( cvWaitKey( 15 ) == 27 )  break; 
} 
cvReleaseImage( &src1 ); 
cvDestroyWindow( name ); 

return 0; } 
4

1 回答 1

0

绘画覆盖图像,然后您应该为每幅不同的绘画保留一份新副本。更简单的方法:

IplImage* src1 = cvLoadImage( "..." );
IplImage* src2 = cvLoadImage( "..." );
...

while( 1 ) {
    if( colorInt == 0) {
        cvShowImage(name, src1);
        cvCircle(src1,pt1,radius,red,thickness,connectivity);
    }
    else {
        cvShowImage(name, src2);
        cvCircle(src2,pt2,radius,red,thickness,connectivity);
    }
    if( cvWaitKey( 15 ) == 27 )  break;
}
cvReleaseImage( &src1 );
cvReleaseImage( &src2 );
于 2013-05-18T18:21:16.547 回答