OpenCV读取多幅图片,读取系列图片,读取文件夹中指定图像类型的系列图片
2014-11-30 15:32
495 查看
读取系列图片通常是将文件夹中的所有文件名保存在txt中,再读取,如链接和链接所示。这是通常的操作方法。
之前写过一篇利用OpenCV读取系列图片的例子,参见链接,但是,实际应用中并不能改变文件名,本文同样实现读取系列图片集方法,具体如下:
代码实现:
将序列图片显示在一个窗口中,如下:
输出结果显示:
之前写过一篇利用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; }结果如下:
输出结果显示:
相关文章推荐
- opencv 读取目标文件夹里图像并排序放入指定文件夹
- OpenCV学习之例程详解(03):搜索文件夹下所有指定类型图片并逐张显示
- Python实例1-读取指定文件夹下指定类型的图片名,打乱后输出txt
- opencv下指定文件夹下的图片灰度化(图片的读取与保存)
- Opencv读取文件夹连续图片,RGB分量显示,图像灰度化
- Opencv读取文件夹连续图片,RGB分量显示,图像灰度化
- 遍历读取指定文件夹下指定类型的所有文件
- [opencv相关]系列1:opencv支持的图像文件类型及cvLoadImage函数读取/载入图像文件出错原因分析
- opencv 将视频读为图片放在指定文件夹中/将指定文件夹中图片读出并合成视频 cvCreateVideoWriter试分析
- 读取文件夹下指定类型的文件(windows)
- android中写一个内部类来选择文件夹中指定的图片类型实例说明
- 关于读取指定文件夹下批图片的问题
- 遍历读取指定文件夹下指定类型的所有文件
- Java读取文件夹下的指定类型的文件 递归筛选 包含子文件夹
- 【OpenCV】OpenCV中如何批量读取文件夹中图片
- 读取文件夹下指定类型的文件(windows)
- Opencv读取文件夹连续图片,RGB分量显示,图像灰度化
- OpenCV(Emgu)入门系列(8):在C#的PictureBox显示由EmguCV读取的图片
- OpenCV学习笔记---- 挨个读取图像文件夹下的所有文件,进行样本训练
- 如何读取某个指定文件夹下所有文件,并筛选出其中的图片文件(jpeg,bmp)展示出来。