#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;
}
制作眼睛样本
最新推荐文章于 2024-08-24 14:00:00 发布