OpenCV搜索文件夹中的图片并保存图片路径和信息

具体如下:

// OpenCVdemo.cpp : Defines the entry point for the console application.
//

#pragma once

// C/C++ includes
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <io.h>
#include <string>

// Set up default call to namespace std
using std::string;

// OpenCV includes
#include <cv.h>
#include <highgui.h>

// Templates for programs
void showImage(const char * name, IplImage * src);
CvScalar getAverageBgrColour(IplImage * rgbImage);
void writeImageDataToFile(IplImage * imageSrc, char * outputFileName, char * fullImagePath);


// Enrtry point for program
int main()
{
	//
	// Set up data to be used in program
	//

	// File finding objects
	struct _finddata_t c_file;
	long hFile;

	// Directory
	char imageDirectory[] = 
		"D:\\Documents\\visual studio 2012\\Projects\\ImageWrapper\\ImageWrapper";

	char metaDataDirectory[] = 
		"D:\\Documents\\visual studio 2012\\Projects\\ImageWrapper\\ImageWrapper\\after";

	// filetype extension to be found
	char imageFileType[] = "jpg";

	// Buffer for using strings
	char fullImagePath[500];
	char fullMetaDataPath[500];
	char buffer[500];

	// 
	// Prepare program
	// 

	// find files of this type (e.g., find all *.jpg files in imageDirectory)
	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( "Opening File: %s \n", c_file.name);

			// XML file name (same as input filename but with XML)
			string metaDataName(c_file.name);				// create string
			int endString = metaDataName.find(imageFileType);	// find location of image extension
			metaDataName.erase(endString);					// remove end of string
			metaDataName += "xml";							// append "xml"

			// Append directory for full path name and metadata name
			sprintf(fullImagePath,"%s\\%s", imageDirectory, c_file.name);
			sprintf(fullMetaDataPath, "%s\\%s", metaDataDirectory ,metaDataName.c_str());

			// Load image
			IplImage * imageTest = cvLoadImage(fullImagePath);

			// Check to see if image is valid
			if (imageTest) 
			{
				// Do Stuff here
				// ....
				// ....

				// Write data to file
				writeImageDataToFile(imageTest, fullMetaDataPath,fullImagePath);

				// Example show image
				showImage("test",imageTest);

				// Pause program for 10 milliseconds
				// (so you can see your image)
				cvWaitKey(10);

				// Release image data
				cvReleaseImage(&imageTest);
			}
			else
			{
				printf("Invalid image \n");
			}

			// keep finding files until no more found, i.e. until there is an error code
		} while( _findnext( hFile, &c_file ) == 0 ); 

		// Close file finder object
		_findclose( hFile );
	}

	return 0;
}

// Simple show image command
void showImage(const char * name, IplImage * src)
{
	cvNamedWindow(name);
	cvShowImage(name,src);
}

// Get average BGR colour (as 
CvScalar getAverageBgrColour(IplImage * bgrImage)
{
	IplImage * rImage = cvCreateImage(cvGetSize(bgrImage),bgrImage->depth,1);
	IplImage * gImage = cvCreateImage(cvGetSize(bgrImage),bgrImage->depth,1);
	IplImage * bImage = cvCreateImage(cvGetSize(bgrImage),bgrImage->depth,1);

	// Split Image
	cvSplit(bgrImage,bImage,gImage,rImage,NULL);

	CvScalar outputData;

	outputData.val[0] = cvMean(bImage);
	outputData.val[1] = cvMean(gImage);
	outputData.val[2] = cvMean(rImage);

	cvReleaseImage(&rImage);
	cvReleaseImage(&bImage);
	cvReleaseImage(&gImage);

	return outputData;
}

void writeImageDataToFile(IplImage * imageSrc, char * outputFileName, char * fullImagePath)
{

	// Sample test data
	CvScalar testData = getAverageBgrColour(imageSrc);

	// Open meta data XML file for storage
	CvFileStorage * fileStorage = cvOpenFileStorage(
		outputFileName,
		0,
		CV_STORAGE_WRITE);

	// Check if filestorage is valid
	if (fileStorage)
	{
		// Write the full location of the original image to
		// an xml tag called "image_location" as a string
		cvWriteString(fileStorage,"image_location",fullImagePath);

		// Write the colours with named tags
		cvWriteReal(fileStorage,"blue_value", testData.val[0]);
		cvWriteReal(fileStorage,"green_value", testData.val[1]);
		cvWriteReal(fileStorage,"red_value", testData.val[2]);

		// Open up a "tree" structure called "overall_colour"
		cvStartWriteStruct(fileStorage,"overall_colour",CV_NODE_MAP);
		// Write the colours with names under this structure
		cvWriteReal(fileStorage,"blue_value", testData.val[0]);
		cvWriteReal(fileStorage,"green_value", testData.val[1]);
		cvWriteReal(fileStorage,"red_value", testData.val[2]);
		// End the strucutre
		cvEndWriteStruct(fileStorage);

		// Start a sequence structure called BGRcolour
		cvStartWriteStruct(fileStorage,"BGRcolour",CV_NODE_SEQ);
		cvWriteReal(fileStorage,NULL, testData.val[0]);
		cvWriteReal(fileStorage,NULL, testData.val[1]);
		cvWriteReal(fileStorage,NULL, testData.val[2]);
		cvEndWriteStruct(fileStorage);

		// Close the file
		cvReleaseFileStorage(&fileStorage);
	}
	else
		printf("Invalid xml file \n");
}

读取xml中的信息

// OpenCVdemo.cpp : Defines the entry point for the console application.
//

#pragma once

// C/C++ includes
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <io.h>
#include <string>

// Set up default call to namespace std
using std::string;

// OpenCV includes
#include <cv.h>
#include <highgui.h>

// Custom includes
//#include "imageWrapper.hpp"

void showImage(const char * name, IplImage * src);
void readImageDataFromFile(char * inputFileName);

// Simple show image command
void showImage(const char * name, IplImage * src)
{
	cvNamedWindow(name);
	cvShowImage(name,src);
}

// Read data from xml file
void readImageDataFromFile(char * inputFileName)
{

	// Open meta data XML file for reading
	CvFileStorage * fileStorage = cvOpenFileStorage(
		inputFileName,
		0,
		CV_STORAGE_READ);

	// Check if filestorage is valid
	if (fileStorage)
	{
		// Set up some variables to store the values
		// And retrieve from the xml file
		double blue =
			cvReadRealByName(fileStorage,NULL,"blue_value");
		double red =
			cvReadRealByName(fileStorage,NULL,"red_value");
		double green =
			cvReadRealByName(fileStorage,NULL,"green_value");
		const char * buffer = 
			cvReadStringByName(fileStorage,NULL,"image_location");

		// Print out the data to screen
		printf("Location: %s \n Red: %f, Green: %f, Blue: %f \n",
			buffer,red,green,blue);

		// Release filestorage
		cvReleaseFileStorage(&fileStorage);
	}
	else
		printf("Invalid xml file \n");
}

// 
int main()
{
	//
	// Set up data to be used in program
	//

	// File finding objects
	struct _finddata_t c_file;
	long hFile;

	// Directory to read from
	char metaDataDirectory[] = "H:\\metaData";

	// filetype extension
	char imageFileType[] = "xml";

	// Buffer for using strings
	char fullImagePath[500];
	char fullMetaDataPath[500];
	char buffer[500];

	// 
	// Prepare program
	// 

	// find files of this type
	sprintf(buffer,"%s\\*.%s", metaDataDirectory, 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( "Opening File: %s \n", c_file.name);
	
			// Append directory for full path name and metadata name
			sprintf(fullMetaDataPath, "%s\\%s", metaDataDirectory, c_file.name);

			// Write data to file
			readImageDataFromFile(fullMetaDataPath);

			// keep finding files until no more found, i.e. until there is an error code
		} while( _findnext( hFile, &c_file ) == 0 ); 

		// Close file finder object
		_findclose( hFile );
	}
	
	system("pause");

	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值