📄 magicscissorsdoc.cpp
字号:
cp4.x = ep.x + dx;
cp4.y = ep.y + dy;
m_ptConner[0] = cp1;
m_ptConner[1] = cp2;
m_ptConner[2] = cp3;
m_ptConner[3] = cp4;
}
CPoint CMagicScissorsDoc::FindEdgePoint(int x1, int y1, int x2, int y2, CPoint pre)
{
int dx,dy;
int step, i;
CPoint point(-1,-1);
CPoint pnt[100];
int num = 0;
float fx,fy;
float ddx,ddy;
int x,y;
dx = x2 - x1;
dy = y2 - y1;
if( dx == 0 )
{
step = abs(dy);
ddx = (float)dx/step;
ddy = (float)dy/step;
}
else if( fabs( (double)dy/dx ) <= 1. )
{
step = abs(dx);
ddx = (float)dx/step;
ddy = (float)dy/step;
}
else
{
step = abs(dy);
ddx = (float)dx/step;
ddy = (float)dy/step;
}
fx = (float)x1;
fy = (float)y1;
BYTE** ptr = m_pEdgeMap->GetPtr();
int px,py,nx,ny;
for( i = 0 ; i < step ; i++ )
{
fx += ddx;
fy += ddy;
x = (int)fx;
y = (int)fy;
if( x < 0 || x >= m_ImgSize.cx-1 || y < 0 || y >= m_ImgSize.cy-1 ) continue;
px = (int)(fx-ddx);
py = (int)(fy-ddy);
nx = (int)(fx+ddx);
ny = (int)(fx+ddy);
if( ptr[y][x] || ptr[y+1][x] || ptr[y][x+1] || ptr[y+1][x+1] == 255 )
{
pnt[num].x = x;
pnt[num].y = y;
num++;
}
}
if( num == 0 ) return point;
if( num == 1 ) return pnt[0];
double min = 9999999;
double dis;
for( i = 0 ; i < num ; i++ )
{
dis = sqrt( (pnt[i].x-pre.x)*(pnt[i].x-pre.x)+(pnt[i].y-pre.y)*(pnt[i].y-pre.y) );
if( dis < min )
{
min = dis;
point = pnt[i];
}
}
return point;
}
void CMagicScissorsDoc::FindLine(CPoint sp, CPoint ep)
{
if( !m_pImage ) return;
CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
pFrame->m_wndOptionBar.UpdateData(TRUE);
int w = pFrame->m_wndOptionBar.m_nSearchWidth;
int e = pFrame->m_wndOptionBar.m_nEdge;
if( !m_pEdgeMap || !m_bEdge )
{
DetectEdge();
}
if( !m_pTempList )
m_pTempList = new CEdgeList;
m_pTempList->Clear();
CEdgePoint* last = m_pEdgeList->GetLastPoint();
if( last ) sp = *last;
FindConner(sp, ep, w);
int dx,dy;
int step;
float x1,y1,x2,y2;
float ddx,ddy;
dx = ep.x - sp.x;
dy = ep.y - sp.y;
if( dx == 0 )
{
step = abs(dy);
ddx = (float)dx/step;
ddy = (float)dy/step;
}
else if( fabs( (double)dy/dx ) <= 1.0 )
{
step = abs(dx);
ddx = (float)dx/step;
ddy = (float)dy/step;
}
else
{
step = abs(dy);
ddx = (float)dx/step;
ddy = (float)dy/step;
}
CPoint pnt;
CEdgePoint* pt;
CEdgeList* pList = m_pTempList;
x1 = sp.x;
y1 = sp.y;
for( int i = 0 ; i < step ; i++ )
{
x1 += ddx;
y1 += ddy;
pList->AddEdgePoint(CPoint(x1,y1));
}
}
void CMagicScissorsDoc::AddNewObject()
{
if( !m_pEdgeList ) return;
if( !m_pCurFrame ) return;
PrepareUndo();
m_pEdgeList->MakeClosedPath();
CObjRgn* pObject = new CObjRgn;
pObject->Create( m_pEdgeList);
m_pEdgeList = NULL;
m_pCurFrame->AddObject(pObject);
if( m_pEdgeListStack )
{
delete m_pEdgeListStack;
m_pEdgeListStack = NULL;
}
}
int CMagicScissorsDoc::SelectObject(CPoint point)
{
if( m_pPolygonList )
{
m_pPolygonList->Select(point);
return 1;
}
else
{
m_pCurFrame->SelectObject(point);
return 0;
}
}
void CMagicScissorsDoc::AddModify(CPoint point)
{
if( !m_pModifyList )
m_pModifyList = new CEdgeList;
m_pModifyList->AddEdgePoint(point);
}
void CMagicScissorsDoc::ModifyEdge()
{
if( m_pModifyList )
{
PrepareUndo();
m_pCurFrame->ModifyEdge(m_pModifyList);
m_pModifyList->Clear();
}
}
void CMagicScissorsDoc::OnDataSave()
{
// TODO: Add your command handler code here
if( m_strDataFile == "" )
{
CString str = m_strVideoFile;
int l = str.ReverseFind('.');
str = str.Left(l) + ".seg";
CFileDialog dlg(FALSE, ".seg", (LPCTSTR)str, OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_EXPLORER|OFN_OVERWRITEPROMPT, "单捞磐 颇老(*.seg)|*.seg||", NULL);
if( dlg.DoModal() == IDOK )
m_strDataFile = dlg.GetPathName();
else return;
}
BeginWaitCursor();
WriteDataFile((LPCTSTR)m_strDataFile);
EndWaitCursor();
}
void CMagicScissorsDoc::OnDataSaveas()
{
// TODO: Add your command handler code here
CString str = m_strVideoFile;
int l = str.ReverseFind('.');
str = str.Left(l) + ".seg";
CFileDialog dlg(FALSE, ".seg", (LPCTSTR)str, OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_EXPLORER|OFN_OVERWRITEPROMPT, "单捞磐 颇老(*.seg)|*.seg||", NULL);
if( dlg.DoModal() == IDOK )
m_strDataFile = dlg.GetPathName();
else return;
BeginWaitCursor();
WriteDataFile((LPCTSTR)m_strDataFile);
EndWaitCursor();
}
void CMagicScissorsDoc::OnDataOpen()
{
// TODO: Add your command handler code here
CString str = m_strVideoFile;
int l = str.ReverseFind('.');
str = str.Left(l) + ".seg";
CFileDialog dlg(TRUE, ".seg", (LPCTSTR)str, OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_EXPLORER|OFN_OVERWRITEPROMPT, "单捞磐 颇老(*.seg)|*.seg||", NULL);
if( dlg.DoModal() == IDOK )
{
BeginWaitCursor();
m_strDataFile = dlg.GetPathName();
ReadDataFile((LPCTSTR)m_strDataFile);
UpdateAllViews(NULL);
EndWaitCursor();
}
}
BOOL CMagicScissorsDoc::WriteDataFile(LPCTSTR strFileName)
{
CFile file;
file.Open( strFileName, CFile::modeCreate|CFile::modeWrite );
BOOL res = m_pFrameList->Write(&file);
file.Close();
return res;
}
BOOL CMagicScissorsDoc::ReadDataFile(LPCTSTR strFileName)
{
if( m_pFrameList ) delete m_pFrameList;
CFile file;
file.Open( strFileName, CFile::modeRead );
m_pFrameList = new CFrameList;
BOOL res = m_pFrameList->Read(&file);
file.Close();
m_pCurFrame = m_pFrameList->GetFrame(m_nCurFrame-1);
return res;
}
void CMagicScissorsDoc::OnOpenGlView()
{
// TODO: Add your command handler code here
OpenGLView();
}
void CMagicScissorsDoc::OnProperty()
{
// TODO: Add your command handler code here
CObjRgn* pObj = m_pCurFrame->GetSelected();
if( pObj )
{
pObj->Property();
if( m_pSecond ) m_pSecond->Invalidate();
}
}
BOOL CMagicScissorsDoc::EdgeSmoothing()
{
// TODO: Add your command handler code here
CObjRgn* pObj = m_pCurFrame->GetSelected();
if( pObj )
{
PrepareUndo();
CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
pFrame->m_wndOptionBar.UpdateData(TRUE);
int w = pFrame->m_wndOptionBar.m_nSmoothTab;
pObj->Smoothing(w);
return TRUE;
}
else return FALSE;
}
BOOL CMagicScissorsDoc::DeleteObject()
{
if( m_pPolygonList )
{
CPolygon* pPolygon = m_pPolygonList->GetSelect();
if( pPolygon )
{
m_pPolygonList->Delete(pPolygon);
return TRUE;
}
else
return FALSE;
}
else
{
CObjRgn* pObj = m_pCurFrame->GetSelected();
if( pObj )
{
PrepareUndo();
m_pCurFrame->DeleteObject(pObj);
m_pCurFrame->ClearSelect();
return TRUE;
}
else return FALSE;
}
}
BOOL CMagicScissorsDoc::PrepareUndo()
{
if( !m_pObjUndoStack )
m_pObjUndoStack = new CObjListStack;
m_pObjUndoStack->Push(m_pCurFrame->GetObjList());
return TRUE;
}
void CMagicScissorsDoc::OnEditUndo()
{
// TODO: Add your command handler code here
if( m_pObjUndoStack )
{
CObjList* pList = m_pObjUndoStack->Pop();
if( pList )
{
m_pCurFrame->ReplaceList( pList );
if( m_pMain ) m_pMain->Invalidate(FALSE);
}
}
}
void CMagicScissorsDoc::CopyPrev()
{
CObjList* pObjList = m_pCurFrame->GetObjList();
if( pObjList )
{
if( pObjList->GetTotal() != 0 ) return;
}
if( m_pPolygonList ) delete m_pPolygonList;
m_pPolygonList = new CPolygonList;
CFrameInfo* pPrev = m_pFrameList->GetFrame(m_nCurFrame-2);
if( !pPrev ) return;
pObjList = pPrev->GetObjList();
if( !pObjList ) return;
int n = pObjList->GetTotal();
CObjRgn* pObj;
CPolygon* pPolygon;
for( int i = 0 ; i < n ; i++ )
{
pObj = pObjList->GetObject(i);
pPolygon = new CPolygon;
pPolygon->Set(pObj->GetEdgeList());
pPolygon->MakeRgn();
m_pPolygonList->Add(pPolygon);
}
}
void CMagicScissorsDoc::TraceEdge()
{
CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
pFrame->m_wndOptionBar.UpdateData(TRUE);
int w = pFrame->m_wndOptionBar.m_nSearchWidth;
int e = pFrame->m_wndOptionBar.m_nEdge;
CObjList* pObjList = m_pCurFrame->GetObjList();
if( pObjList )
{
if( pObjList->GetTotal() != 0 ) return;
}
if( !m_pEdgeMap || !m_bEdge )
{
DetectEdge();
/*CImageColor color(m_pImage);
if( m_pEdgeMap )
color.ConvertGray(m_pEdgeMap );
else
m_pEdgeMap = color.ConvertGray(TRUE);
CEdgeDetector edge(m_pEdgeMap);
edge.Edge_Deriche(m_fEdge[e-1]);
m_bEdge = TRUE;*/
}
CEdgeTracer tracer(m_pEdgeMap);
CEdgeList* pEdgeList;
CFrameInfo* pPrev = m_pFrameList->GetFrame(m_nCurFrame-2);
if( !pPrev ) return;
pObjList = pPrev->GetObjList();
if( !pObjList ) return;
int n = pObjList->GetTotal();
CObjRgn* pObj;
for( int i = 0 ; i < n ; i++ )
{
pObj = pObjList->GetObject(i);
if( m_pPolygonList )
pEdgeList = tracer.TraceEdge( m_pPolygonList->GetPolygon(i), w );
else
pEdgeList = tracer.TraceEdge( pObj->GetEdgeList(), w );
AddNewObject(pEdgeList, pObj->GetObject());
pObj->Smoothing(5);
}
if( m_pPolygonList )
{
delete m_pPolygonList;
m_pPolygonList = NULL;
}
if(m_pSecond) m_pSecond->Invalidate(FALSE);
}
void CMagicScissorsDoc::AddNewObject(CEdgeList* pEdgeList, C3DObject* p3DObject)
{
if( !pEdgeList ) return;
if( !m_pCurFrame ) return;
PrepareUndo();
pEdgeList->MakeClosedPath();
CObjRgn* pObject = new CObjRgn;
pObject->SetSize(m_ImgSize);
pObject->Create(pEdgeList);
if( p3DObject )
pObject->CopyPrev(p3DObject);
m_pCurFrame->AddObject(pObject);
}
void CMagicScissorsDoc::OnWriteObject()
{
AfxGetMainWnd()->UpdateWindow();
BeginWaitCursor();
WriteObject(NULL);
EndWaitCursor();
}
void CMagicScissorsDoc::WriteObject(LPCTSTR strFileName)
{
if( m_strSeg == "" )
{
AfxMessageBox("历厘 版肺啊 汲沥登瘤 臼疽嚼聪促.");
return;
}
CString dir = m_strSeg;
int l = dir.ReverseFind('\\');
dir = dir.Left(l);
CFileFind find;
if( !find.FindFile( dir ) )
{
if( ! ::CreateDirectory( dir, NULL ) )
{
AfxMessageBox("叼泛配府甫 父甸 荐 绝嚼聪促.");
return;
}
}
find.Close();
CString FileName = m_strSeg + ".info";
CStdioFile out;
out.Open( FileName, CFile::modeCreate|CFile::modeWrite );
CString str;
CImage* pImage = new CImage(m_ImgSize, 24, 0);
CImage* pObject;
CColorPixel** ptrsrc;
CColorPixel** ptrdes;
CString extname;
int fnum = m_pFrameList->GetTotal();
int i,j,objnum, ptnum;
CFrameInfo* pInfo;
CObjList* pObjList;
CObjRgn* pRgn;
CEdgeList* pPtList;
CPoint pnt;
CRect rect;
int w,h, x,y, sx, sy;
CString output;
CWriteObjDlg* pDlg = new CWriteObjDlg;
pDlg->Create(IDD_WRITE_OBJECT);
pDlg->SetWindowText("按眉 沥焊甫 历厘钦聪促.");
pDlg->m_Progress.SetRange(0, fnum-1);
pDlg->m_Progress.SetPos(0);
output.Format("0 / %d frames", fnum );
pDlg->GetDlgItem(IDC_OUT)->SetWindowText(output);
pDlg->ShowWindow(SW_SHOW);
pDlg->CenterWindow();
int count = 0;
int sframe = m_nStartFrame;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -