ddView.cpp
上传用户:wjfckcom
上传日期:2014-06-28
资源大小:1989k
文件大小:13k
- // ddView.cpp : implementation of the CDdView class
- //
- #include "stdafx.h"
- #include "dd.h"
- #include "ddDoc.h"
- #include "ddView.h"
- #include "HistogramDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CDdView
- IMPLEMENT_DYNCREATE(CDdView, CView)
- BEGIN_MESSAGE_MAP(CDdView, CView)
- //{{AFX_MSG_MAP(CDdView)
- ON_COMMAND(ID_HISTOGRAM, OnHistogram)
- ON_COMMAND(ID_IMG, OnImg)
- ON_COMMAND(ID_MATCHCONTOUR, OnMatchcontour)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDdView construction/destruction
- CDdView::CDdView()
- {
- // TODO: add construction code here
- }
- CDdView::~CDdView()
- {
- }
- BOOL CDdView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDdView drawing
- void CDdView::OnDraw(CDC* pDC)
- {
- CDdDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- pDoc->myimage.CopyOf(pDoc->pSrcImg,1);
- CRect rect;
- GetClientRect(rect);
- rect.bottom = pDoc->myimage.Height();
- rect.right = pDoc->myimage.Width();
-
- pDoc->myimage.DrawToHDC(pDC->m_hDC , rect);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDdView printing
- BOOL CDdView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CDdView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CDdView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDdView diagnostics
- #ifdef _DEBUG
- void CDdView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CDdView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CDdDoc* CDdView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDdDoc)));
- return (CDdDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CDdView message handlers
- void CDdView::OnHistogram()
- {
- int i,j;
- int width = GetDocument()->width;
- int height = GetDocument()->height;
- CvMemStorage * storage = cvCreateMemStorage(0);
- CvSeq * contour = 0;
-
- IplImage* pImage = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
- pContourImg1 = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);
- Image = cvCreateImage(cvSize(width,height),8,1);
- IplImage* img = cvCreateImage(cvSize(width,height),8,3);
- cvCopy(GetDocument()->pSrcImg,img);
- cvCvtColor(GetDocument()->pSrcImg, Image, CV_RGB2GRAY);
- cvThreshold(Image,pImage,150, 255,CV_THRESH_BINARY_INV);
- cvSmooth(pImage, Image, CV_MEDIAN);
-
- cvCvtColor(Image, pContourImg1, CV_GRAY2BGR);
- cvFindContours( Image, storage, &contour, sizeof(CvContour),
- CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
- cvDrawContours(pContourImg1, contour, CV_RGB(255,0,255),
- CV_RGB(255, 0, 0), 1, 1, 8);
- n1 = contour->total;
- CvPoint *PointArray=new CvPoint[n1];
- cvCvtSeqToArray( contour, PointArray, CV_WHOLE_SEQ );//提取点集
-
- seq1 = new struct contourrate[n1];
- double* r = (double*)calloc(n1,sizeof(double));
- double sum = 0.0;
- for(i = 0; i<n1;i++)
- {
- int x0 =PointArray[i].x;
- int y0 =PointArray[i].y;
- int x1 =PointArray[(i-5+n1)%n1].x;
- int y1 =PointArray[(i-5+n1)%n1].y;
- int x2 =PointArray[(i+5+n1)%n1].x;
- int y2 =PointArray[(i+5+n1)%n1].y;
- if(((uchar*)(pImage->imageData + pImage->widthStep*(y1/2+y2/2)))[x1/2+x2/2]==0)
- r[i] = 0 - (double)abs((y2-y1)*(x0-x1)+(x2-x1)*(y1-y0))/(double)((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));
- else
- r[i] = (double)abs((y2-y1)*(x0-x1)+(x2-x1)*(y1-y0))/(double)((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));
- sum += r[i];
- }
-
- for(i =0; i<n1; i++)
- {
- seq1[i].p.x = PointArray[i].x;
- seq1[i].p.y = PointArray[i].y;
- seq1[i].q = (float) (r[i]/sum) ;
- }
- /* int gray[256]={0} ;
-
- for(i = 0;i < height;i++)
- for(j=0; j< width; j++)
- {
- int n =((uchar*)(Image->imageData + Image->widthStep*i))[j];
- gray[n]++;
- }
- for(i=0; i<256; i++)
- tongji[i] = gray[i]/(width*height/1.0f);*/
- CHistogramDlg* dlg;
- dlg = new CHistogramDlg(this);
- dlg->Create(IDD_HISTOGRAMDLG);
- for(i=0; i<n1; i++)
- dlg->m_fIntensity[i*256/n1+1] = seq1[i].q;
- dlg->ShowWindow(SW_RESTORE);
- cvCopy(pContourImg1, GetDocument()->pSrcImg);
- Invalidate();
- free(r);
-
- cvReleaseImage(&pImage);
- cvReleaseImage(&img);
-
- }
- void CDdView::OnImg()
- {
- CFileDialog filedlg(TRUE, NULL, NULL, OFN_HIDEREADONLY,
- "所有文件 (*.*)|*.*||BMP Files(*.BMP)|*.BMP|JPEG Files(*.JPG)|*.JPG|");
- if(filedlg.DoModal()!=IDOK)
- {
- }
- CString filename = filedlg.GetPathName();
- IplImage* img = cvLoadImage(filename,0);
-
- int i,j;
- int width = img->width;
- int height = img->height;
- CvMemStorage * storage = cvCreateMemStorage(0);
- CvSeq * contour = 0;
- pContourImg2 = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);
- IplImage* pImage = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
-
- cvThreshold(img,pImage,150, 255,CV_THRESH_BINARY_INV);
- cvSmooth(pImage, img, CV_MEDIAN);
-
-
- cvCvtColor(img, pContourImg2, CV_GRAY2BGR);
- cvFindContours( img, storage, &contour, sizeof(CvContour),
- CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
- cvDrawContours(pContourImg2, contour, CV_RGB(255,0,255),
- CV_RGB(255, 0, 0), 1, 1, 8);
-
- n2 = contour->total;
- CvPoint *PointArray=new CvPoint[n2];
- cvCvtSeqToArray( contour, PointArray, CV_WHOLE_SEQ );//提取点集
-
- seq2 = new struct contourrate [n2];
- double* r = (double*)calloc(n2,sizeof(double));
- double sum = 0.0;
- for(i = 0; i<n2;i++)
- {
- int x0 =PointArray[i].x;
- int y0 =PointArray[i].y;
- int x1 =PointArray[(i-5+n2)%n2].x;
- int y1 =PointArray[(i-5+n2)%n2].y;
- int x2 =PointArray[(i+5+n2)%n2].x;
- int y2 =PointArray[(i+5+n2)%n2].y;
-
- if(((uchar*)(pImage->imageData + pImage->widthStep*(y1/2+y2/2)))[x1/2+x2/2]==0)
- r[i] = 0 - (double)abs((y2-y1)*(x0-x1)+(x2-x1)*(y1-y0))/(double)((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));
- else
- r[i] = (double)abs((y2-y1)*(x0-x1)+(x2-x1)*(y1-y0))/(double)((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));
- sum += r[i];
- }
-
- for(i =0; i<n2; i++)
- {
- seq2[i].p.x = PointArray[i].x;
- seq2[i].p.y = PointArray[i].y;
- seq2[i].q =(float) (r[i]/sum);
- }
- CHistogramDlg* dlg;
- dlg = new CHistogramDlg(this);
- dlg->Create(IDD_HISTOGRAMDLG);
- for(i=0; i<n2; i++)
- dlg->m_fIntensity[i*256/n2+1] = seq2[i].q;
- dlg->ShowWindow(SW_RESTORE);
-
- cvNamedWindow("src", CV_WINDOW_AUTOSIZE);
- cvShowImage("src",pContourImg2);
- free(r);
-
- cvReleaseImage(&pImage);
- cvReleaseImage(&img);
- }
- void CDdView::OnMatchcontour()
- {
- //t1 ...t4 alloc
- t1 = new struct rateseq[n1/5];
- t2 = new struct rateseq[n1/5];
- t3 = new struct rateseq[n2/5];
- t4 = new struct rateseq[n2/5];
- int i, j;
- int m1, m2, m3,m4;
- m1 = m2 = m3 = m4 = 0;
- for(i=1; i<n1-1; i++)
- {
- if(seq1[i].q>seq1[i-1].q&&seq1[i].q>seq1[i+1].q&&seq1[i].q>0)
- {
- t1[m1].q=seq1[i].q;
- t1[m1].index = i;
- m1++;
- continue;
- }
- else if(seq1[i].q<seq1[i-1].q&&seq1[i].q<seq1[i+1].q&&seq1[i].q<0)
- {
- t2[m2].q=seq1[i].q;
- t2[m2].index = i;
- m2++;
- continue;
- }
- continue;
- }
-
- for(i=1; i<n2-1; i++)
- {
- if(seq2[i].q>seq2[i-1].q&&seq2[i].q>seq2[i+1].q&&seq2[i].q>0)
- {
- t3[m3].q=seq2[i].q;
- t3[m3].index=i;
- m3++;
- continue;
- }
- else if(seq2[i].q<seq2[i-1].q&&seq2[i].q<seq2[i+1].q&&seq2[i].q<0)
- {
- t4[m4].q=seq2[i].q;
- t4[m4].index = i;
- m4++;
- continue;
- }
- continue;
- }
- /* IplImage* resultimg = cvCreateImage(cvSize(Image->width,Image->height),8,3);
-
- cvCvtColor(Image,resultimg,CV_GRAY2BGR); */
- IplImage* resultimg = stack_imgs(pContourImg1, pContourImg2);
- for(i=0; i<m1-1; i++)
- for(j=0; j<m3-1; j++)
- {
- if(fabs(t1[i].q-t3[j].q)<0.05*(t1[i].q+t3[j].q))
- {
- float hausv1 = Hausdorff(t1[i],t3[j], seq1 ,seq2);
- if (hausv1<THREHOLD)
- {
- /* int z = 0;
-
- if (t1[i].index<10)
- {
-
- for(int k=0; k<t1[i].index+10; k++)
- {
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3] = 0;
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3+1] = 125;
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3+2] = z;
- }
- z+=30;
- }
- else if (t1[i].index>n1-10)
- {
- for(int k=t1[i].index-10; k<n1-10; k++)
- {
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3] = z;
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3+1] = 125;
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3+2] = 0;
- }
- z+=30;
- }
- else
- {
- for(int k=t1[i].index-10; k<t1[i].index+10; k++)
- {
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3] = z;
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3+1] = 125;
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3+2] = z;
- }
- z+=30;
- }*/
-
- /*int m,n;
- for(m=0; m<10&&t1[i].index-m>0&&t3[j].index-m>0;m++)
- {
- CvPoint pt1 = seq1[(t1[i].index-m)].p;
- CvPoint pt2 = seq2[(t3[j].index-m)].p;
- pt2.y+=pContourImg1->height;
- cvLine( resultimg, pt1, pt2, CV_RGB(255,0,i*50%255), 1, 8, 0 );
- }
- for(n=1; n<10&&t1[i].index+n<n1&&t3[j].index+n<n2;n++)
- {
- CvPoint pt1 = seq1[t1[i].index+n].p;
- CvPoint pt2 = seq2[t3[j].index+n].p;
- pt2.y+=pContourImg1->height;
- cvLine( resultimg, pt1, pt2, CV_RGB(255,i*50%255,255), 1, 8, 0 );
- }*/
- CvPoint pt1 = seq1[t1[i].index].p;
- CvPoint pt2 = seq2[t3[j].index].p;
- pt2.y+=pContourImg1->height;
- cvLine( resultimg, pt1, pt2, CV_RGB(255,i*50%255,255), 1, 8, 0 );
-
-
- }
- }
- continue;
- }
- for(i=0; i<m2; i++)
- for(j=0; j<m4; j++)
- {
- if (fabs(t2[i].q-t4[j].q)<0.05*abs(t2[i].q+t4[j].q))
- {
- float hausv2 = Hausdorff(t2[i], t4[j], seq1, seq2);
- if (hausv2<THREHOLD)
- {
- /* if (t2[i].index<10)
- {
- for(int k=0; k<t2[i].index+10; k++)
- {
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3] = 255;
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3+1] = 255;
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3+2] = 0;
- }
- }
- else if (t2[i].index>n1-10)
- {
- for(int k=t2[i].index-10; k<n1-10; k++)
- {
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3] = 255;
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3+1] =255;
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3+2] = 0;
- }
- }
- else
- {
- for(int k=t2[i].index-10; k<t2[i].index+10; k++)
- {
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3] = 255;
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3+1] = 255;
- ((uchar*)(resultimg->imageData + resultimg->widthStep*(seq1[k].p.y)))[(seq1[k].p.x)*3+2] = 0;
- }
- }*/
- /*int m,n;
- for(m=0; m<10&&t2[i].index-m>0&&t4[j].index-m>0;m++)
- {
- CvPoint pt1 = seq1[t2[i].index-m].p;
- CvPoint pt2 = seq2[t4[j].index-m].p;
- pt2.y+=pContourImg1->height;
- cvLine( resultimg, pt1, pt2, CV_RGB(i*50%255,0,255), 1, 8, 0 );
- }
- for(n=1; n<10&&t2[i].index+n<n1&&t4[j].index+n<n2;n++)
- {
- CvPoint pt1 = seq1[(t2[i].index+n)].p;
- CvPoint pt2 = seq2[t4[j].index+n].p;
- pt2.y+=pContourImg1->height;
- cvLine( resultimg, pt1, pt2, CV_RGB(255,i*50%255,i*50%255), 1, 8, 0 );
- }*/
- CvPoint pt1 = seq1[t2[i].index].p;
- CvPoint pt2 = seq2[t4[j].index].p;
- pt2.y+=pContourImg1->height;
- cvLine( resultimg, pt1, pt2, CV_RGB(255,i*50%255,0), 1, 8, 0 );
- }
- }
- continue;
- }
-
- cvNamedWindow("matchresult",CV_WINDOW_AUTOSIZE);
- cvShowImage("matchresult",resultimg);
- delete [] t1;
- delete [] t2;
- delete [] t3;
- delete [] t4;
- delete [] seq1;
- delete [] seq2;
- cvReleaseImage(&Image);
- cvReleaseImage(&pContourImg2);
- cvReleaseImage(&pContourImg1);
- cvReleaseImage(&resultimg);
- }