demoDlg.cpp
资源名称:meanshift.rar [点击查看]
上传用户:gzysjn
上传日期:2013-07-12
资源大小:225k
文件大小:10k
源码类别:
2D图形编程
开发平台:
Visual C++
- // demoDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "demo.h"
- #include "demoDlg.h"
- //#include "cvaux.h"
- #include "cv.h" // include core library interface
- #include "highgui.h" // include GUI library interface
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- ////////////////////////////////////////////
- IplImage *image = 0, *hsv = 0, *hue = 0, *mask = 0, *backproject = 0, *histimg = 0;
- CvHistogram *hist = 0;
- int backproject_mode = 0;
- int select_object = 0;
- int track_object = 0;
- int show_hist = 1;
- CvPoint origin;
- CvRect selection;
- CvRect track_window;
- CvBox2D track_box;
- CvConnectedComp track_comp;
- int hdims = 16;
- float hranges_arr[] = {0,180};
- float* hranges = hranges_arr;
- int vmin = 10, vmax = 256, smin = 30;
- void on_mouse(int event, int x, int y, int flags, void *param);
- /////////////////////////////////////////////////////////////////////////////
- // CDemoDlg dialog
- CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CDemoDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CDemoDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CDemoDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CDemoDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
- //{{AFX_MSG_MAP(CDemoDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON1, OnButtonEdge)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDemoDlg message handlers
- BOOL CDemoDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // 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
- // TODO: Add extra initialization here
- return TRUE; // return TRUE unless you set the focus to a control
- }
- // 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 CDemoDlg::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);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CDemoDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CDemoDlg::OnOK()
- {
- // TODO: Add extra validation here
- CDialog::OnOK();
- }
- void CDemoDlg::OnButtonEdge()
- {
- CFileDialog dlg(TRUE, _T("*.avi"), "",
- OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,
- "AVI files (*.avi) |*.avi|All Files (*.*)|*.*||",NULL);
- CString strFileName;
- char title[]= {"打开视频文件"};
- dlg.m_ofn.lpstrTitle= title;
- if (dlg.DoModal() == IDOK) {
- strFileName= dlg.GetPathName();
- }
- IplImage* tmp_frame = NULL;
- CvCapture* capture = NULL;
- capture= cvCaptureFromFile(strFileName);
- tmp_frame = cvQueryFrame(capture);
- if(!tmp_frame)
- {
- printf("bad video n");
- exit(0);
- }
- cvNamedWindow( "Histogram", 1 );
- cvNamedWindow( "MeanShiftDemo", 1 );
- cvSetMouseCallback( "MeanShiftDemo",on_mouse, 0 );
- //cvCreateTrackbar( "Vmin", "MeanShiftDemo", &vmin, 256, 0 );
- // cvCreateTrackbar( "Vmax", "MeanShiftDemo", &vmax, 256, 0 );
- // cvCreateTrackbar( "Smin", "MeanShiftDemo", &smin, 256, 0 );
- bool bBackProject=true;
- CvConnectedComp comp;
- for(;;)
- {
- IplImage* frame = 0;
- int i, bin_w, c;
- frame = cvQueryFrame( capture );
- if( !frame )
- break;
- if( !image )
- {
- /* allocate all the buffers */
- image = cvCreateImage( cvGetSize(frame), 8, 3 );
- image->origin = frame->origin;
- hsv = cvCreateImage( cvGetSize(frame), 8, 3 );
- hue = cvCreateImage( cvGetSize(frame), 8, 1 );
- mask = cvCreateImage( cvGetSize(frame), 8, 1 );
- backproject = cvCreateImage( cvGetSize(frame), 8, 1 );
- hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );
- histimg = cvCreateImage( cvSize(320,200), 8, 3 );
- cvZero( histimg );
- }
- cvCopy( frame, image, 0 );
- cvCvtColor( image, hsv, CV_BGR2HSV );
- if( track_object )
- {
- int _vmin = vmin, _vmax = vmax;
- cvInRangeS( hsv, cvScalar(0,smin,MIN(_vmin,_vmax),0),
- cvScalar(180,256,MAX(_vmin,_vmax),0), mask );
- cvSplit( hsv, hue, 0, 0, 0 );
- if( track_object < 0 )
- {
- float max_val = 0.f;
- cvSetImageROI( hue, selection );
- cvSetImageROI( mask, selection );
- cvCalcHist( &hue, hist, 0, mask );
- cvGetMinMaxHistValue( hist, 0, &max_val, 0, 0 );
- cvConvertScale( hist->bins, hist->bins, max_val ? 255. / max_val : 0., 0 );
- cvResetImageROI( hue );
- cvResetImageROI( mask );
- track_window = selection;
- track_object = 1;
- cvZero( histimg );
- bin_w = histimg->width / hdims;
- for( i = 0; i < hdims; i++ )
- {
- int val = cvRound( cvGetReal1D(hist->bins,i)*histimg->height/255 );
- CvScalar color = hsv2rgb(i*180.f/hdims);
- cvRectangle( histimg, cvPoint(i*bin_w,histimg->height),
- cvPoint((i+1)*bin_w,histimg->height - val),
- color, -1, 8, 0 );
- }
- }
- // if(bBackProject)
- // {
- cvCalcBackProject( &hue, backproject, hist );
- cvAnd( backproject, mask, backproject, 0 );
- bBackProject=false;
- // }
- cvMeanShift( backproject, track_window,
- cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 1 ),
- &comp );
- //cvCamShift( backproject, track_window,
- //// cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ),
- // &track_comp, &track_box );
- //track_window = track_comp.rect;
- track_window= comp.rect;
- CvPoint pt1,pt2;
- pt1.x=comp.rect.x;
- pt1.y=comp.rect.y;
- pt2.x=comp.rect.x+comp.rect.width;
- pt2.y=(comp.rect.y+comp.rect.height);
- cvRectangle(image,pt1,pt2,CV_RGB(0,0,255));
- if( backproject_mode )
- cvCvtColor( backproject, image, CV_GRAY2BGR );
- // if( image->origin )
- // track_box.angle = -track_box.angle;
- // cvEllipseBox( image, track_box, CV_RGB(255,0,0), 3, CV_AA, 0 );
- }
- if( select_object && selection.width > 0 && selection.height > 0 )
- {
- cvSetImageROI( image, selection );
- cvXorS( image, cvScalarAll(255), image, 0 );
- cvResetImageROI( image );
- }
- cvShowImage( "MeanShiftDemo", image );
- cvShowImage( "Histogram", histimg );
- c = cvWaitKey(10);
- if( c == 27 )
- break;
- switch( c )
- {
- case 'b':
- backproject_mode ^= 1;
- break;
- case 'c':
- track_object = 0;
- cvZero( histimg );
- break;
- case 'h':
- show_hist ^= 1;
- if( !show_hist )
- cvDestroyWindow( "Histogram" );
- else
- cvNamedWindow( "Histogram", 1 );
- break;
- default:
- ;
- Sleep(100);
- }
- }
- cvReleaseCapture( &capture );
- cvDestroyWindow("CamShiftDemo");
- }
- void on_mouse(int event, int x, int y, int flags, void *param)
- {
- if( !image )
- return;
- if( image->origin )
- y = image->height - y;
- if( select_object )
- {
- selection.x = MIN(x,origin.x);
- selection.y = MIN(y,origin.y);
- selection.width = selection.x + CV_IABS(x - origin.x);
- selection.height = selection.y + CV_IABS(y - origin.y);
- selection.x = MAX( selection.x, 0 );
- selection.y = MAX( selection.y, 0 );
- selection.width = MIN( selection.width, image->width );
- selection.height = MIN( selection.height, image->height );
- selection.width -= selection.x;
- selection.height -= selection.y;
- }
- switch( event )
- {
- case CV_EVENT_LBUTTONDOWN:
- origin = cvPoint(x,y);
- selection = cvRect(x,y,0,0);
- select_object = 1;
- break;
- case CV_EVENT_LBUTTONUP:
- select_object = 0;
- if( selection.width > 0 && selection.height > 0 )
- track_object = -1;
- break;
- }
- }
- CvScalar CDemoDlg::hsv2rgb(float hue)
- {
- int rgb[3], p, sector;
- static const int sector_data[][3]=
- {{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}};
- hue *= 0.033333333333333333333333333333333f;
- sector = cvFloor(hue);
- p = cvRound(255*(hue - sector));
- p ^= sector & 1 ? 255 : 0;
- rgb[sector_data[sector][0]] = 255;
- rgb[sector_data[sector][1]] = 0;
- rgb[sector_data[sector][2]] = p;
- return cvScalar(rgb[2], rgb[1], rgb[0],0);
- }