OpenCV读取多幅图片,读取系列图片,读取文件夹中指定图像类型的系列图片

读取系列图片通常是将文件夹中的所有文件名保存在txt中,再读取,如链接链接所示。这是通常的操作方法。

之前写过一篇利用OpenCV读取系列图片的例子,参见链接,但是,实际应用中并不能改变文件名,本文同样实现读取系列图片集方法,具体如下:

代码实现:

#include <io.h>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
	//IplImage *desimg,*srcimg;
	Mat desimg,srcimg;
	Mat output,src,tumbnail1;
	//load multiple images
	//File finding objects
	struct _finddata_t c_file;

	long hFile;
	char imageDirectory[] = "D:\\Documents\\visual studio 2012\\Projects\\read_multiple_images\\read_multiple_images";
	char imageFileType[] = "jpg";
	char fullImagePath[1000];
	char buffer[1000];  
	char str[100]; 
	int i = 0;
	sprintf(buffer,"%s\\*.%s", imageDirectory, imageFileType);
	hFile = _findfirst( buffer, &c_file );
	/*Check to make sure that there are files in directory*/
	if( hFile == -1L )
		printf( "No %s files in current directory!\n", imageFileType );
	else
	{   
		// List all files in directory
		printf( "Listing of files:\n" );
		// Loop through all images of imageFileType
		do  
		{   
			itoa(i+1,str,10); 
			string s1(str);   
			// Show file name
			printf( "\nOpening File: %s \n", c_file.name);
			sprintf(fullImagePath,"%s\\%s", imageDirectory, c_file.name);
			// Load image
			//desimg = cvLoadImage(fullImagePath);
			desimg = imread(fullImagePath);
			imshow(s1,desimg);
			i++;

		} while( _findnext( hFile, &c_file ) == 0 );

		// Close file finder object
		_findclose( hFile );
	}
	waitKey(0);
	return 0;
}
结果如下:



将序列图片显示在一个窗口中,如下:

/*
#include <iostream>
#include <opencv2/opencv.hpp>
#include <io.h>
using namespace cv;
using namespace std;

int main()
{
	//IplImage *desimg,*srcimg;
	Mat desimg,srcimg;
	Mat output,src,tumbnail1;
	srcimg=cvLoadImage("panorama_image1.jpg",1);
	output=cvCreateMat(srcimg.cols,srcimg.rows,3);
	//load multiple images
	//File finding objects
	struct _finddata_t c_file;

	long hFile;
	char imageDirectory[] = "D:\\Documents\\visual studio 2012\\Projects\\read_multiple_images\\read_multiple_images";
	char imageFileType[] = "jpg";
	char fullImagePath[1000];
	char buffer[1000];  
	sprintf(buffer,"%s\\*.%s", imageDirectory, imageFileType);
	hFile = _findfirst( buffer, &c_file );
	//*Check to make sure that there are files in directory
	if( hFile == -1L )
		printf( "No %s files in current directory!\n", imageFileType );
	else
	{   
		// List all files in directory
		printf( "Listing of files:\n" );
		// Loop through all images of imageFileType
		do  
		{   
			// Show file name
			printf( "\nOpening File: %s \n", c_file.name);
			sprintf(fullImagePath,"%s\\%s", imageDirectory, c_file.name);
			// Load image
			//desimg = cvLoadImage(fullImagePath);
			desimg = imread(fullImagePath);
			Mat src1 = desimg;//=cv::cvarrToMat(desimg); //convert ipl img to mat
			Mat src2= srcimg;//cv::cvarrToMat(srcimg);

			cv::Mat tumbnails2;

			tumbnail1=cvCreateMat(src1.rows/2,src1.cols/2,3);
			tumbnails2=cvCreateMat(src2.rows/2,src2.cols/2,3);

			cv::resize(src1, tumbnails2,tumbnails2.size());
			cv::resize(src2, tumbnail1,tumbnail1.size());
			vconcat(tumbnail1,tumbnails2,output);

		} while( _findnext( hFile, &c_file ) == 0 );

		// Close file finder object
		_findclose( hFile );
	}
	imshow("concatenation of multiple images",output);
	waitKey(0);
	return 0;
}
*/
#include <io.h>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
cv::Mat createOne(std::vector<cv::Mat> & images, int cols, int min_gap_size);
int main()
{
	//IplImage *desimg,*srcimg;
	Mat desimg,srcimg;
	Mat output;
	std::vector<cv::Mat> my_images;
	//load multiple images
	//File finding objects
	struct _finddata_t c_file;

	long hFile;
	char imageDirectory[] = "D:\\Documents\\visual studio 2012\\Projects\\read_multiple_images\\read_multiple_images";
	char imageFileType[] = "jpg";
	char fullImagePath[1000];
	char buffer[1000];  
	char str[100]; 
	int i = 0;
	sprintf(buffer,"%s\\*.%s", imageDirectory, imageFileType);
	hFile = _findfirst( buffer, &c_file );
	/*Check to make sure that there are files in directory*/
	if( hFile == -1L )
		printf( "No %s files in current directory!\n", imageFileType );
	else
	{   
		// List all files in directory
		printf( "Listing of files:\n" );
		// Loop through all images of imageFileType
		do  
		{   
			itoa(i+1,str,10); 
			string s1(str);   
			// Show file name
			printf( "\nOpening File: %s \n", c_file.name);
			sprintf(fullImagePath,"%s\\%s", imageDirectory, c_file.name);
			// Load image
			//desimg = cvLoadImage(fullImagePath);
			desimg = imread(fullImagePath);
			Mat sized_dst = cv::Mat(Size(desimg.cols/2,desimg.rows/2),desimg.type());
			resize(desimg,sized_dst,sized_dst.size());
			imshow(s1,sized_dst);
			my_images.push_back(sized_dst);
			i++;

		} while( _findnext( hFile, &c_file ) == 0 );
		//cout<<"i = "<<i<<endl;
		output = createOne(my_images,1,5);
		imshow("output",output);
		imwrite("output.jpg",output);
		// Close file finder object
		_findclose( hFile );
	}
	waitKey(0);
	return 0;
}

cv::Mat createOne(std::vector<cv::Mat> & images, int cols, int min_gap_size)
{
	// let's first find out the maximum dimensions
	int max_width = 0;
	int max_height = 0;
	for ( int i = 0; i < images.size(); i++) {
		// check if type is correct 
		// you could actually remove that check and convert the image 
		// in question to a specific type
		if ( i > 0 && images[i].type() != images[i-1].type() ) {
			std::cerr << "WARNING:createOne failed, different types of images";
			return cv::Mat();
		}
		max_height = std::max(max_height, images[i].rows);
		max_width = std::max(max_width, images[i].cols);
	}
	// number of images in y direction
	int rows = std::ceil(images.size() / cols);

	// create our result-matrix
	cv::Mat result = cv::Mat::zeros(rows*max_height + (rows-1)*min_gap_size,
		cols*max_width + (cols-1)*min_gap_size, images[0].type());
	size_t i = 0;
	int current_height = 0;
	int current_width = 0;
	for ( int y = 0; y < rows; y++ ) {
		for ( int x = 0; x < cols; x++ ) {
			if ( i >= images.size() ) // shouldn't happen, but let's be safe
				return result;
			// get the ROI in our result-image
			cv::Mat to(result,
				cv::Range(current_height, current_height + images[i].rows),
				cv::Range(current_width, current_width + images[i].cols));
			// copy the current image to the ROI
			images[i++].copyTo(to);
			current_width += max_width + min_gap_size;
		}
		// next line - reset width and update height
		current_width = 0;
		current_height += max_height + min_gap_size;
	}
	return result;
}
结果如下:

输出结果显示:




  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值