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

📄 vidcapdlgdlg.cpp

📁 capture the image of the inpu object from the user and then detect image edge of the object
💻 CPP
字号:
// VidCapDlgDlg.cpp : implementation file
//

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


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

int Start_Gausian=0;
int Start_Sobel=0;
int Start_Laplacian=0;
int Start_rotation=0;
int rotation_angle=0;
int Start_color=0;
int depo[76800];
long int hist[76800];
BLOBS BlobPoint[100];
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About




class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVidCapDlgDlg dialog

CVidCapDlgDlg::CVidCapDlgDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVidCapDlgDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVidCapDlgDlg)
	m_Black = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVidCapDlgDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVidCapDlgDlg)
	DDX_Check(pDX, IDC_CHECK_BLACK, m_Black);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVidCapDlgDlg, CDialog)
	//{{AFX_MSG_MAP(CVidCapDlgDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_SHOW, OnButtonShow)
	ON_WM_TIMER()
	ON_COMMAND(ID_MENUITEM_VIDEOSOURCE, OnMenuitemVideosource)
	ON_COMMAND(ID_MENUITEM_SAMPLECOLOR, OnMenuitemSamplecolor)
	ON_COMMAND(ID_MENUITEM_LOADCOLOR, OnMenuitemLoadcolor)
	ON_BN_CLICKED(IDC_BUTTON_TRACK, OnButtonTrack)
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
	ON_BN_CLICKED(IDC_CHECK_BLACK, OnCheckBlack)
	ON_BN_CLICKED(IDC_Start_Sobel, OnStartSobel)
	ON_BN_CLICKED(IDC_Gausian_ON, OnGausianON)
	ON_BN_CLICKED(IDC_Laplacian_ON, OnLaplacianON)
	ON_BN_CLICKED(IDC_rotate, Onrotate)
	ON_BN_CLICKED(IDC_Stop_All, OnStopAll)
	ON_BN_CLICKED(IDC_colour_detect, Oncolourdetect)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVidCapDlgDlg message handlers

BOOL CVidCapDlgDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	

     	if(!(port.OpenPort("com1"))){
		MessageBox("Cannot open Communication Port.Please\nquit the application & re-start your PC.","Error",MB_OK+MB_ICONERROR);
	}
	else{
		if(!(port.ConfigurePort(19200,8,1,NOPARITY ,ONESTOPBIT ))){
			MessageBox("Cannot Configure Communication Port","Error",MB_OK+MB_ICONERROR);
			port.ClosePort();}
		else{
			if(!(port.SetCommunicationTimeouts(0,100,0,0,0))){
				MessageBox("Cannot Configure Communication Timeouts","Error",MB_OK+MB_ICONERROR);
			port.ClosePort();}
		
			}

	}


	// TODO: Add extra initialization here
	
     	if(!m_FrameGrabber.GetSafeHwnd())
	{
		m_FrameGrabber.Create(0,9,this);
		lpbi = m_FrameGrabber.GetDIB();   
        h=lpbi->bmiHeader.biHeight;
        w=lpbi->bmiHeader.biWidth;
		Res = int((lpbi->bmiHeader.biBitCount)/8);
		//SetTimer(1, 100, NULL);
	}
	//	m_Frame=0;

   gDC=GetDC();

  return TRUE;  // return TRUE  unless you set the focus to a control
}

void CVidCapDlgDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CVidCapDlgDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);


         	if(!m_FrameGrabber.GetSafeHwnd())
	{
		CRect rc;
		GetClientRect(rc);	
	}
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CVidCapDlgDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CVidCapDlgDlg::OnButtonShow() 
{   
  

   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);
}

void CVidCapDlgDlg::OnTimer(UINT nIDEvent) 
{
LPBYTE pBmpdata;	//-- pointer to the bitmap
LPBYTE pBmpdata2;
//LPBYTE pBmpdata3;
int CandidateBlobs;
int *pBmpdata3;
int *pBmpdata4;

int Y, row, col,i;
float norm, dist;
float n_red, n_green, n_blue;

float ref_red=(float)0.645;
float ref_blue=(float)0.165;
float ref_green=(float)0.188;


int num=0;

BYTE red, green, blue,temp; 
pBmpdata2= (BYTE *) malloc (h*w*sizeof(BYTE));
pBmpdata3= (int *) malloc (h*w*sizeof(int));
pBmpdata4= (int *) malloc (h*w*sizeof(int));
	if(nIDEvent==1)
	{
   lpbi = m_FrameGrabber.GetDIB();  
   lpDIBBits = (LPBYTE)((LPDWORD)(lpbi->bmiColors + 
   lpbi->bmiHeader.biClrUsed) + 
   ((lpbi->bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));

  StretchDIBits(gDC->m_hDC,10,0,w,h,0,0,w,h,lpDIBBits,lpbi,DIB_RGB_COLORS,SRCCOPY);


////// convert colour to gray and store in memory
int t;
for (row=0; row<h; row++){ t=w-1;			
for (col=0; col<w; col++)
			{
	    	 pBmpdata = lpDIBBits+(w*row + col)*Res;
			 red=*pBmpdata;
             blue=*(pBmpdata+1);
			 green=*(pBmpdata+2);
			Y = BYTE(0.299*red + 0.587*green + 0.114*blue + 0.500);
			*(pBmpdata2+w*row + col)=Y;
			*(pBmpdata3+w*row+t)=Y; 
			*(pBmpdata4+w*row + col)=Y;

		//	depo[w*row+t]=Y;
			t--;//invert the image so to eliminate mirror effect.
	 }}



DrawRectTwoPoint(0, 0,50,50,w,255, pBmpdata3);



//////////////////Operations 
/////sobel
if (Start_Sobel==1){
int sum_val1=0;
int sum_val2=0;
float temp;
for (row=1; row<h-1; row++){			
for (col=1; col<w-1; col++){
temp=0;
sum_val1=0;
sum_val2=0;
sum_val1=(-1)*(*(pBmpdata2+(w*row-1)+ col-1))+(-2)*(*(pBmpdata2+(w*row-1)+ col))+(-1)*(*(pBmpdata2+(w*row-1)+ col+1));
sum_val1+=(*(pBmpdata2+(w*row+1)+ col-1))+(2)*(*(pBmpdata2+(w*row+1) + col))+(*(pBmpdata2+(w*row+1)+col+1));
sum_val2=(-1)*(*(pBmpdata2+(w*row-1)+ col-1))+(-2)*(*(pBmpdata2+(w*row)+ col-1))+(-1)*(*(pBmpdata2+(w*row+1)+ col-1));
sum_val2+=(*(pBmpdata2+(w*row-1)+ col+1))+(2)*(*(pBmpdata2+(w*row) + col+1))+(*(pBmpdata2+(w*row+1)+col+1));
 temp = abs ((sum_val1*sum_val1)+(sum_val2*sum_val2));
		    temp= (float)sqrt(temp);

if (temp>70) *(pBmpdata3+w*row + col)=255;
else *(pBmpdata3+w*row + col)=0;
		}
		}
}

// laplacian edge detector	
if (Start_Laplacian==1){
int sum_val=0;
for (row=1; row<h-1; row++){			
for (col=1; col<w-1; col++){
sum_val=(-4)*(*(pBmpdata2+w*row + col))+(*(pBmpdata2+w*row + col-1))+(*(pBmpdata2+w*row + col+1))+(*(pBmpdata2+w*(row-1)+ col))+(*(pBmpdata2+w*(row+1) + col));
if (sum_val>15) *(pBmpdata3+w*row + col)=255;
else *(pBmpdata3+w*row + col)=0;}}


  } 

// laplacian edge detector	
if (Start_Gausian==1){
//int mask[25]={0,1,2,1,0, 1,3,4,3,1,2,4,8,4,2,1,3,4,3,1,0,1,2,1,0};
int mask[81]={0,0,1,1,1,1,1,0,0,
			  0,1,2,3,3,3,2,1,0,
			  1,2,3,6,7,6,3,2,1,
			  1,3,6,9,11,9,6,3,1,
			  1,3,7,11,12,11,7,3,1,
	          1,3,6,9,11,9,6,3,1,
	          1,2,3,6,7,6,3,2,1,
	          0,1,2,3,3,3,2,1,0,
              0,0,1,1,1,1,1,0,0};

conv2d (pBmpdata2, pBmpdata3, mask, row, col, 9);
							} 

if (Start_rotation==1){
rotate(row, col, rotation_angle, pBmpdata2, pBmpdata3);
if (rotation_angle<360){rotation_angle=rotation_angle+4;}
else rotation_angle=0;
median3x3(pBmpdata3, pBmpdata3, row, col);

}

//////////////////////////////////////////////////
/// detect red colour
////////////////////////////////////////////////
if (Start_color==1){

////// detect colour
		for (int row=0; row<h; row++){			
	 for (int col=0; col<w; col++){
	    	 pBmpdata = lpDIBBits+(w*row + col)*Res;
				  red=*pBmpdata; blue=*(pBmpdata+1); green=*(pBmpdata+2);
			norm=(float)((red+green+blue)/3);  
	  if (norm==0) norm=1;
	  n_red=(float)(red/norm);
	  n_blue=(float)blue/norm;
	  n_green=(float)green/norm;
	  dist=(n_red-ref_red)*(n_red-ref_red)+(n_blue-ref_blue)*(n_blue-ref_blue);
	  dist=(float)sqrt((double)dist);
	 if (dist>0.8) *(pBmpdata2+w*row + col)=0;
	else *(pBmpdata2+w*row + col)=255;
	 }}

binarysmooth(1, 1, h-1, w-1, w, pBmpdata2, pBmpdata3);
num=LabelObjects (1, 1, h-1, w-1,w, pBmpdata2, pBmpdata3);

for (i=0;i<76800;i++){hist[i]=0;}
int obj_count;
obj_count=Histogram (1, 1, h-1, w-1, w, 1,num,hist, pBmpdata3);
if (obj_count==0 ) obj_count=1;

CandidateBlobs=TestObjects(1, 1, h-1, w-1, w, pBmpdata3, 1,num,hist,obj_count, BlobPoint);

//mark blob area on the image
for (int k=0;k<CandidateBlobs;k++){
DrawRectTwoPoint(BlobPoint[k].CornX0, BlobPoint[k].CornY0,BlobPoint[k].CornX1,BlobPoint[k].CornY1,w,255, pBmpdata4);}
int cent_X=BlobPoint[0].CenterY;
int cent_Y=BlobPoint[0].CenterX;
depo[w*(cent_X)+cent_Y]=255;
depo[w*(cent_X-1)+cent_Y]=255;depo[w*(cent_X+1)+cent_Y]=255;
depo[w*(cent_X)+cent_Y+1]=255;depo[w*(cent_X)+cent_Y-1]=255;
depo[w*(cent_X+1)+cent_Y+1]=255;depo[w*(cent_X-1)+cent_Y-1]=255;
depo[w*(cent_X+1)+cent_Y-1]=255;depo[w*(cent_X-1)+cent_Y+1]=255;

}// end of clour track

///////////////////////////////////////////////////////////////////////////////////////////////////////
if (Start_color==1){

BYTE temp0;
for ( row=0; row<h; row++){	
	 for ( int col=0; col<w; col++){
	    	 pBmpdata = lpDIBBits+(w*row + col)*Res;
				 temp0=*(pBmpdata4+w*row + col);
				 temp=depo[w*row+col];
				 if (temp+temp0<255) temp=temp+temp0;else temp=255;
				  *pBmpdata=temp;
                  *(pBmpdata+1)=temp;
			      *(pBmpdata+2)=temp;
                  
	 }}

}

else {

for ( row=0; row<h; row++){	
	 for ( int col=0; col<w; col++){
	    	 pBmpdata = lpDIBBits+(w*row + col)*Res;
				 temp=*(pBmpdata3+w*row + col);
				  *pBmpdata=temp;
                  *(pBmpdata+1)=temp;
				  *(pBmpdata+2)=temp;
                  
	 }}

}

StretchDIBits(gDC->m_hDC,10,300,w,h,0,0,w,h,lpDIBBits,lpbi,DIB_RGB_COLORS,SRCCOPY);


free (pBmpdata4);
free (pBmpdata3);
free (pBmpdata2);



///////////  capture is here

  //  UpdateData(TRUE);
 
	
	}
	CDialog::OnTimer(nIDEvent);
}

void CVidCapDlgDlg::OnMenuitemVideosource() 
{

	if(m_FrameGrabber.GetSafeHwnd())
	{
		m_FrameGrabber.VideoSourceDialog();

	}
	
}

void CVidCapDlgDlg::OnMenuitemSamplecolor() 
{
	KillTimer(1);
	m_FrameGrabber.DestroyWindow();

    SampleDlg* sap;
    sap=new SampleDlg;
    sap->Create(IDD_DIALOG_SAP); 
    sap->ShowWindow(SW_SHOW); 	

}




void CVidCapDlgDlg::OnMenuitemLoadcolor() 
{

	
FILE* fp;

fp=fopen(ColorFile,"r");
fscanf(fp,"%d %d %d %d %d %d\n ",
		&colorinfor->redcolYmin,&colorinfor->redcolUmin,&colorinfor->redcolVmin,
		&colorinfor->redcolYmax,&colorinfor->redcolUmax,&colorinfor->redcolVmax);
	
fscanf(fp,"%d %d %d %d %d %d\n ",
		&colorinfor->bluecolYmin,&colorinfor->bluecolUmin,&colorinfor->bluecolVmin,
		&colorinfor->bluecolYmax,&colorinfor->bluecolUmax,&colorinfor->bluecolVmax);

fscanf(fp,"%d %d %d %d %d %d\n ",
		&colorinfor->greencolYmin,&colorinfor->greencolUmin,&colorinfor->greencolVmin,
		&colorinfor->greencolYmax,&colorinfor->greencolUmax,&colorinfor->greencolVmax);

fscanf(fp,"%d %d %d %d %d %d\n ",
		&colorinfor->othercolYmin,&colorinfor->othercolUmin,&colorinfor->othercolVmin,
		&colorinfor->othercolYmax,&colorinfor->othercolUmax,&colorinfor->othercolVmax);
fclose(fp);

}

void CVidCapDlgDlg::OnButtonTrack() 
{
	SetTimer(1,100,NULL);
}







void CVidCapDlgDlg::OnButtonStop() 
{
KillTimer(1);
}

void CVidCapDlgDlg::OnCheckBlack() 
{
	// TODO: Add your control notification handler code here
}


void CVidCapDlgDlg::OnStartSobel() 
{
Start_Sobel=1; Start_Gausian=0; Start_Laplacian=0;Start_rotation=0;		
Start_color=0;
}

void CVidCapDlgDlg::OnGausianON() 
{
Start_Sobel=0; Start_Gausian=1; Start_Laplacian=0;Start_rotation=0;	
Start_color=0;
}

void CVidCapDlgDlg::OnLaplacianON() 
{
Start_Sobel=0; Start_Gausian=0; Start_Laplacian=1;Start_rotation=0;		
Start_color=0;
}


void CVidCapDlgDlg::Onrotate() 
{
Start_Sobel=0; Start_Gausian=0; Start_Laplacian=0;Start_rotation=1;		
Start_color=0;
	
}

void CVidCapDlgDlg::OnStopAll() 
{
Start_Sobel=0; Start_Gausian=0; Start_Laplacian=0;Start_rotation=0;		
Start_color=0;
}

void CVidCapDlgDlg::Oncolourdetect() 
{
	// TODO: Add your control notification handler code here
Start_Sobel=0; Start_Gausian=0; Start_Laplacian=0;Start_rotation=0;		
Start_color=1;		
	
}

⌨️ 快捷键说明

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