【问题标题】:Generating a histogram生成直方图
【发布时间】:2013-07-28 20:02:37
【问题描述】:

我在生成直方图时遇到问题。我有一组数据,我想对其进行计数并存储在一个文件中,以便在 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);
}

【问题讨论】:

    标签: c++ histogram


    【解决方案1】:

    使用该值来计算它的 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

    【讨论】:

      猜你喜欢
      • 2010-12-24
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      • 2012-08-14
      • 2014-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多