Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6147957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:11:40+00:00 2026-05-23T19:11:40+00:00

I am trying to track objects inside an image using histogram data from the

  • 0

I am trying to track objects inside an image using histogram data from the object. I pass in a reference image to get the histogram data and store it in a Mat. From there I load in an image and try and use the histogram data to detect the object. The problem I am coming with is not only is it not tracking the object, but it’s not updating the detection. If I load image “1.jpg” the detection will claim that the object is in the top right corner when it’s in the bottom left. When I pass in the second image the detection field does not move at all. This continues for the next batch of images as well. Below is a code snippet of my application.

This is being done in a Windows 7 32-bit environment using OpenCV2.3 in VS2010. Thanks in advance for any help

int main( int argc, char** argv )
{
    vector<string> szFileNames;
    IplImage* Image;
    Mat img, hist, backproj;
    Rect trackWindow;

    // Load histogram data
    hist = ImageHistogram("C:/Users/seb/Documents/redbox1.jpg", backproj);

    Image = cvLoadImage("C:/Users/seb/Documents/1.jpg");
        img = Mat(Image);
    trackWindow = Rect(0, 0, Image->width, Image->height);

    imshow("Histogram", hist);

    while(true)
    {
        Detection(img, backproj, trackWindow);
        imshow("Image", img);

        char c = cvWaitKey(1);

        switch(c)
        {
            case 32:
            {
                cvReleaseImage(&Image);
                Image = cvLoadImage("C:/Users/seb/Documents/redbox2.jpg");
                img = Mat(Image);
                break;
            }
        }
    }

    cvReleaseImage(&Image);

    // Destroy all windows
    cvDestroyWindow("Histogram");
    cvDestroyWindow("Image");

    return 0;
}

Mat ImageHistogram(string szFilename, Mat& backproj)
{
    // Create histogram values
    int vmin = 10;
    int vmax = 256;
    int smin = 30;
    int hsize = 16;
    float hranges[] = {0,180};
    const float* phranges = hranges;

    // Load the image
    IplImage* Image = cvLoadImage(szFilename.c_str());
    Rect rect = Rect(0, 0, Image->width, Image->height);

    // Convert Image to a matrix
    Mat ImageMat = Mat(Image);

    // Create and initialize the Histogram
    Mat hsv, mask, hue, hist, histimg = Mat::zeros(200, 320, CV_8UC3);
    cvtColor(ImageMat, hsv, CV_BGR2HSV);

    // Create and adjust the histogram values
    inRange(hsv, Scalar(0, smin, vmin), Scalar(180, 256, vmax), mask);
    int ch[] = {0, 0};

    hue.create(hsv.size(), hsv.depth());
    mixChannels(&hsv, 1, &hue, 1, ch, 1);

    Mat roi(hue, rect), maskroi(mask, rect);
    calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);
    normalize(hist, hist, 0, 255, CV_MINMAX);

    histimg = Scalar::all(0);
    int binW = histimg.cols / hsize;
    Mat buf(1, hsize, CV_8UC3);
    for( int i = 0; i < hsize; i++ )
        buf.at<Vec3b>(i) = Vec3b(saturate_cast<uchar>(i*180./hsize), 255, 255);
    cvtColor(buf, buf, CV_HSV2BGR);

    for( int i = 0; i < hsize; i++ )
    {
        int val = saturate_cast<int>(hist.at<float>(i)*histimg.rows/255);
        rectangle( histimg, Point(i*binW,histimg.rows),
            Point((i+1)*binW,histimg.rows - val),
            Scalar(buf.at<Vec3b>(i)), -1, 8 );
    }

    calcBackProject(&hue, 1, 0, hist, backproj, &phranges);
    backproj &= mask;

    cvReleaseImage(&Image);

    return histimg;
}

void Detection(Mat& image, Mat& backproj, Rect& trackWindow)
{
    RotatedRect trackBox = CamShift(backproj, trackWindow, TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ));
    int test2 = trackWindow.area();
    if(trackBox.size.height > 0 && trackBox.size.width > 0)
    {
        if( trackWindow.area() <= 1 )
        {
            int cols = backproj.cols, rows = backproj.rows, r = (MIN(cols, rows) + 5)/6;
            trackWindow = Rect(trackWindow.x - r, trackWindow.y - r,
                trackWindow.x + r, trackWindow.y + r) &
                Rect(0, 0, cols, rows);
        }

        int test = trackBox.size.area();
        if(test >= 1)
        {
            rectangle(image, trackBox.boundingRect(), Scalar(255,0,0), 3, CV_AA);
            ellipse( image, trackBox, Scalar(0,0,255), 3, CV_AA );
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:11:41+00:00Added an answer on May 23, 2026 at 7:11 pm

    I’ve figured out the issue. It had to deal with me not converting the image that I’m checking upon. I had to get histogram data from my colored box and then I had to get the histogram from the image I was using to search.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to replace an Object inside my nested array (colArray) by using
Using Xcode/Cocoa and the ExtAudioFile API, I'm trying to store away AudioBufferList* objects away
I am trying to track what users are searching for on my site (from
I'm trying to track down a memory leak in a java process, using jmap
I'm trying to store all the users inside a sharedObject thats created on the
I'm trying to track phone sales vs. web sales using our existing web forms
I am trying to store custom routes for the Zend Framework inside the database.
i'm trying to use compiled assets and code from a swc. Inside a new
I'm trying to drag data from the Winforms portion of my application on a
I am trying to track down why some objects in my application are pinned.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.