📄 blobtracker.cpp
字号:
/*M/////////////////////////////////////////////////////////////////////////////////////////// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.//// By downloading, copying, installing or using the software you agree to this license.// If you do not agree to this license, do not download, install,// copy or use the software.////// Intel License Agreement// For Open Source Computer Vision Library//// Copyright (C) 2002, Intel Corporation, all rights reserved.// Third party copyrights are property of their respective owners.//// Redistribution and use in source and binary forms, with or without modification,// are permitted provided that the following conditions are met://// * Redistributions of source code must retain the above copyright notice,// this list of conditions and the following disclaimer.//// * Redistributions in binary form must reproduce the above copyright notice,// this list of conditions and the following disclaimer in the documentation// and/or other materials provided with the distribution.//// * The name of Intel Corporation may not be used to endorse or promote products// derived from this software without specific prior written permission.//// This software is provided by the copyright holders and contributors "as is" and// any express or implied warranties, including, but not limited to, the implied// warranties of merchantability and fitness for a particular purpose are disclaimed.// In no event shall the Intel Corporation or contributors be liable for any direct,// indirect, incidental, special, exemplary, or consequential damages// (including, but not limited to, procurement of substitute goods or services;// loss of use, data, or profits; or business interruption) however caused// and on any theory of liability, whether in contract, strict liability,// or tort (including negligence or otherwise) arising in any way out of// the use of this software, even if advised of the possibility of such damage.////M*/// ////////////////////////////////////////////////////////////////////////////// BlobTracker.cpp//// This file contains the bulk of the implementation of the // BlobTracker class.//// ////////////////////////////////////////////////////////////////////////////#include <stack>#include <algorithm>#include <comcat.h>#include "cvstreams.h"#include "BlobTracker.h"#include "BlobTrkObject.h"#include "BlobTrackerPropertyPage.h"static void FloodFill(unsigned char *image, int width, int height, int start_x, int start_y, int new_color, CvRect &rect, int &area);//// CreateInstance//// Used by the DirectShow base classes to create instances//CUnknown *BlobTracker::CreateInstance(IUnknown *outer, HRESULT *phr){ CUnknown *punk = new BlobTracker(outer, phr); if (punk == NULL) { *phr = E_OUTOFMEMORY; } return punk;}// ////////////////////////////////////////////////////////////////////////////// BlobTracker::BlobTracker()//// Constructor.//// ////////////////////////////////////////////////////////////////////////////BlobTracker::BlobTracker(IUnknown *outer, HRESULT *phr) : CUnknown(NAME("Blob Tracker"), outer){ // Set member variables to defaults m_output_options = IBlobTracker::OUTPUT_CROSSHAIRS; m_num_objects = 0; m_pixel_threshold = 99; m_size_threshold = 3; m_distance_threshold = 80; m_image = NULL; m_tmp_image = NULL; m_objects = new BlobTrackerObjectList;}// ////////////////////////////////////////////////////////////////////////////// BlobTracker::~BlobTracker()//// Destructor.//// ////////////////////////////////////////////////////////////////////////////BlobTracker::~BlobTracker(){ cvReleaseImage( &m_image ); cvReleaseImage( &m_tmp_image ); delete m_objects;}HRESULT BlobTracker::NonDelegatingQueryInterface(REFIID iid, void **ppv){ if (iid == IID_IBlobTracker) return GetInterface((IUnknown *)(void *)static_cast<IBlobTracker *>(this), ppv); if (iid == IID_ITracker) return GetInterface((IUnknown *)(void *)static_cast<ITracker *>(this), ppv); return CUnknown::NonDelegatingQueryInterface(iid, ppv);}// ITracker methodsSTDMETHODIMP BlobTracker::CheckFormat(IplImage *image_header){ if (image_header->depth != IPL_DEPTH_8U || image_header->width % 4 != 0) { return E_FAIL; } if (image_header->nChannels == 1 && strncmp(image_header->colorModel, "GRAY", 4) == 0) { return NOERROR; } if (image_header->nChannels == 3 && strncmp(image_header->colorModel, "RGB", 3) == 0) { return NOERROR; } return E_FAIL;}STDMETHODIMP BlobTracker::SetFormat(IplImage *image_header){ HRESULT hr = CheckFormat(image_header); if (FAILED(hr)) return E_FAIL; m_image_format = *image_header; m_image = cvCreateImage( cvSize(image_header->width,image_header->height), IPL_DEPTH_8U, 1 ); m_image->origin = image_header->origin; if (image_header->nChannels != 1) { m_tmp_image = cvCreateImage( cvSize(image_header->width,image_header->height), IPL_DEPTH_8U, 1 ); m_image->origin = image_header->origin; } return NOERROR;}STDMETHODIMP BlobTracker::SetNumberOfObjects(int num_objects){ if (num_objects < 1) return E_FAIL; m_num_objects = num_objects; return NOERROR;}STDMETHODIMP BlobTracker::GetPropertyPage(GUID *page){ *page = CLSID_BlobTrackerPropertyPage; return NOERROR;}// ////////////////////////////////////////////////////////////////////////////// BlobTracker::Process()//// Performs the processing of one frame.//// Inputs: // Input frame data (src)//// ////////////////////////////////////////////////////////////////////////////static inline bool MatchFormat(IplImage *image, IplImage *format){ return (image->nChannels == format->nChannels && image->width == format->width && image->height == format->height && image->depth == format->depth && image->dataOrder == format->dataOrder && image->origin == format->origin && strncmp(image->channelSeq, format->channelSeq, 4) == 0);}// Used for sorting in descending order by sizestatic inline bool size_cmp(const BlobTrackerObject &o1, const BlobTrackerObject &o2){ return o1.GetSize() > o2.GetSize();}// compute the square of the distance between two points.static inline int distance(const CvPoint &p1, const CvPoint &p2){ int dx = p1.x - p2.x; int dy = p1.y - p2.y; return dx*dx + dy*dy;}static inline bool InRect(const CvRect &rect, const CvPoint &p){ return (p.x >= rect.x && p.x < rect.x + rect.width && p.y >= rect.y && p.y < rect.y + rect.height);}STDMETHODIMP BlobTracker::Process(IplImage *image){ // Make sure we've gone through our initialization stage if (m_image == NULL || m_num_objects == 0) return E_FAIL; // Make sure the image format matches what is expected. if (!MatchFormat(image, &m_image_format)) return E_FAIL; // Convert the image to gray if required. IplImage *gray_image = image; if (image->nChannels != 1) { cvCvtColor(image, m_tmp_image, CV_BGR2GRAY ); gray_image = m_tmp_image; } // Dilation or blurring of the image enlarges small points to make them more visible // and connects any points with gaps so they become a single point. // Blurring has the advantage that it helps filter out noise, whereas dilation // exacerbates noise. However, blurring reduces the intensity of the bright spots, // so the threshold must be set lower. // Since background noise isn't a big problem with the cameras we use, on the // whole we determined that dilation improves tracking. cvDilate(gray_image, m_image, 0, 2); //iplBlur(gray_image, m_image, 3, 3, 1, 1); // Copy the modified image back into the input image, so the // image seen in the video window is the one that is actually processed. // This is fairly expensive, especially if color conversion is required // (i.e, if the input image is RGB); this step may be eliminated with no // loss of tracking functionality, in which case the video window will // show the original image rather than the processed image. (Crosshairs // are drawn on the input image in any case.) if (image->nChannels == 1) cvCopy(m_image, image); else cvCvtColor(m_image, image, CV_GRAY2BGR );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -