#include <stdio.h> //header for printf()
#include <stdlib.h> //header for exit() function (used when error occurs)
#include <opencv/cv.h>
#include <opencv/cv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv/highgui.h>
#include <afxdlgs.h> //header of MFC
#include <Windows.h>
using namespace cv;
using namespace std;
int halfwin = 5;
Point pointPOI;
Mat image;
Mat imageRef;
Mat imageNext;
void onMouse(int event, int x, int y, int flags, void* param){
/*printf("(%d, %d) ", x, y);
printf("The Event is: %d ", event);
printf("The flags is: %d ", flags);
printf("The param is: %d\n", param);*/
if(flags == 1){
pointPOI.x = x; pointPOI.y = y;
printf("(x,y)=(%d,%d)\n", pointPOI.x, pointPOI.y);
circle(image, pointPOI,2,0,1,8,0);
imshow("ReferenceImage", image);
}
}
CStringW getFilepath(){
CStringW CstrFolderPath, CstrFileExt, CstrFilePathName;
CFileDialog openfiledlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, NULL);
if (openfiledlg.DoModal() == IDOK){ //顯示開啟檔案視窗
CstrFolderPath = openfiledlg.GetFolderPath();
CstrFileExt = openfiledlg.GetFileExt();
}
CstrFilePathName = CstrFolderPath + "\\*." + CstrFileExt;
return CstrFilePathName; //回傳格式: "C:\123\*.jpg"
}
int main(){
if(!AfxWinInit(::GetModuleHandle(NULL),NULL,::GetCommandLine(),0)){return 1;} //MFC module initialization
CStringW strFolderpath = getFilepath();
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(strFolderpath, &FindFileData); //strFolderpath格式: "C:\123\*.jpg"
CStringA CstrFilepathname;
string strFilepathname;
if(hFind != INVALID_HANDLE_VALUE){
wcout<< FindFileData.cFileName << endl;
CstrFilepathname = FindFileData.cFileName; //利用CStringA轉換成imread可用的string格式
strFilepathname = CstrFilepathname;
}
image = imread(strFilepathname);
imageRef = imread(strFilepathname);
namedWindow("ReferenceImage");
imshow("ReferenceImage", image);
while(true){
cvSetMouseCallback("ReferenceImage", onMouse, NULL);
if (waitKey(1)>=0){break;}
}
printf("pointPOI(x,y) = (%d,%d)\n",pointPOI.x, pointPOI.y);
// main loop
while(true){
if(!FindNextFile(hFind, &FindFileData)){break;}
wcout<< FindFileData.cFileName << endl;
CstrFilepathname = FindFileData.cFileName;
strFilepathname = CstrFilepathname;
imageNext = imread(strFilepathname);
cout << "Image has been read: " << strFilepathname << endl;
Mat imageRefCrop(imageRef, Rect(pointPOI.x-halfwin, pointPOI.y-halfwin, 2*halfwin+1, 2*halfwin+1));
cout << "Image has been cropped: imageRef" << endl;
Mat imageNextCrop(imageNext, Rect(pointPOI.x-2*halfwin, pointPOI.y-2*halfwin, 4*halfwin+1, 4*halfwin+1));
cout << "Image has been cropped: imageNext" << endl;
// do something with the images
//imageRef.release();
imageNext.copyTo(imageRef);
imageNext.release();
}
FindClose(hFind);
system("pause");
return 0;
}