BOOL CRunHistoRange::CallIpp(BOOL bMessage) { UpdateLUT(FALSE); CParmHistoRangeDlg dlg(this); UpdateData(&dlg, FALSE); if (dlg.DoModal() != IDOK) return FALSE; ShowHistogram(); UpdateLUT(); return TRUE; }
bool AdaptiveHistogramCamshift::ToggleShowHistogram() { if (!m_showHistogram) { ShowHistogram(); } else { HideHistogram(); } return m_showHistogram; }
void AdaptiveHistogramCamshift::Init(const CvSize& frameSize, const InitParams& initParams) { // Get frame size m_frameSize = frameSize; // Load init params. m_histDims = initParams.histDims; m_vMin = initParams.vMin; m_vMax = initParams.vMax; m_sMin = initParams.sMin; m_sBox = initParams.sBox; m_histRanges[0] = initParams.histRanges[0] * 0.5f; m_histRanges[1] = initParams.histRanges[1] * 0.5f; // Create histograms float* histRanges[2] = { &m_histRanges[0], &m_histRanges[1] }; m_hist = cvCreateHist(1, &m_histDims, CV_HIST_ARRAY, histRanges, 1); m_histSubdiv = cvCreateHist(1, &m_histDims, CV_HIST_ARRAY, histRanges, 1); m_histTrackWnd = cvCreateHist(1, &m_histDims, CV_HIST_ARRAY, histRanges, 1); // Create images m_imgHue = cvCreateImage(m_frameSize, 8, 1); m_imgMask = cvCreateImage(m_frameSize, 8, 1); m_imgHSV = cvCreateImage(m_frameSize, 8, 3); m_imgBackproject = cvCreateImage(m_frameSize, 8, 1); cvZero(m_imgBackproject); // Create histogram image m_histImg = cvCreateImage( m_frameSize, 8, 3 ); cvZero( m_histImg ); // Show controlsGUI, backproject, histogram if (initParams.showControlsGUI) { ShowControlsGUI(); } if (initParams.showBackproject) { ShowBackproject(); } if (initParams.showHistogram) { ShowHistogram(); } // Set initialized flag m_initialized = true; }
void AdaptiveHistogramCamshift::AdaptHistogram(IplImage* hue, IplImage* mask, IplImage* out) { // Prepare to analyze new track window const CvRect& updateHistRect = m_trackCompRect; // Now subdivide the track window and sum all pixels in each square // subdivision of size m_sBox using pixel values given by the // current hustogram. When we are done, we will normalize the sum // from each subdivision and then be able to tell roughly how // similar each subregion is to the track histogram. // Make sure box size is greater than or equal to 2, and protect from // changes made in GUI const int sBox = std::max(m_sBox, 2); if (sBox != m_sBox) { m_sBox = sBox; cvSetTrackbarPos(ControlNames[ControlName_SBox], m_controlsGUIWndName.c_str(), sBox); } std::vector<float> subdivs; int numRows, numCols; SubdivideSumTrackWnd(sBox, &numRows, &numCols, &subdivs); // Check window is less than sBox in size. if (subdivs.empty()) { return; } // Histogram for image subdivisions for (int i = 0; i < m_histDims; ++i) { float *bin = cvGetHistValue_1D(m_histTrackWnd, i); *bin = 0; } // Find the max vlaue of the subdivisions float maxVal = *std::max_element(subdivs.begin(), subdivs.end());; // DEBUG maxval //printf("maxVal: %f\n", maxVal); // Step through all subdivisions and weight them into a new histogram // if they are greater than minVal. The weight function is r^2 where // r is the ratio of the subdivision value to the max subdivision // value. if (maxVal > 0) { float minVal = maxVal * 0.125f; float* subdivsCur = &subdivs[0]; for (int i = 0; i < numRows; ++i) { for (int j = 0; j < numCols; ++j, ++subdivsCur) { // Create a box around this area CvPoint roiP1 = cvPoint(updateHistRect.x + sBox * j, updateHistRect.y + sBox * i); CvPoint roiP2 = cvPoint(roiP1.x + sBox, roiP1.y + sBox); if (*subdivsCur < minVal) { if(*subdivsCur > (minVal * 0.0625)) { // Get ratio to max subdivision float ratioMaxSubdiv = *subdivsCur / maxVal; // Get color of surrounding box CvScalar boxColor = colors[GREEN]; for (int colorInd = 0; colorInd < 3; ++colorInd) { boxColor.val[colorInd] *= ratioMaxSubdiv; } // Draw the box (darker green means less weight) if (out) { cvRectangle(out, roiP1, roiP2, boxColor, 1); } } else { // Draw a red box around this subdivision since it is not used if (out) { cvRectangle(out, roiP1, roiP2, colors[RED], 1 ); } } } else { // Get ratio to max subdivision float ratioMaxSubdiv = *subdivsCur / maxVal; // Get weight into histogram of track window float weightVal = ratioMaxSubdiv * ratioMaxSubdiv; // DEBUG weights //printf("w %d: %f\t", j, weightVal); // Get color of surrounding box CvScalar boxColor = colors[GREEN]; for (int colorInd = 0; colorInd < 3; ++colorInd) { boxColor.val[colorInd] *= ratioMaxSubdiv; } // Draw the box (darker green means less weight) if (out) { cvRectangle(out, roiP1, roiP2, boxColor, 1); } // Weight this subdivision into the histogram for the track window CvRect thisSubdivRect = cvRect(roiP1.x, roiP1.y, sBox, sBox); cvSetImageROI(hue, thisSubdivRect); cvSetImageROI(mask, thisSubdivRect); cvCalcHist(&hue, m_histSubdiv, 0, mask); cvResetImageROI(hue); cvResetImageROI(mask); // Weight this into the track window histogram for (int binNum = 0; binNum < m_histDims; ++binNum) { float* thisBin = cvGetHistValue_1D(m_histTrackWnd, binNum); *thisBin *= (1.0f - weightVal); *thisBin += static_cast<float>(cvGetReal1D(m_histSubdiv->bins, binNum)) * weightVal; } } } // DEBUG weights //printf("\n"); } // DEBUG weights //printf("\n"); } // DEBUG histograms //printf("Wnd BEFORE WT\n"); //for( int i = 0; i < m_histDims; i++ ) { // float *bin = cvGetHistValue_1D( m_histTrackWnd, i ); // printf("%2d: %3.1f ", i, *bin); // if( 0 == (i+1) % 8 ) { // printf("\n"); // } //} // Now scale track window histogram to tracking histogram scale float trackWndHistMaxVal; cvGetMinMaxHistValue(m_histTrackWnd, 0, &trackWndHistMaxVal, 0, 0); cvConvertScale(m_histTrackWnd->bins, m_histTrackWnd->bins, trackWndHistMaxVal ? HIST_SCALE / trackWndHistMaxVal : 0., 0); // Use aging to weight track window histogram into tracking histogram float averageBin = 0; for (int binNum = 0; binNum < m_histDims; ++binNum) { float* thisBin = cvGetHistValue_1D(m_hist, binNum); *thisBin *= (1.0f - (m_ageRatio / 100.0f)); *thisBin += static_cast<float>(cvGetReal1D(m_histTrackWnd->bins, binNum)) * (m_ageRatio / 100.0f); averageBin += *thisBin; } averageBin /= m_histDims; // DEBUG average bin //printf("Avg bin: %f.\n", averageBin); // See if this histogram is dying //if( averageBin < SMALLEST_AVG_HIST_BIN ) { // cvConvertScale( m_hist->bins, m_hist->bins, SMALLEST_AVG_HIST_BIN / averageBin, 0 ); // DEBUG averageBin //printf("Hist saved for average bin: %f\n", averageBin); //} // DEBUG track hist //printf("Track\n"); //for( int i = 0; i < m_histDims; i++ ) { // float *bin = cvGetHistValue_1D( m_hist, i ); // printf("%2d: %3.1f ", i, *bin); // if( 0 == (i+1) % 8 ) { // printf("\n"); // } //} //printf("\n"); // Now compute histogram image cvZero(m_histImg); for (int i = 0; i < m_histDims; ++i) { const double raw = cvGetReal1D(m_hist->bins, i) * (m_histImg->height / HIST_SCALE); const int val = cvRound(raw); CvScalar color = hsv2rgb((i * m_histRanges[1]) / m_histDims); cvRectangle(m_histImg, cvPoint(i * m_binWidth, m_histImg->height), cvPoint((i + 1) * m_binWidth, m_histImg->height - val), color, -1, 8, 0); } // Show histogram if (m_showHistogram) { ShowHistogram(); } }