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 9006207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:20:57+00:00 2026-06-16T01:20:57+00:00

#include <cv.h> #include <highgui.h> #include <iostream> #include <cmath> #include <cstdlib> #include <fstream> using namespace

  • 0
#include <cv.h>
#include <highgui.h>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <fstream>

using namespace std;

typedef struct histBundle {
double rCh[256];
double gCh[256];
double bCh[256];
}bundleForHist;

bundleForHist getHistFromImage (IplImage* img, int numBins) {
float range[] = {
    0, 
    numBins
};
float *ranges[] = { 
    range 
};

bundleForHist bfh;

CvHistogram *hist = cvCreateHist (1, &numBins, CV_HIST_ARRAY, ranges, 1);
cvClearHist (hist);
IplImage* imgRed   = cvCreateImage(cvGetSize(img), 8, 1);
IplImage* imgGreen = cvCreateImage(cvGetSize(img), 8, 1);
IplImage* imgBlue  = cvCreateImage(cvGetSize(img), 8, 1);
cvSplit(img, imgBlue, imgGreen, imgRed, NULL);
cvCalcHist(&imgRed, hist, 0, 0);
for (int i = 0; i < numBins; ++i) {
    bfh.rCh[i] = cvQueryHistValue_1D(hist, i);
}
cvClearHist(hist);
cvCalcHist(&imgGreen, hist, 0, 0);
for (int i = 0; i < numBins; ++i) {
    bfh.gCh[i] = cvQueryHistValue_1D(hist, i);
}
cvClearHist(hist);
cvCalcHist(&imgBlue, hist, 0, 0);
for (int i = 0; i < numBins; ++i) {
    bfh.bCh[i] = cvQueryHistValue_1D(hist, i);
}
cvClearHist(hist);
return bfh;
}

int main (int argc, char** argv) {
int c;
IplImage* img = NULL;
int frame_number = 0;
CvCapture* capture = cvCaptureFromAVI ("Cricketc1.avi");
assert(capture);
int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
cvNamedWindow ("Video", 0);

while (1) {
    //IplImage * img = cvLoadImage("C:\\Users\\ANIMES~1\\Desktop\\bw.jpg");
    img = cvQueryFrame(capture);
    frame_number++;
    if (img) {
        cvShowImage("Video", img);
        int numBins = 256;
        bundleForHist bfh;
        bfh = getHistFromImage (img, numBins);
        double totalForR = 0;
        double totalForG = 0;
        double totalForB = 0;
        double probR[256];
        double probG[256];
        double probB[256];
        for (int i = 0; i < numBins-1; ++i) {
            totalForR += bfh.rCh[i];
            totalForG += bfh.gCh[i];
            totalForB += bfh.bCh[i];
        }
        double lengthHistogram = totalForR + totalForG + totalForB;
        for (int i = 0; i < 256; ++i) {
            probR[i] = bfh.rCh[i]/(double)lengthHistogram;
            probG[i] = bfh.gCh[i]/(double)lengthHistogram;
            probB[i] = bfh.bCh[i]/(double)lengthHistogram;
            //file << bfh.rCh[i] << "\t" << bfh.gCh[i] << "\t" << bfh.bCh[i] << "\t" << probR[i] << "\t" << probG[i] << "\t" << probB[i] << "\n";
        }

        double entropyR = 0.0;
        double entropyG = 0.0;
        double entropyB = 0.0;
        for (int i = 0; i < numBins; ++i) {
            entropyR += probR[i]*log(probR[i]);
            entropyG += probG[i]*log(probG[i]);
            entropyB += probB[i]*log(probB[i]);
        }
        cout << frame_number << "\t" << (-1.0)*(entropyR + entropyG + entropyB) << endl;
    }
    c = cvWaitKey(1000/fps);
    if (c == 27)
        break;
}
//cvReleaseImage(&img);
cvReleaseCapture(&capture);
cvDestroyWindow("Video");
return 0;
}

OUTPUT:

.
.
254     -1.#IND
255     -1.#IND
256     -1.#IND
257     -1.#IND
258      5.5686
.
.

I first found the image entropy, which came out to be correct.
but nearly 80 % of the frame entropies for the video are coming out to be -1.#IND.

This is the video ….download

What could be going wrong?

  • 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-06-16T01:20:59+00:00Added an answer on June 16, 2026 at 1:20 am

    It might be the case that prob[i] = 0 for some i, therefore you are calculating log(0) which is undefined. To fix that you simply discard such “probabilities”:

    for (int i = 0; i < numBins; ++i) {
        if (prob[i])
            entropy += prob[i]*log(prob[i]);
    }
    

    For the other error you found regarding the 0 value in the bin 255, that is due to the range you specified. OpenCV considers the range for the relevant function as [start, end), so specifying a range of [0, 255) is going to ignore the end value of 255. What you want is to keep both 0 and 255, therefore:

    float range[] = {0, numBins};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; int main() { int a, b, c, max; cout<<a=;
#include <iostream> #include <cassert> #include <vector> #include <ctime> #include <cstdlib> #include <Windows.h> using namespace
#include <iostream> #include <string.h> using namespace std; int main() { int e=0; int b=0;
#include <iostream> #include <string> using namespace std; string crash() { } int noCrash() {
I have this code in OpenCV C++: #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> using namespace cv;
#include <iostream> #include <tuple> int main(){ auto bt=std::make_tuple(std::tuple<>(),std::tuple<std::tuple<>>()); //Line 1 auto bt2=std::make_tuple(std::tuple<>(),std::tuple<>()); //Line 2
#include <list> using std::list; class foo { ... class bar : public foo {
#include opencv2/core/core.hpp #include opencv2/highgui/highgui.hpp #include opencv2/imgproc/imgproc.hpp #include <QtGui> //make QImage point to the contents
#include cv.h #include highgui.h #include <stdio.h> int main(int argc, char* argv[]){ cvNamedWindow(Window1, CV_WINDOW_AUTOSIZE); IplImage*
I am using VS2010 and opencv2.1. Under Win7, the sample programme is below: #include

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.