#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <math.h>
#include "windows.h"
#include <vector>
#include <string>
#include "iostream"
using namespace std;
typedef vector<string> file_lists;
template<typename T>T sqr(T x){return x*x;};
CvPoint Point;
bool Drawing=false;
static int str_compare(const void *arg1, const void *arg2)
{
return strcmp((*(std::string*)arg1).c_str(), (*(std::string*)arg2).c_str());//比较字符串arg1 and arg2
}
file_lists ScanDirectory(const std::string &path, const std::string &extension)
{
WIN32_FIND_DATA wfd;//WIN32_FIND_DATA:Contains information about the file that is found by the
//FindFirstFile, FindFirstFileEx, or FindNextFile function
HANDLE hHandle;
string searchPath, searchFile;
file_lists vFilenames;
int nbFiles = 0;
searchPath = path + "/*" + extension;
hHandle = FindFirstFile(searchPath.c_str(), &wfd);//Searches a directory for a file or subdirectory
//with a name that matches a specific name
if (INVALID_HANDLE_VALUE == hHandle)
{
fprintf(stderr, "ERROR(%s, %d): Cannot find (*.%s)files in directory %s/n",
__FILE__, __LINE__, extension.c_str(), path.c_str());
exit(0);
}
do
{
//. or ..
if (wfd.cFileName[0] == '.')
{
continue;
}
// if exists sub-directory
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)//dwFileAttributes:The file attributes of a file
{
//FILE_ATTRIBUTE_DIRECTORY:The handle identifies a directory
continue;
}
else//if file
{
searchFile = path + "/" + wfd.cFileName;
vFilenames.push_back(searchFile);
nbFiles++;
}
}while (FindNextFile(hHandle, &wfd));//Call this member function to continue a file search begun
//with a call to CGopherFileFind::FindFile
FindClose(hHandle);//Closes a file search handle opened by the FindFirstFile, FindFirstFileEx,
//or FindFirstStreamW function
// sort the filenames
qsort((void *)&(vFilenames[0]), (size_t)nbFiles, sizeof(string), str_compare);//Performs a quick sort
return vFilenames;
}
void onMouse(int event,int x,int y,int flags,void* param );
void Draw(IplImage* img0,CvPoint point)
{
for(int r=0;r<img0->height;r++)
{
for(int c=0;c<img0->width;c++)
{
double s=cvGetReal2D(img0,r,c);
double d=(((double)sqr(r-point.y)+sqr(c-point.x)));
s=s*(exp((250*250-2*d)/(250*250)));
//d=exp(-d/(250*250));
cvSetReal2D(img0,r,c,s);
}
}
cvNormalize(img0,img0,0,1,CV_MINMAX);
}
int _tmain(int argc, _TCHAR* argv[])
{
Point=cvPoint(0,0);
time_t start,end,time; /*注意计时所用的变量名称*/
/*程序开始执行,开始计时*/
start=clock();
file_lists files = ScanDirectory("D:/HC", ".png");
if (files.empty())
{
cout<<"no image file find in current directory.."<<endl;
system("pause");
exit(-1);
}
int size = files.size();
cout<<"there are "<<size<<" image files totally...."<<endl;
for (int i=0; i<size; i++)
{
IplImage* img = cvLoadImage(files[i].c_str(),-1);
IplImage *img0=cvCreateImage(cvGetSize(img),IPL_DEPTH_32F,1);
cvConvertScale(img,img0,1.0/255);
cvNamedWindow("Webcam",1);
cvSetMouseCallback("Webcam",onMouse,(void*)img0);
//cvZero(img0);
IplImage* temp=cvCloneImage(img0);
while(true)
{
cvCopyImage(img0,temp);
// if(Drawing)
// Draw(temp,Point);
cvShowImage("Webcam",temp);
if(cvWaitKey(15)==27)
break;
}
//cvShowImage("Webcam",img0);
//cvWaitKey(0);
cvConvertScale(img0,img,255);
string a="D:/test/out/";
string b;
b.assign(files[i],6,8);
a.append(b);
a.append("_RC.png");
cvSaveImage(a.c_str(),img);
cout<<files[i].c_str()<<i<<endl;
}
//cvNamedWindow("outwindow", CV_WINDOW_AUTOSIZE);
//cvShowImage("outwindow", result);
//cvSaveImage("D:/image/test_meanshift/put/1.jpg",result);
end=clock();
time=end-start;//这里的时间是计算机内部时间
cout << endl << "time:" << time << endl;
cvWaitKey(0);
return 0;
}
void onMouse(int event,int x,int y,int flags,void* param )
{
IplImage*img=(IplImage*)param;
switch(event)
{
case CV_EVENT_LBUTTONUP:
{
Point=cvPoint(x,y);
}
Draw(img,Point);
break;
}
printf("( %d, %d) ",x,y);
printf("The Event is : %d ",event);
printf("The flags is : %d ",flags);
printf("The param is : %d\n",param);
}