5
#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;
}

出力:

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

私は最初に画像のエントロピーを見つけましたが、それは正しいことがわかりました。しかし、ビデオのフレームエントロピーの80%近くが出てきています-1.#IND

これはビデオです....ダウンロード

何がうまくいかない可能性がありますか?

4

1 に答える 1

3

prob[i] = 0一部のについては、未定義iの計算を行っている可能性があります。log(0)そのような「確率」を単に破棄することを修正するには:

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

ビン 255 の 0 値に関して見つかった他のエラーについては、指定した範囲が原因です。OpenCV は、関連する関数の範囲を [start, end) と見なすため、[0, 255) の範囲を指定すると、255 の終了値が無視されます。必要なのは、0 と 255 の両方を保持することです。したがって、次のようになります。

float range[] = {0, numBins};
于 2012-12-17T20:50:42.410 に答える