openCV学习笔记(5):使用sprintf函数实现在窗口连续显示同一文件夹下的图片

本文记录了在VS2010和OpenCV2.3.1环境下,利用sprintf函数动态读取并显示同一文件夹下的一系列图片的过程,详细介绍了sprintf函数的应用。

一、环境:

vs2010 + opencv2.3.1

二、程序:

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

int main()
{
	Mat colorImage;
	//Mat depthImage;
	///////////////////////////////////////////////////////////////////
	///由于需要调用sprintf函数,定义colorImageSrc时必须使用数组的形式
	///////////////////////////////////////////////////////////////////
	//char *colorImageSrc = "I:\\The Graduation Design\\Material\\DepthImage\\rgb\\seq0_0000_1.ppm";
	char colorImageSrc[100]  = "I:\\The Graduation Design\\Material\\DepthImage\\rgb\\seq0_0000_1.ppm";

	//char *depthImageSrc;
	int i = 1;
	//int j = 0;
	//char *dest = "I:\\The Graduation Design\\Material\\DepthImage\\rgb\\seq0_0000_1.ppm";
	char *dest = "I:\\The Graduation Design\\Material\\DepthImage\\rgb\\seq0_%04d_1.ppm";
	
	while(colorImageSrc)//直到显示完所有图片
	{	
		colorImage = imread(colorImageSrc,1);
		if(!colorImage.data)
		{
			cout<<"error"<<endl;
			return -1;
		}
		imshow("colorImage",colorImage);
		/////////////////////////////////////////////
		///使用sprintf函数
		/////////////////////////////////////////////
	    sprintf(colorImageSrc,dest,i);
		cout<<colorImageSrc<<endl;
		i++;
		//此处需要waiKey()
		waitKey(1);
	}

	return 0;
}

三、结果:



四、重点——sprintf()函数

     该函数原型

int sprintf( char *buffer, const char *format, [ argument] … );
参数列表
bufferchar型指针,指向将要写入的字符串的缓冲区, 必须指明该字符串大小
format:格式化字符串。
[argument].. .:可选参数,可以是任何类型的数据。
返回值:字符串长度( strlen
有关该函数的其他说明,本篇文章不作介绍,大家可以百度百科或者查阅其他blog。

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值