使用滑块openCV c ++移动对象

时间:2013-05-18 15:50:28

标签: c++ opencv

我对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; } 

1 个答案:

答案 0 :(得分: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 );