2017-05-23 50 views
-1

嗨我編寫霍夫變換的代碼和累加器空間將在程序中查看,但我不知道我的代碼有什麼問題。請幫助霍夫變換學校代碼

public Image<Rgb, Byte> houghTransform(Image<Gray, Byte> input) 
    { 
     int width = input.Width, height = input.Height; 

     //1. create accumulator space 
     double MaxRho = Math.Sqrt(((width/2) * (width/2)) + ((height/2) * (height/2))); 
     double MaxTheta = Math.PI * 2; 

     int[,] accumulator = new int[width * 2,height*2]; 

     for (int i=0;i< width;i++) 
      for (int j = 0; j < height; j++) 
      { 
       accumulator[i, j] = 0; 
      } 

     //2. Loop for point in image 
     for (int i = 0; i < width; i++) 
      for (int j = 0; j < height; j++) 
       if (input.Data[j, i, 0] == 0) //0 for black, 255 for white 
       { 
        for (int k = 0; k < height; k++) 
        { 
         double rho = ((double)(i - (width/2)) * Math.Cos((MaxTheta/height) * (double)k)) + ((double)(j - (height/2)) * Math.Sin((MaxTheta/height) * (double)k)); 
         int newRho = (int)((rho/MaxRho) * width); 
         if(newRho >= 0 && newRho < width) accumulator[newRho, k]++; 
        } 
       } 

     int maxima = 0; 
     for (int i = 0; i < width; i++) 
      for (int j = 0; j < height * 2; j++) 
      { 
       if (accumulator[i, j] > maxima) 
       { maxima = accumulator[i, j]; } 
      } 

     //3. Tresholding the acc 
     for (int i = 0; i < width; i++) 
      for (int j = 0; j < height ; j++) 
      { 
       int temp = 0; 
       if(maxima!=0) temp = (accumulator[i, j]/maxima) * 255; 
       accumulator[i, j] = (byte)temp; 
      } 

     //4. Save to Image<Gray, Byte> 
     Image<Rgb, Byte> output = new Image<Rgb, Byte>((int)width, height*2); 
     for (int i = 0; i < width; i++) 
      for (int j = 0; j < height; j++) 
      { 
       output.Data[j, i, 0] = (byte)accumulator[i, j]; 
       output.Data[j, i, 1] = (byte)accumulator[i, j]; 
       output.Data[j, i, 2] = (byte)accumulator[i, j]; 
      } 

     return output; 

    } 

測試結果:我的累加器只有4點

img

我所期待:

what the result must be

+0

有沒有錯誤? –

+0

沒有錯誤 –

+0

[codereview.se]是查看代碼的最佳位置。如果您真的有調試問題(非工作代碼),請詳細解釋它。 –

回答

0

我做到了,所以這個問題是閾值,必須先轉換爲雙精度才能計算,然後再轉換爲整數。