生成直方图

时间:2013-07-28 20:02:37

标签: c++ histogram

我在生成直方图时遇到问题。我有一组数据,我想要计算并存储在一个文件中,以便在excel图表中,就像图像直方图一样。

这是我的代码,但我总共得到了正确数量的数据,但频率不是频率。我已经将直方图的宽度设置为0.01,这只是稍后我必须改进它的测试。

void TPictureWindow::Histogramm()
{
  unsigned long retval;
  char buffer[100];

  int valueHist[260];
  int finalVal;
  double value = 0.0;
  double histWidth = 0.01;
  int counter = 0;
  scaling = 0.005078125;

  HANDLE hDataFile = CreateFile("Histogramm.txt", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

  for(int i = 0; i < NumImages; i++)
  {

    for(int j = 0; j < edge[i][0]->total; j++)
    {
        for(int k = 0;k < sizeof(valueHist);k++){

           CvPoint* pt1 = CV_GET_SEQ_ELEM(CvPoint, edge[i][0], j);
           CvPoint* pt2 = CV_GET_SEQ_ELEM(CvPoint, edge[i][1], j);

           value = (pt1->y-pt2->y)*scaling;

           if(value < 0) value = 0.0;

           finalVal = value/histWidth;
        //sprintf(buffer, "final: %d\nvalue: %f\n", finalVal, value);
        //MessageBox(buffer, "", MB_OK);



           if(finalVal == k){

              counter++;
              valueHist[k] = counter;
              sprintf(buffer, "%9.3d\n", valueHist[k]);
              WriteFile(hDataFile, buffer, strlen(buffer), &retval, NULL);
           }
        }
     }


 }
 CloseHandle(hDataFile);
 PostMessage(READY_WITH, IMG_3D_PROFILE, 0);
}

1 个答案:

答案 0 :(得分:0)

使用该值计算其bin而不是搜索bin应该更容易和更快。

for(int i = 0; i < NumImages; i++) {
   for(int j = 0; j < edge[i][0]->total; j++) {
       CvPoint* pt1 = CV_GET_SEQ_ELEM(CvPoint, edge[i][0], j);
       CvPoint* pt2 = CV_GET_SEQ_ELEM(CvPoint, edge[i][1], j);
       int bin

       value = (pt1->y-pt2->y)*scaling;

       if(value < 0) value = 0.0;

       int bin = floor(value/binSize);

       if ( bin < maxBin ) {
          hist[bin] = hist[bin] + 1;
       } else {
        /* Report a lost point if interested */
      }
    }
 }

如果maxBin * binSize&lt; MAX(值)。