#include <windows.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
using namespace std;
bool Drawing=false;//標記鼠標響應
IplImage *temp;//存放在鼠標響應後的圖
IplImage *src;//存放原圖
vector<CvRect> boxVector;//存放在圖上框出的框
CvRect box;//每一個框,也可以不用全局變量
// 深度優先遞歸遍歷目錄中所有的文件
BOOL DirectoryList(const string &Path,vector<string> &file_lists,vector<string> &file_names)
{
WIN32_FIND_DATA FindData;
HANDLE hError;
int FileCount = 0;
string FilePathName;
// 構造路徑
FilePathName=Path;
FilePathName=FilePathName+ "\\*.*";//取文件下所有文件
hError = FindFirstFile(FilePathName.c_str(), &FindData);
if (hError == INVALID_HANDLE_VALUE)
{
printf("搜索失敗!");
return 0;
}
while(::FindNextFile(hError, &FindData))
{
string FullPathName;
// 過慮.和..
if (strcmp(FindData.cFileName, ".") == 0
|| strcmp(FindData.cFileName, "..") == 0 )
{
continue;
}
// 構造完整路徑
FullPathName=Path+FindData.cFileName;
FileCount++;
file_lists.push_back(FullPathName);
file_names.push_back(FindData.cFileName);
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
printf("<Dir>");
DirectoryList(FullPathName,file_lists,file_names);
}
}
return 0;
}
void Mouse_Call_Back(int event,int x,int y,int flags,void *param)
{
if(event==CV_EVENT_LBUTTONDOWN)
{
box.x=x;
box.y=y;
box.width=0;
box.height=0;
Drawing=true;//按下左鍵畫圖標誌記爲真
}
else if(event==CV_EVENT_MOUSEMOVE)
{
if(Drawing==true)
{
cvCopy(src,temp);
box.width=abs(x-box.x);
box.height=abs(y-box.y);
CvPoint pt1,pt2;
pt1.x=box.x;
pt1.y=box.y;
pt2.x=x;
pt2.y=y;
cvDrawRect(temp,pt1,pt2,cvScalar(0,255,0));
cvShowImage("pic",temp);
}
}
else if(event==CV_EVENT_LBUTTONUP)
{
if((box.width>0)&&(box.height>0))
{
boxVector.push_back(box);
}
Drawing=false;
}
}
int main()
{
IplImage *dst;//摳的圖
vector<string> file_lists;
vector<string> file_names;
ifstream OrigImgPath;
ifstream EyePosPath;
ifstream NewImgPath;
ofstream eyePos;//存放眼睛座標的txt文件
string OIPath;//存放原圖的文件夾
string NIPath;
string EPPath;
OrigImgPath.open("OrigImgPath.txt");//原圖文件夾
OrigImgPath>>OIPath;
OrigImgPath.close();
EyePosPath.open("EyePositionPath.txt");
EyePosPath>>EPPath;
EyePosPath.close();
NewImgPath.open("NewImgPath.txt");
NewImgPath>>NIPath;
NewImgPath.close();
DirectoryList(OIPath,file_lists,file_names);
int len=file_lists.size();
cout<<"共有"<<len<<"張圖片!"<<endl;
for(size_t i=0;i<file_lists.size();i++)
{
string picName(file_names[i],0,file_names[i].size()-4);
string EyePos;
EyePos=EPPath+picName+".txt";
eyePos.open(EyePos.c_str());
src = cvLoadImage(file_lists[i].c_str(),1);
temp=cvCloneImage(src);
cvNamedWindow("pic");
cvShowImage("pic",src);
cout<<"處理第"<<i<<"張圖..."<<endl;
cvSetMouseCallback("pic",Mouse_Call_Back,0);
int key;
while(1)
{
key=cvWaitKey(0);
if(key==27)
{
cvDestroyWindow("pic");
cvReleaseImage(&src);
cvReleaseImage(&temp);
eyePos.close();
return 0;
}
break;
}
for(int j=0;j<boxVector.size();j++)
{
eyePos << boxVector[j].x<< " " << boxVector[j].y << " "<< boxVector[j].width << " "<< boxVector[j].height << endl;
cvSetImageROI(src,boxVector[j]);
dst=cvCreateImage(cvSize(boxVector[j].width,boxVector[j].height),src->depth,src->nChannels);
dst=cvCloneImage(src);
char IndexN[5];
itoa(j,IndexN,10);
string smallImage=picName+"_"+IndexN+".jpg";
string savePath;
savePath=NIPath+smallImage;//新圖像的絕對路徑加文件名
cvSaveImage(savePath.c_str(),dst);
cvResetImageROI(src);
cvReleaseImage(&dst);
}
while(!boxVector.empty())
{
boxVector.pop_back();
}
eyePos.close();
}
cvDestroyWindow("pic");
cvReleaseImage(&src);
cvReleaseImage(&temp);
cout<<"圖片處理結束!"<<endl;
return 0;
}
製作眼睛樣本
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.