0
我在生成直方圖時遇到了問題。我有一組數據,我想要計算並存儲在一個文件中,以便在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);
}