⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sampledlg.cpp

📁 capture the image of the inpu object from the user and then detect image edge of the object
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// SampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VidCapDlg.h"
#include "SampleDlg.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// SampleDlg dialog

ColorInfor* colorinfor= new ColorInfor;

SampleDlg::SampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(SampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(SampleDlg)
	m_AddCol = FALSE;
	m_ColType = -1;
	m_Pixel0 = 0;
	m_Pixel1 = 0;
	m_Pixel2 = 0;
	m_Pixel4 = 0;
	m_Pixel3 = 0;
	m_Var = 0;
	m_ShowSymbal = FALSE;
	//}}AFX_DATA_INIT
}


void SampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(SampleDlg)
	DDX_Check(pDX, IDC_CHECK_AddCol, m_AddCol);
	DDX_Radio(pDX, IDC_RADIO1, m_ColType);
	DDX_Text(pDX, IDC_EDIT1, m_Pixel0);
	DDX_Text(pDX, IDC_EDIT2, m_Pixel1);
	DDX_Text(pDX, IDC_EDIT3, m_Pixel2);
	DDX_Text(pDX, IDC_EDIT5, m_Pixel4);
	DDX_Text(pDX, IDC_EDIT4, m_Pixel3);
	DDX_Text(pDX, IDC_EDIT6, m_Var);
	DDX_Check(pDX, IDC_CHECK_SHOWSYMBAL, m_ShowSymbal);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(SampleDlg, CDialog)
	//{{AFX_MSG_MAP(SampleDlg)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_BN_CLICKED(ID_SAVE, OnSave)
	ON_BN_CLICKED(IDC_PATCH, OnPatch)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// SampleDlg message handlers

void SampleDlg::OnOK() 
{
   
   lpbi = m_FrameGrabber.GetDIB();    
   h=lpbi->bmiHeader.biHeight;
   w=lpbi->bmiHeader.biWidth;
   lpDIBBits = (LPBYTE)((LPDWORD)(lpbi->bmiColors + 
			lpbi->bmiHeader.biClrUsed) + 
			((lpbi->bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
    
StretchDIBits(gDC->m_hDC,0,0,w,h,0,0,w,h,lpDIBBits,
	  lpbi,DIB_RGB_COLORS,SRCCOPY);
}






BOOL SampleDlg::OnInitDialog() 
{
   	
   CDialog::OnInitDialog();
	
	if(!m_FrameGrabber.GetSafeHwnd())
	{
		m_FrameGrabber.Create(0,9,this);
	
		 lpbi = m_FrameGrabber.GetDIB();    
   h=lpbi->bmiHeader.biHeight;
   w=lpbi->bmiHeader.biWidth;
   lpDIBBits = (LPBYTE)((LPDWORD)(lpbi->bmiColors + 
			lpbi->bmiHeader.biClrUsed) + 
			((lpbi->bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
	}
   
   m_AddCol=FALSE;
   gDC=new CDC;
   gDC=GetDC();
   Pre=CPoint(0,0);
   SapArea=new long[200*200];


	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void SampleDlg::DisplayImg(CDC *dc, long *img, int w, int h, int x, int y)
{	
int i,j;
for(i=0;i<h;i++)
for(j=0;j<w;j++)
dc->SetPixel(x+j,y+i,*(img+i*w+j)); 

}

void SampleDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{	

   char ss[10];
   sprintf(ss,"%d  %d ",point.x,point.y);
   gDC->TextOut(w,200,ss); 
    
	if(point.x<w && point.y<h &&
		point.x>0 && point.y>0
		)
	{
	  int x,y;
	  x=point.x-10;
      y=point.y-10;
   if(x<0)
	   x=0;
   if(y<0)
	   y=0;

       int i,j,m,n;
	   COLORREF col;

    for(i=y;i<(point.y+10) && i<h;i++)
       for(j=x;j<(point.x+10) && j<w;j++)
		{
        col=gDC->GetPixel(j,i);		
	   
		for(m=0;m<10;m++)
			for(n=0;n<10;n++)
			{
	  *(SapArea+((i-y)*10+m)*200+(j-x)*10+n)=col;  
			}		     
		}

	  DisplayImg(gDC,SapArea,200,200,w,0); 
	}	     
  if(point.x<(w+200) &&
	  point.x>w && point.y < 200 && point.y>0)
  {    
	Pre=point;  
  }
	CDialog::OnLButtonDown(nFlags, point);
}

void SampleDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if(point.x>w && point.x<(w+200) &&
		point.y>0 && point.y<200 &&
		Pre.x !=0 && Pre.y !=0)
	{   
		int i,j;
	   // BYTE red,green,blue;
		int y, u, v;
		BYTE Ymin=255, Ymax=0, Umin=255, Umax=0, Vmin=255, Vmax=0;
		long col;

		for(i=Pre.x+1;i<point.x-1;i++)
			for(j=Pre.y+1;j<point.y-1;j++)
			{
			col=gDC->GetPixel(i,j);
			RGB2YUV(GetRValue(col),GetGValue(col),GetBValue(col),
				y,u,v);

              	    if ( y < Ymin ) Ymin = (BYTE) y;
					if ( y > Ymax ) Ymax = (BYTE) y;

					if ( u < Umin ) Umin = (BYTE) u;
					if ( u > Umax ) Umax = (BYTE) u;

					if ( v < Vmin ) Vmin = (BYTE) v;
					if ( v > Vmax ) Vmax = (BYTE) v;
			}

	   
	    UpdateData(TRUE);
        if(m_AddCol)
		
		switch(m_ColType)
		{
			case 0:
			if(colorinfor->redcolYmin>Ymin)
              colorinfor->redcolYmin=Ymin;   
		
			if(colorinfor->redcolUmin>Umin)
           colorinfor->redcolUmin=Umin;

           if(colorinfor->redcolVmin>Vmin)
			colorinfor->redcolVmin=Vmin;

		   
		   if(colorinfor->redcolYmax<Ymax)       
		   colorinfor->redcolYmax=Ymax;       
           
		   if(colorinfor->redcolUmax<Umax)
		   colorinfor->redcolUmax=Umax;

           if(colorinfor->redcolVmax<Vmax)
		   colorinfor->redcolVmax=Vmax;
		     break;

			 case 1:
          if(colorinfor->bluecolYmin>Ymin)
           colorinfor->bluecolYmin=Ymin;   
		
				if(colorinfor->bluecolUmin>Umin)
           colorinfor->bluecolUmin=Umin;

           if(colorinfor->bluecolVmin>Vmin)
				colorinfor->bluecolVmin=Vmin;

		   
		   if(colorinfor->bluecolYmax<Ymax)       
		   colorinfor->bluecolYmax=Ymax;       
           
		   if(colorinfor->bluecolUmax<Umax)
		   colorinfor->bluecolUmax=Umax;

           if(colorinfor->bluecolVmax<Vmax)
		   colorinfor->bluecolVmax=Vmax;
		     break;

			 case 2:
           if(colorinfor->greencolYmin>Ymin)
           colorinfor->greencolYmin=Ymin;   
		
				if(colorinfor->greencolUmin>Umin)
           colorinfor->greencolUmin=Umin;

           if(colorinfor->greencolVmin>Vmin)
				colorinfor->greencolVmin=Vmin;

		   
		   if(colorinfor->greencolYmax<Ymax)       
		   colorinfor->greencolYmax=Ymax;       
           
		   if(colorinfor->greencolUmax<Umax)
		   colorinfor->greencolUmax=Umax;

           if(colorinfor->greencolVmax<Vmax)
		   colorinfor->greencolVmax=Vmax;
		     break;

			 case 3:
           if(colorinfor->othercolYmin>Ymin)
           colorinfor->othercolYmin=Ymin;   
		
				if(colorinfor->othercolUmin>Umin)
           colorinfor->othercolUmin=Umin;

           if(colorinfor->othercolVmin>Vmin)
				colorinfor->othercolVmin=Vmin;

		   
		   if(colorinfor->othercolYmax<Ymax)       
		   colorinfor->othercolYmax=Ymax;       
           
		   if(colorinfor->othercolUmax<Umax)
		   colorinfor->othercolUmax=Umax;

           if(colorinfor->othercolVmax<Vmax)
		   colorinfor->othercolVmax=Vmax;
		     break;
		}
		else
		
		
          	switch(m_ColType)
		{
			case 0:
           colorinfor->redcolYmin=Ymin;       
           colorinfor->redcolUmin=Umin;
           colorinfor->redcolVmin=Vmin;

		   colorinfor->redcolYmax=Ymax;       
           colorinfor->redcolUmax=Umax;
           colorinfor->redcolVmax=Vmax;
		     break;

			 case 1:
           colorinfor->bluecolYmin=Ymin;       
           colorinfor->bluecolUmin=Umin;
           colorinfor->bluecolVmin=Vmin;

		   colorinfor->bluecolYmax=Ymax;       
           colorinfor->bluecolUmax=Umax;
           colorinfor->bluecolVmax=Vmax;
		     break;

			 case 2:
           colorinfor->greencolYmin=Ymin;       
           colorinfor->greencolUmin=Umin;
           colorinfor->greencolVmin=Vmin;

		   colorinfor->greencolYmax=Ymax;       
           colorinfor->greencolUmax=Umax;
           colorinfor->greencolVmax=Vmax;
		     break;

			 case 3:
           colorinfor->othercolYmin=Ymin;       
           colorinfor->othercolUmin=Umin;
           colorinfor->othercolVmin=Vmin;

		   colorinfor->othercolYmax=Ymax;       
           colorinfor->othercolUmax=Umax;
           colorinfor->othercolVmax=Vmax;
		     break;		
		}
	}
    Pre=(0,0);
	CDialog::OnLButtonUp(nFlags, point);
}

void SampleDlg::OnMouseMove(UINT nFlags, CPoint point) 
{	
	if (point.x<(w+200) &&
	  point.x>w && point.y < 200 && point.y>0 &&
		(MK_LBUTTON & nFlags)==MK_LBUTTON 
		&& Pre.x!=0 && Pre.y!=0)	
	{     
		DisplayImg(gDC,SapArea,200,200,w,0);
        gDC->MoveTo(Pre);
		gDC->LineTo(point.x,Pre.y);
		gDC->LineTo(point);
		gDC->LineTo(Pre.x,point.y);
		gDC->LineTo(Pre);

	}
	CDialog::OnMouseMove(nFlags, point);
}

void SampleDlg::FindPatch(int posx, int posy, int ww, int hh, 
						  int ImgW,int ImgH, 
						  BYTE Ymin, BYTE Umin, BYTE Vmin, BYTE Ymax, BYTE Umax, BYTE Vmax)
{
   BYTE red, green, blue; 
   LPBYTE pBmpdata;	//-- pointer to the bitmap
   int y, u, v;
   int i; 
   short bytes=3; 


if(posx<0)
{
ww=ww+posx;
posx=0;
}
if(posy<0)
{
	hh=hh+posy;
	posy=0;
}

   int patchthresh=3; 
 
 
     UpdateData(TRUE);
    
     
	 short  patchobj=0;
    
     int patchcount[maxpatch];
     int patchxl[maxpatch];
     int patchyt[maxpatch];
     int patchxr[maxpatch];
	 int patchyb[maxpatch];
 
  
     int    lastpatchcol[maxpatch];
	 int    lastpatchrow[maxpatch];
    for(i=0;i<maxpatch;i++)
	{
	   patchxl[i]=0;
	   patchxr[i]=0;
	   patchyt[i]=0;
	   patchyb[i]=0;
       patchcount[i]=0;
       lastpatchcol[i]=0;
	   lastpatchrow[i]=0;
	}




/////patch parameter end
     
   
   
  
 // int test=0;

  



	 //-- scan
for (int row=10; row<hh-10 && (row+posy)<ImgH; row++)
		{
			
			for (int col=10; col<ww-10 && (col+posx)<ImgW; col++)
			{
               
			pBmpdata = 	lpDIBBits 	+
				(ImgW*(row+posy) + col+posx)*bytes;
               
				  blue  = *pBmpdata;
                  green = *(pBmpdata+1);
				  red   = *(pBmpdata+2);

				bool Scaned=FALSE; 
				for(i=1;i<=patchobj;i++)
				  if(col<=patchxr[i] && col>=patchxl[i] &&
        		  row>=patchyt[i] && row<=patchyb[i])
					  Scaned=TRUE;
				   

				
                
                   if(!Scaned)
				   {

				RGB2YUV(red,green,blue,y,u,v);
				   
			   
			   ////////patch	 
				if (( (Ymin<=y) && (y<=Ymax) ) &&
				     ( (Umin<=u) && (u<=Umax) ) &&
				     ( (Vmin<=v) && (v<=Vmax) ) )

				{   
                   patchobj++;    
				
			   patchcount[patchobj]++;
               int prex=col-1;
			   int prey=row-1;

               int endx=col+1;
                 int endy=row+1;
                   bool finished=false;
             short emptycolnum=0;

	         short emptyrownum=0;

             short emptycolnum0=0;
             short emptyrownum0=0;

			  if(patchobj==2)
				 {

//				 AfxMessageBox("hello");
				 
				 
				 }
			    
			  while(!finished)
				  {
				          
				/////////// start col scan
					  bool emptycol1=true;
					
					  for(i=prex-1;i<=endx+1;i++)
					  {	
					 pBmpdata  = 	lpDIBBits 	+
				(ImgW*(endy+posy) + i+posx)*bytes;
                  blue  = *pBmpdata;
                  green = *(pBmpdata+1);
				  red   = *(pBmpdata+2);
				  RGB2YUV(red,green,blue,y,u,v);

				   	if (( (Ymin<=y) && (y<=Ymax) ) &&
				     ( (Umin<=u) && (u<=Umax) ) &&
				     ( (Vmin<=v) && (v<=Vmax) ) )
					{
						if(i!=prex && i!=endx &&
							i!=prex-1 && i!=endx+1
							)
				        patchcount[patchobj]++;

					
				   
                         emptycol1=false;
					// break;
					}			  
					  }

					   bool emptycol2=true;
					
					  for(i=prex-1;i<=endx+1;i++)
					  {	
					 pBmpdata  = 	lpDIBBits 	+
				(ImgW*(endy+1+posy) + i+posx)*bytes;
                  blue  = *pBmpdata;
                  green = *(pBmpdata+1);
				  red   = *(pBmpdata+2);
				  RGB2YUV(red,green,blue,y,u,v);

				   	if (( (Ymin<=y) && (y<=Ymax) ) &&
				     ( (Umin<=u) && (u<=Umax) ) &&
				     ( (Vmin<=v) && (v<=Vmax) ) )
					{
						
						if(i!=prex && i!=endx &&
							i!=prex-1 && i!=endx+1
							)
				        patchcount[patchobj]++;


					
				   
                   emptycol2=false;
					// break;
					}			  
					  }



                      
                           ////end of col scan

					  ///start of row scan
				      bool emptyrow1=true;
					
                  for(i=prey-1;i<=endy+1;i++)
					  {	
						  pBmpdata = 	lpDIBBits 	+
				(ImgW*(i+posy) + endx+posx)*bytes;
                   blue  = *pBmpdata;
                  green = *(pBmpdata+1);
				  red   = *(pBmpdata+2);
				  	RGB2YUV(red,green,blue,y,u,v);

				   	if (( (Ymin<=y) && (y<=Ymax) ) &&

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -