📄 segmentview.cpp
字号:
BitmapToClient(cr);
InvalidateRect(cr);
}
}
m_pSelectedBlob = pBlob;
pBlob->GetRect(cr);
BitmapToClient(cr);
InvalidateRect(cr);
if (m_pTemplateBlob) {
// Remember to paint the new template location too
GetTemplateRect(cr);
BitmapToClient(cr);
InvalidateRect(cr);
// Let's display the match inside the fake tool tip.
double dfMatch = pBlob->MatchBlobs(*m_pTemplateBlob);
CString csz;
char buffer[20];
AfxFormatString1(csz,IDS_SHORT_MATCH,gcvt(dfMatch,3,buffer));
m_staticInfo.SetWindowText(csz);
CClientDC dc(&m_staticInfo);
CSize csClient = dc.GetTextExtent(csz);
CRect crInfo(ptClient,csClient);
AdjustWindowRect(&crInfo,WS_BORDER,FALSE);
m_staticInfo.SetWindowPos(&wndTop,crInfo.left,crInfo.top,crInfo.Width(),crInfo.Height(),SWP_SHOWWINDOW);
}
break;
}
}
CScrollView::OnLButtonDown(nFlags, point);
}
//-----------------------------------------------------------------
//
// CSegmentView::ClientToBitmap - Transform coordinates from client
// relative to bitmap relative.
//
//-----------------------------------------------------------------
void CSegmentView::ClientToBitmap(CPoint &pt)
{
pt.x += GetScrollPos(SB_HORZ);
pt.y += GetScrollPos(SB_VERT);
}
void CSegmentView::BitmapToClient(CPoint &pt)
{
pt.x -= GetScrollPos(SB_HORZ);
pt.y -= GetScrollPos(SB_VERT);
}
void CSegmentView::ClientToBitmap(CRect & cr)
{
ClientToBitmap(cr.TopLeft());
ClientToBitmap(cr.BottomRight());
}
void CSegmentView::BitmapToClient(CRect & cr)
{
BitmapToClient(cr.TopLeft());
BitmapToClient(cr.BottomRight());
}
//-----------------------------------------------------------------
//
// CSegmentView::OnUpdate - It's safest to remove selection after
// the image has changed.
//
//-----------------------------------------------------------------
void CSegmentView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
m_pSelectedBlob = NULL;
m_pTemplateBlob = NULL;
m_blobBullseye.resize(0);
CScrollView::OnUpdate(pSender,lHint,pHint);
}
//-----------------------------------------------------------------
//
// CSegmentView::OnBlobStatistics - Display statistics for the
// currently selected blob.
//
//-----------------------------------------------------------------
void CSegmentView::OnBlobStatistics()
{
CBlobStatsDlg dlg(this);
ASSERT(m_pSelectedBlob);
dlg.m_nArea = m_pSelectedBlob->GetArea();
CPoint ptCentroid = m_pSelectedBlob->GetCentroid();
dlg.m_nCentroidX = ptCentroid.x;
dlg.m_nCentroidY = ptCentroid.y;
CRect cr;
m_pSelectedBlob->GetRect(cr);
dlg.m_nTop = cr.top;
dlg.m_nLeft = cr.left;
dlg.m_nBottom = cr.bottom;
dlg.m_nRight = cr.right;
dlg.m_nLoops = m_pSelectedBlob->m_nLoops;
dlg.DoModal();
}
void CSegmentView::OnUpdateBlobStatistics(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_pSelectedBlob != NULL);
}
//-----------------------------------------------------------------
//
// CSegmentView::OnBlobChoosetemplate - Make the currently selected
// blob into the template blob.
//
//-----------------------------------------------------------------
void CSegmentView::OnBlobChoosetemplate()
{
// We have to invalidate the old template, or else
// the old template's junk never clears.
CRect cr;
if (m_pTemplateBlob) {
GetTemplateRect(cr);
BitmapToClient(cr);
InvalidateRect(cr);
}
m_pTemplateBlob = m_pSelectedBlob;
// Might as well paint where the new one goes...
GetTemplateRect(cr);
BitmapToClient(cr);
InvalidateRect(cr);
}
void CSegmentView::OnUpdateBlobChoosetemplate(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_pSelectedBlob != NULL);
}
//-----------------------------------------------------------------
//
// CSegmentView::OnBlobMatch - Match the currently selected blob
// against the template blob
//
//-----------------------------------------------------------------
void CSegmentView::OnBlobMatch()
{
CMonochromeBitmap &mb = GetDocument()->GetMonochromeBitmap();
ASSERT(m_pSelectedBlob);
ASSERT(m_pTemplateBlob);
double dfMatch = m_pSelectedBlob->MatchBlobs(*m_pTemplateBlob);
char buffer[20];
gcvt(dfMatch,3,buffer);
CString csz;
AfxFormatString1(csz,IDS_MATCH1,buffer);
MessageBox(csz);
}
void CSegmentView::OnUpdateBlobMatch(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_pSelectedBlob && m_pTemplateBlob);
}
//-----------------------------------------------------------------
//
// CSegmentView::OnCreate - Create the fake tool tip.
//
//-----------------------------------------------------------------
int CSegmentView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CScrollView::OnCreate(lpCreateStruct) == -1)
return -1;
// Create our little fake tool tip.
CRect crInfo(0,0,100,100);
if (! m_staticInfo.Create(NULL,WS_CHILD|WS_BORDER,crInfo,this)) {
return -1;
}
return 0;
}
//-----------------------------------------------------------------
//
// CSegmentView::OnLButtonUp - Kill the template matching tooltip on button up.
//
//-----------------------------------------------------------------
void CSegmentView::OnLButtonUp(UINT nFlags, CPoint point)
{
m_staticInfo.ShowWindow(SW_HIDE);
CScrollView::OnLButtonUp(nFlags, point);
}
//-----------------------------------------------------------------
//
// CSegmentView::GetTemplateRect - We overlay the template blob outline
// over the selected blob outline. This involves
// matching their centroids (because that's how
// the matching is done) and computing the offset
// to the template blob's top left corner.
//
// cr - return template rectangle here.
//
//-----------------------------------------------------------------
void CSegmentView::GetTemplateRect(CRect & cr)
{
ASSERT(m_pTemplateBlob);
ASSERT(m_pSelectedBlob);
CMonochromeBitmap &mb = GetDocument()->GetMonochromeBitmap();
CPoint ptTemplate = m_pTemplateBlob->GetCentroid();
CPoint ptSelection = m_pSelectedBlob->GetCentroid();
CRect crTemplate;
m_pTemplateBlob->GetRect(crTemplate);
// Figure how far away the template center is from the top left corners.
CSize csOffsetTemplate = ptTemplate - crTemplate.TopLeft();
// The template rectangle starts at the selection center
// minus the template offset.
cr.TopLeft() = ptSelection - csOffsetTemplate;
cr.BottomRight() = cr.TopLeft() + crTemplate.Size();
}
//-----------------------------------------------------------------
//
// CSegmentView::OnImageFindbullseyes - This builds a list of the
// blobs in any "bullseyes" in the image. We
// define a bullseye as a center with two or more
// objects (that have at least one loop) whose
// centroids are close.
//
//-----------------------------------------------------------------
void CSegmentView::OnImageFindbullseyes()
{
// We define a bullseye as three concentric objects (they can be
// ellipses if the target is tilted) whose centroids fall within
// the same 10 x 10 rectangle.
CMonochromeBitmap &mb = GetDocument()->GetMonochromeBitmap();
CQuadTree<CBlob> &qt = mb.GetQuadTree();
// OK, this is simplicity and it runs in N log N rather than N * N.
const int nBlobs = mb.GetBlobCount();
for (int i = 0; i < nBlobs; i++) {
CBlob *pBlob = mb.GetBlob(i);
CRect cr(pBlob->GetCentroid(),CSize(0,0));
// OK, if something is smaller than 5x5, then this rectangle
// could possibly enclose another small point. If the inside
// bullseye is really small, we still won't fail because the
// larger one will pass the test.
// Let's also throw in the contingency that the test object
// has at least one loop. That excludes the center anyway.
if (pBlob->GetArea() > 25 && pBlob->m_nLoops > 0) {
cr.InflateRect(5,5);
vector<CBlob *> vBlob;
qt.GetItems(cr,vBlob);
if (vBlob.size() >= 3) {
m_blobBullseye.insert(m_blobBullseye.end(),vBlob.begin(),vBlob.end());
}
}
}
Invalidate();
}
void CSegmentView::OnUpdateImageFindbullseyes(CCmdUI* pCmdUI)
{
pCmdUI->Enable(GetDocument()->IsEmpty() == FALSE);
}
//-----------------------------------------------------------------
//
// CSegmentView::OnViewReversevideo - Flip the reverse video flag.
//
//-----------------------------------------------------------------
void CSegmentView::OnViewReversevideo()
{
m_bReverseVideo = ! m_bReverseVideo;
Invalidate();
}
void CSegmentView::OnUpdateViewReversevideo(CCmdUI* pCmdUI)
{
pCmdUI->Enable(GetDocument()->IsEmpty() == FALSE);
pCmdUI->SetCheck(m_bReverseVideo);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -