Opencv根据键盘指令,连续显示图像

要求图像按照一定的编号序列命名,响应键盘指令:
Esc则退出程序;Enter/Space显示下一帧图像。
优先上框架代码。
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include <opencv2/opencv.hpp>  
  4. using namespace std;  
  5. using namespace cv;  
  6.   
  7. #define row 320  
  8. #define col 640  
  9.   
  10. int _tmain(int argc, _TCHAR* argv[])  
  11. {  
  12.     int nserial = 1;     //< 序列变量  
  13.     int ntotal = 15;     //< 序列总数  
  14.     const char* pcname;  //< 指向文件名称的首地址  
  15.     string strnum;       //< 序列变量寄存器  
  16.     string strname;      //< 文件名称寄存器  
  17.     stringstream ss;     //< 格式转换容器  
  18.   
  19.     // 定义图像寄存器及图像路径  
  20.     Mat image;  
  21.     string strimg1 = "D:\\exam\\img_0000";  
  22.     string strimg2 = ".png";  
  23.       
  24.     Mat dstimg(row, col, CV_8UC3, Scalar(0, 0, 0));  
  25.     namedWindow("Example", WINDOW_AUTOSIZE);  
  26.     imshow("Example", dstimg);  
  27.   
  28.     // 采集键盘指令  
  29.     char ckey = waitKey(0);  
  30.   
  31.     while(1)  
  32.     {  
  33.         // 按下Esc键则退出程序  
  34.         if(ckey == 27)  
  35.         {  
  36.             return 0;  
  37.         }  
  38.   
  39.         // 按下回车或空格显示图像  
  40.         if(ckey == 13 || ckey == 32)  
  41.         {  
  42.   
  43.             if (nserial > ntotal)  
  44.             {  
  45.                 nserial = 1;  
  46.             }  
  47.   
  48.             //int转换为string  
  49.             ss.clear();  
  50.             ss << nserial;  
  51.             ss >> strnum;  
  52.   
  53.             // 序列变量位数变化时,修改名称string  
  54.             if ((nserial >=1)&(nserial <10))  
  55.             {  
  56.                 strimg1 = "D:\\exam\\img_0000";  
  57.             }  
  58.             else if ((nserial >=10)&(nserial < 100))  
  59.             {  
  60.                 strimg1 = "D:\\exam\\img_000";  
  61.             }  
  62.   
  63.             // 图像路径名拼接  
  64.             strname = strimg1 + strnum + strimg2;  
  65.             pcname = strname.data();  
  66.             image = imread(pcname);  
  67.   
  68.             // 更新显示区域  
  69.             imshow("Example", image);  
  70.             nserial++;  
  71.         }  
  72.         // 更新键盘指令  
  73.         ckey = waitKey(0);  
  74.     }// 显示图像while循环结束  
  75.   
  76.      destroyWindow("Example");  
  77.   
  78.     return 0;  
  79. }  
其中需要注意的地方有以下几点:
1、在复制图像文件的路径名称时要注意,有时候盘符名称之前会莫名其妙多出一个不可视的字符,就会造成找不到文件的情况。
2、代码中用到了stringstream类,将任意类型的变量传入给该类的对象后,再由该类的对象传出来,即可获得string类型的数据。既stringstream类用来完成对string类型的转换,如代码所示。
  1.             //int转换为string  
  2.             ss.clear();  
  3.             ss << nserial;  
  4.             ss >> strnum; 
其中的ss.clear()是为了清空ss对象,以便新的变量传入。
3、 int waitKey (int delay=0);

函数只在HgihGUI窗口被创建并活动的时候可用。

它的作用是等待delay毫秒的时间,在此时间内,如果键盘上某一个按键触发消息,则该函数传回这个按键的ASCII码。如果延时时间内没有按键消息被触发,则返回-1.

特殊的,当delay = 0的时候,无限期等待下去。
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值