【问题标题】:Weird output while finding entropy of frames of a video in opencv在opencv中查找视频帧熵时的奇怪输出
【发布时间】:2012-12-17 19:30:36
【问题描述】:
#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

这是视频......download

可能出了什么问题?

【问题讨论】:

  • 查看代码 2 秒表明您正在执行entropy = blah。这不是熵的计算方式,您需要求和,所以您想要entropy += blah
  • 哦!对不起......我做到了,但我仍然得到-1.#QNAN的输出.....
  • 更多 2 秒查看它,我看到您在 calcHistProb 返回一个局部变量,并稍后在程序中将其视为指针。我真的怀疑你编译这个没有收到警告,你忽略了警告。在不忽略警告的情况下修复所有这些语言问题,并编辑您的代码。
  • @mmgp 我已经编辑了代码并且所有的警告都已经解决了。但是那个错误还是来了....!
  • 指这个link ...他们说有非法操作...但是我找不到... !!!!

标签: image-processing opencv entropy opencvdotnet


【解决方案1】:

prob[i] = 0 可能是某些i 的情况,因此您正在计算未定义的log(0)。要解决这个问题,您只需丢弃此类“概率”:

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

对于您发现的有关 bin 255 中的 0 值的其他错误,这是由于您指定的范围造成的。 OpenCV 将相关函数的范围视为 [start, end),因此指定范围为 [0, 255) 将忽略 255 的结束值。您想要保留 0 和 255,因此:

float range[] = {0, numBins};

【讨论】:

  • 我已经解决了图像的 0 值问题,我想问的是,视频怎么会有错误....????
  • 处理视频的代码未正确修复。
  • 看,你还在用你当前的代码做probR[i]*log(probR[i]);等等。在申请 log 之前,您没有检查 probR[i] == 0
  • 哦!我已经这样做了....忘记将其发布在cmets上...现在一切都很好...!谢谢...
  • 我应该在while循环中加入什么标准,以便在视频播放后自动跳出循环?我不希望任何 cvWaitKey() 来完成这项工作!
猜你喜欢
  • 1970-01-01
  • 2013-03-15
  • 2020-03-17
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
  • 2017-11-13
  • 2016-04-13
  • 2017-03-19
相关资源
最近更新 更多