2013-02-08 72 views
0

我想在opencv中使用cvCamShift函數。我寫了一個類做this.this是它的代碼:如何cvCamShift工作良好

#include <cv.h> 
#include <highgui.h> 
using namespace std; 


class Tracker{ 

    // File-level variables 
    IplImage * pHSVImg; // the input image converted to HSV color mode 
    IplImage * pHueImg; // the Hue channel of the HSV image 
    IplImage * pMask; // this image is used for masking pixels 
    IplImage * pProbImg; // the face probability estimates for each pixel 
    CvHistogram * pHist; // histogram of hue in the original face image 

    CvRect prevFaceRect; // location of face in previous frame 
    CvBox2D faceBox;  // current face-location estimate 

    int vmin; 
    int vmax; 
    int smin; 

    void updateHueImage(IplImage* pImg); 
public: 
    Tracker(IplImage * pImg, CvRect pFaceRect); 
    ~Tracker(); 
    CvBox2d track(IplImage* pImg); 

}; 



Tracker::Tracker(IplImage * pImg, CvRect pFaceRect){ 
    // File-level variables 
    int nHistBins = 30;     // number of histogram bins 
    float rangesArr[] = {0,180};   // histogram range 
    vmin = 10; 
    vmax = 256; 
    smin = 55; 
    float * pRanges = rangesArr; 

    pHSVImg = cvCreateImage(cvGetSize(pImg), 8, 3); 
    pHueImg = cvCreateImage(cvGetSize(pImg), 8, 1); 
    pMask = cvCreateImage(cvGetSize(pImg), 8, 1); 
    pProbImg = cvCreateImage(cvGetSize(pImg), 8, 1); 

    pHist = cvCreateHist(1, &nHistBins, CV_HIST_ARRAY, &pRanges, 1); 

    float maxVal = 0.f; 

    // Create a new hue image 
    updateHueImage(pImg); 

    // Create a histogram representation for the face 
    cvSetImageROI(pHueImg, pFaceRect); 
    cvSetImageROI(pMask, pFaceRect); 
    cvCalcHist(&pHueImg, pHist, 0, pMask); 
    cvGetMinMaxHistValue(pHist, 0, &maxVal, 0, 0); 
    cvConvertScale(pHist->bins, pHist->bins, maxVal? 255.0/maxVal : 0, 0); 
    cvResetImageROI(pHueImg); 
    cvResetImageROI(pMask); 

    // Store the previous face location 
    prevFaceRect = pFaceRect; 


} 


Tracker::~Tracker(){ 
    cvReleaseImage(&pHSVImg); 
    cvReleaseImage(&pHueImg); 
    cvReleaseImage(&pMask); 
    cvReleaseImage(&pProbImg); 

    cvReleaseHist(&pHist); 
} 

void Tracker::updateHueImage(IplImage * pImg) 
{ 
    // Convert to HSV color model 
    cvCvtColor(pImg, pHSVImg, CV_BGR2HSV); 

    // Mask out-of-range values 
    cvInRangeS(pHSVImg, cvScalar(0, smin, MIN(vmin,vmax), 0), 
       cvScalar(180, 256, MAX(vmin,vmax) ,0), pMask); 

    // Extract the hue channel 
    cvSplit(pHSVImg, pHueImg, 0, 0, 0); 
} 

CvBox2D Tracker::track(IplImage * pImg) 
{ 
    CvConnectedComp components; 


    updateHueImage(pImg); 


    cvCalcBackProject(&pHueImg, pProbImg, pHist); 
    cvAnd(pProbImg, pMask, pProbImg, 0); 

    cvCamShift(pProbImg, prevFaceRect, 
       cvTermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1), 
       &components, &faceBox); 


    prevFaceRect = components.rect; 
    faceBox.angle = -faceBox.angle; 

    return faceBox; 
} 

它做工不錯,但是當跟蹤對象超出框架或財產以後的來自它的面前,它使運行時錯誤。我的一個朋友的代碼與我的代碼非常相似,即使在最糟糕的情況下也不會產生任何運行時錯誤。請指導我如何調試這個。

+0

運行時錯誤是什麼樣的? – Walfie

+0

visual studio說:訪問違規讀取位置 – akrami

回答

0

我認爲,當被跟蹤物體離開框架或其前面有什麼東西出現時,參數prevFaceRect將爲空,可能是因爲它顯示錯誤。因此,在調用這個函數

cvCamShift(pProbImg, prevFaceRect, 
       cvTermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1), 
       &components, &faceBox); 

添加一個檢查之前,要知道prevFaceRect是否爲空或不是

+0

我檢查了prevFaceRect變量。在上次通話中,它是-858993560。在它之前,我已經將可接受的值發送給cvCamShift函數,但它已經返回了這個矩形。 – akrami

+0

我不確定我明白你的意思。你的意思是prevFaceRect的值是-858993560 ?,所以這意味着它的垃圾值rt?因此,在分配「prevFaceRect = components.rect」之前,設置if條件來檢查components.rect.width> 0,如果它比0分配值。我的意思是這樣的 if(components.rect.width> 0) prevFaceRect = components.rect –

相關問題