2017-05-23 30 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

を、私はそれをしなかったので、問題がです閾値化は、計算のために最初にdoubleに変換してから、再び整数に変換する必要があります。

関連する問題