📄 picture trainingdlg.cpp
字号:
CFileFind finder;
// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*");
// start working for files
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
continue;
// if it's a directory, recursively search it
if (finder.IsDirectory())
{
CString str = finder.GetFilePath();
FindFileNumRecurse(str);
}
if (!finder.IsDots()&&!finder.IsDots()) {
CString SingleName=finder.GetFileName();
if(SingleName.Find(".jpg")!=-1||SingleName.Find(".bmp")!=-1||SingleName.Find(".png")!=-1)
FileNum++;
}
}
finder.Close();
}
//////////////////////////////////////////////////////////////////////////
//将文件名数组建立,用递归的方法
VOID CPictureTrainingDlg::CreateFileNameRecurse(CString pstr)
{
CFileFind finder;
// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*");
// start working for files
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
continue;
// if it's a directory, recursively search it
if (finder.IsDirectory())
{
CString str = finder.GetFilePath();
CreateFileNameRecurse(str);
}
if (!finder.IsDots()&&!finder.IsDots()) {
CString SingleName=finder.GetFileName();
if(SingleName.Find(".jpg")!=-1||SingleName.Find(".bmp")!=-1||SingleName.Find(".png")!=-1)
{ FilePathName[CurrentFileNum]=finder.GetFilePath();
CurrentFileNum++;
}
}
}
finder.Close();
}
void CPictureTrainingDlg::OnBUTTONNext()
{
// TODO: Add your control notification handler code here
BoundaryPoint[0]=BoundaryPoint[1]=(0,0);
CurrentDealNum++;
if(IsWorking)
{
if (CurrentImg) {
CurrentImg->ImageClear();
CurrentImg=NULL;
}
if (FilePathName!=NULL) {
CurrentImg=new CImage;
CurrentImg->ReadFromFile(FilePathName[CurrentDealNum%FileNum]);
}
m_Cuurent_Deal_Number=CurrentDealNum%FileNum+1;
UpdateData(FALSE);
Invalidate(TRUE);
}
}
void CPictureTrainingDlg::OnBUTTONPrevious()
{
// TODO: Add your control notification handler code here
BoundaryPoint[0]=BoundaryPoint[1]=(0,0);
CurrentDealNum--;
if (IsWorking) {
if (CurrentImg) {
CurrentImg->ImageClear();
CurrentImg=NULL;
}
if (FilePathName!=NULL) {
CurrentImg=new CImage;
CurrentImg->ReadFromFile(FilePathName[CurrentDealNum%FileNum]);
}
m_Cuurent_Deal_Number=CurrentDealNum%FileNum+1;
UpdateData(FALSE);
Invalidate(TRUE);
}
}
void CPictureTrainingDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (point.x>20&&point.y>20&&point.x<490&&point.y<510) {
IsInRegion=TRUE;
LosingCapture=FALSE; //不用刷新背景
if (IsLocating==TRUE&&IsReady!=2) {
BoundaryPoint[1].y=point.y;
BoundaryPoint[1].x=(long)(BoundaryPoint[0].x+((double)point.y-BoundaryPoint[0].y)*(double)TargetWidth/(double)TargetHeight*470/490.0*CurrentImg->Height()/CurrentImg->Width());
InvalidateRect(PicRegion,TRUE);
}
}
else
{IsInRegion=FALSE;
LosingCapture=TRUE; }
CDialog::OnMouseMove(nFlags, point);
}
void CPictureTrainingDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (IsWorking&&IsInRegion&&IsReady!=2) {
BoundaryPoint[0]=BoundaryPoint[1]=point;
IsLocating=TRUE;
//LoadCursor(AfxGetInstanceHandle( ),"IDC_HAND");
}
InvalidateRect(PicRegion,TRUE);
CDialog::OnLButtonDown(nFlags, point);
}
void CPictureTrainingDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (IsWorking&&IsInRegion&&IsReady!=2) {
IsReady=1;
BoundaryPoint[1].y=point.y;
BoundaryPoint[1].x=(long)(BoundaryPoint[0].x+((double)point.y-BoundaryPoint[0].y)*(double)TargetWidth/(double)TargetHeight*470/490.0*CurrentImg->Height()/CurrentImg->Width());
// LoadCursor(AfxGetInstanceHandle( ),"IDC_HAND");
}
if (IsReady==2) {
ScreenCenter=point;
}
IsLocating=FALSE;
InvalidateRect(PicRegion,TRUE);
CDialog::OnLButtonUp(nFlags, point);
}
BOOL CPictureTrainingDlg::OnEraseBkgnd(CDC* pDC)
{
if (LosingCapture) {
CBrush backBrush(RGB(192,192,192));
CBrush*pOldBrush=pDC->SelectObject(&backBrush);
CRect rect;
pDC->GetClipBox(&rect);
pDC->PatBlt(rect.left,rect.top,rect.Width(),rect.Height(),PATCOPY);
pDC->SelectObject(pOldBrush);
}
return TRUE;
}
void CPictureTrainingDlg::OnKillFocus(CWnd* pNewWnd)
{
CDialog::OnKillFocus(pNewWnd);
// TODO: Add your message handler code here
LosingCapture=TRUE;
}
void CPictureTrainingDlg::OnCaptureChanged(CWnd *pWnd)
{
// TODO: Add your message handler code here
LosingCapture=TRUE;
CDialog::OnCaptureChanged(pWnd);
}
void CPictureTrainingDlg::OnButtonWriteToFile()
{
// TODO: Add your control notification handler code here
IsAdd=TRUE;
DealWithImage();
//计算图像的中心点
ImgCenter.x=long((ScreenCenter.x-BoundaryPoint[0].x)/(double)(BoundaryPoint[1].x-BoundaryPoint[0].x)*(double)TargetWidth);
ImgCenter.y=long((ScreenCenter.y-BoundaryPoint[0].y)/(double)(BoundaryPoint[1].y-BoundaryPoint[0].y)*(double)TargetHeight);
if (ImgCenter.x>TargetWidth||ImgCenter.y>TargetHeight) {
MessageBox("中心点越界,请重新选取!");
return;
}
CStdioFile mFileW;
CString CSTotalNumber;
CString CSWidth,CSHeight,CSCenterX,CSCenterY;
CSWidth.Format("%d",TargetWidth);
CSHeight.Format("%d",TargetHeight);
CSCenterX.Format("%d",ImgCenter.x);
CSCenterY.Format("%d",ImgCenter.y);
TotalSample+=2;
mFileW.Open(_T(m_ResultsFolder+"\\Label.inf "),CFile::modeWrite);
CSTotalNumber.Format("%d",TotalSample);
mFileW.SeekToBegin();
mFileW.WriteString("The Number of the Train Set is:(Filename\\Width\\Height\\CenterX\\CnterY)\n");
mFileW.WriteString(CSTotalNumber+"\n");
mFileW.SeekToEnd();
CSTotalNumber.Format("%d",TotalSample-1);
mFileW.WriteString(CSTotalNumber+".bmp"+"\n");
mFileW.WriteString(CSWidth+"\n");
mFileW.WriteString(CSHeight+"\n");
mFileW.WriteString(CSCenterX+"\n");
mFileW.WriteString(CSCenterY+"\n");
SaveImg->WriteToFile(m_ResultsFolder+"\\"+CSTotalNumber+".bmp"); //保存图像
//把图像左右反转,再写一遍
CSWidth.Format("%d",TargetWidth);
CSHeight.Format("%d",TargetHeight);
CSCenterX.Format("%d",TargetWidth-ImgCenter.x);
CSCenterY.Format("%d",ImgCenter.y);
CSTotalNumber.Format("%d",TotalSample);
mFileW.WriteString(CSTotalNumber+".bmp"+"\n");
mFileW.WriteString(CSWidth+"\n");
mFileW.WriteString(CSHeight+"\n");
mFileW.WriteString(CSCenterX+"\n");
mFileW.WriteString(CSCenterY+"\n");
mFileW.Close();
CImage Mirror(*SaveImg);
for(int i=0;i<SaveImg->Bits()/8;i++)
for(int j=0;j<SaveImg->Height();j++)
for(int k=0;k<SaveImg->Width();k++)
Mirror.Data()[(j*SaveImg->Width()+k)*SaveImg->Bits()/8+i]=SaveImg->Data()[(j*SaveImg->Width()+SaveImg->Width()-k-1)*SaveImg->Bits()/8+i];
Mirror.WriteToFile(m_ResultsFolder+"\\"+CSTotalNumber+".bmp"); //保存图像
}
void CPictureTrainingDlg::DealWithImage()
{
short Depth;
int width,height;
width=CurrentImg->Width();
height=CurrentImg->Height();
Depth=CurrentImg->Bits()/8;
long NormalizeSizeX,NormalizeSizeY;
SelectSize.x=BoundaryPoint[1].x-BoundaryPoint[0].x;
SelectSize.y=BoundaryPoint[1].y-BoundaryPoint[0].y; //图片选择的区域
AspectRatioX=CurrentImg->Width()/470.0;
AspectRatioY=CurrentImg->Height()/490.0; //实际图片显示或者
CPoint ActualBoudary[2];
ActualBoudary[0].x=long(AspectRatioX*(BoundaryPoint[0].x-20));
ActualBoudary[1].x=long(AspectRatioX*(BoundaryPoint[1].x-20));
ActualBoudary[0].y=long(AspectRatioY*(BoundaryPoint[0].y-20));
ActualBoudary[1].y=long(AspectRatioY*(BoundaryPoint[1].y-20));
RealSize.x=(long)(SelectSize.x*AspectRatioX);
RealSize.y=(long)(SelectSize.y*AspectRatioY);
if (IsNormalize) { //应该归一化的图像的大小
NormalizeSizeX=TargetWidth;
NormalizeSizeY=TargetHeight;
}
else
{
NormalizeSizeX=RealSize.x;
NormalizeSizeY=RealSize.y;
}
if (SaveImg) {
SaveImg->ImageClear();
SaveImg=NULL;
}
CSize NormalizeSize;
NormalizeSize.cx=NormalizeSizeX;
NormalizeSize.cy=NormalizeSizeY;
if (CurrentImg->Bits()==8) {
SaveImg=new CImage(NormalizeSize,256,Depth*8); //保存成黑白图像
}
else
SaveImg=new CImage(NormalizeSize,0,Depth*8); ; //保存成真彩色图像
double InterAspectX,InterAspectY;
InterAspectX=(double)RealSize.x/NormalizeSizeX; //插值的比例
InterAspectY=(double)RealSize.y/NormalizeSizeY;
int i,j,k;
double w1,w2,w3,w4; //采用双线性插值的系数
double p1,p2,p3,p4; //四个像素点
double DoubleX,DoubleY; //实数距离
CPoint Abs1,Abs2,Abs3,Abs4;
for(k=0;k<Depth;k++)
for(j=0;j<NormalizeSizeY;j++)
for(i=0;i<NormalizeSizeX;i++)
{
DoubleX=(double)(i*InterAspectX);
DoubleY=(double)(j*InterAspectY);
w1=DoubleX-floor(DoubleX);
w2=floor(DoubleX)+1-DoubleX;
w3=DoubleY-floor(DoubleY);
w4=floor(DoubleY)+1-DoubleY;
Abs1.x=long(floor(DoubleX)+ActualBoudary[0].x);
Abs1.y=long(floor(DoubleY)+ActualBoudary[0].y);
Abs2.x=long(floor(DoubleX)+1+ActualBoudary[0].x);
Abs2.y=long(floor(DoubleY)+ActualBoudary[0].y);
Abs3.x=long(floor(DoubleX)+ActualBoudary[0].x);
Abs3.y=long(floor(DoubleY)+1+ActualBoudary[0].y);
Abs4.x=long(floor(DoubleX)+1+ActualBoudary[0].x);
Abs4.y=long(floor(DoubleY)+1+ActualBoudary[0].y);
p1=CurrentImg->Data()[(Abs1.y*width+Abs1.x)*Depth+k];
p2=CurrentImg->Data()[(Abs2.y*width+Abs2.x)*Depth+k];
p3=CurrentImg->Data()[(Abs3.y*width+Abs3.x)*Depth+k];
p4=CurrentImg->Data()[(Abs4.y*width+Abs4.x)*Depth+k];
SaveImg->Data()[long(j*NormalizeSizeX+i)*Depth+k]=
(BYTE)(w4/(w3+w4)*(p1*w2/(w1+w2)+p2*w1/(w1+w2))+w3/(w3+w4)*(p3*w2/(w1+w2)+p4*w1/(w1+w2)));
}
CurrentImg->Dib();
SaveImg->Dib();
}
void CPictureTrainingDlg::OnButtonPoint()
{
// TODO: Add your control notification handler code here
IsReady=2; //已经处于点点的状态了
}
void CPictureTrainingDlg::OnBUTTONConfirm()
{
// TODO: Add your control notification handler code here
IsReady=0;
}
void CPictureTrainingDlg::OnBUTTONEraseLast()
{
// TODO: Add your control notification handler code here
if (!IsAdd) {
MessageBox("还没有添加过文件!");
return;
}
CStdioFile mFileR;
mFileR.Open(_T(m_ResultsFolder+"\\Label.inf "),CFile::modeRead);
CString info,CSTotal,*CSbuffer;
int total,i;
//取得当前一共有多少条目
mFileR.ReadString(info);
mFileR.ReadString(CSTotal);
total=atoi(CSTotal);
CStdioFile mFileDeleteBmp;
mFileDeleteBmp.Remove(m_ResultsFolder+"\\"+CSTotal+".bmp");
//mFileDeleteBmp.Close();
CSbuffer=new CString[total*5];
for(i=0;i<total*5;i++) //读出来
{
mFileR.ReadString(CSbuffer[i]);
}
mFileR.Close();
total--;
CSTotal.Format("%d",total);
CStdioFile mFileDelete;
mFileDelete.Remove(_T(m_ResultsFolder+"\\Label.inf "));
// mFileDelete.Close();
CStdioFile mFileW(_T(m_ResultsFolder+"\\Label.inf "),CFile::modeWrite|CFile::modeCreate);
mFileW.SeekToBegin();
mFileW.WriteString(info+"\n");
mFileW.WriteString(CSTotal+"\n");
for(i=0;i<(total)*5;i++) //写进去
{
mFileW.WriteString(CSbuffer[i]+"\n");
}
mFileW.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -