您的位置:首页 > 运维架构

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

2014-11-30 15:32 495 查看
读取系列图片通常是将文件夹中的所有文件名保存在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;
}
结果如下:



输出结果显示:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐