由于opencv库中并没有专门为开关而设的函数,可以用滑动条做开关
代码:
#include#include int g_switch_value = 0;IplImage *img;void switch_off_fcuntion();void switch_on_function();void switch_callback(int position){ if (!position) { switch_off_fcuntion(); } else { switch_on_function(); }}int main(void){ cvNamedWindow("sample",1); cvCreateTrackbar("Switch", "sample", &g_switch_value, 1, switch_callback);//中间的数值用来自定义可变换区间长度 while (1) { if (cvWaitKey(15) == 27) { cvReleaseImage(&img); cvDestroyAllWindows(); break; } } return 0;}void switch_off_fcuntion(){ puts("This is q1"); img = cvLoadImage("q1.jpg", -1); cvShowImage("sample", img); puts("Q1"); return;}void switch_on_function(){ puts("This is q1"); img = cvLoadImage("q2.jpg", -1); cvShowImage("sample", img); puts("Q2"); return;}