2012-04-14 16 views
2

Ich versuche, einen Guassian-Filter ohne ConvolveOp zu erstellen. Ich habe eine Menge Probleme zu versuchen, dies zu arbeiten, ich habe einen Graustufenfilter zu arbeiten, aber für dieses habe ich Probleme, die Position eines Pixels 8 Nachbarn zu finden, so kann ich den Filter anwenden. hier ist was ich bisher habe. Ist das der richtige Weg, um jedes der Pixel zu erreichen?Gaußfilter ohne Verwendung von ConvolveOp

public class Gaussian implements Filter { 


    public void filter(PixelImage pi) { 
    Pixel[][] data = pi.getData(); 
    Pixel[][] original = data; 


    int kernel_rows = 3; 
    int kernel_cols = 3; 

    // define kernel here (double loop), these are the 1/16, 2/16, etc... 
    // values that you're multiplying the image pixels by 
    double[][] kernel = {{1,2,1}, 
     {2,4,2}, 
     {1,2,1}}; 

    // iterate over each pixel in the image 
    for (int row = 0; row < pi.getHeight(); row ++) { 
     for (int col = 0; col < pi.getWidth(); col++) { 
     // iterate over each pixel in the kernel 
     for (int row_offset = 0 ; row_offset < kernel_rows ; row_offset++) { 
      for (int col_offset = 0 ; col_offset < kernel_cols ; col_offset++) { 

      // subtract by half the kernel size to center the kernel 
      // on the pixel in question 
      // ** you'll have to modify to account for boundary conditions ** 
      int row_index = row + row_offset - kernel_rows/2; 
      int col_index = col + col_offset - kernel_cols/2; 

      int r =0; 
      int g =0; 
      int b =0; 




      r += (data[row_index][col_index].red * kernel[row_offset][col_offset])/16; 
      g += (data[row_index][col_index].green * kernel[row_offset][col_offset])/16; 
      b += (data[row_index][col_index].blue * kernel[row_offset][col_offset])/16; 
      Pixel temp =new Pixel(r, g, b); 
      original[row][col] = temp; 
      } 
     } 
     data = original; 
     pi.setData(data); 

     } 
    } 
    } 
} 

Antwort

4

eine Faltung ist im Wesentlichen eine Quadruple verschachtelte Schleife: zwei in einer Schleife durch die Pixel in dem Bild und bei jedem Bildpunkt, zwei in einer Schleife über die Pixel in dem Kernel.

So können Sie Ihren Code wesentlich mit so etwas wie dieses aufzuräumen:

int kernel_rows = 3; 
    int kernel_cols = 3; 

    // define kernel here (double loop), these are the 1/16, 2/16, etc... 
    // values that you're multiplying the image pixels by 
    double[][] kernel = ... 

    // iterate over each pixel in the image 
    // leave a kernel_rows/2 sized gap around the edge of the image 
    // so that we don't run into IndexOutOfBounds exceptions 
    // when performing the convolution 
    for (int row = kernel_rows/2; row < pi.getHeight() - kernel_rows/2; row ++) { 
    for (int col = kernel_cols/2; col < pi.getWidth() - kernel_cols/2; col++) { 

     int r = 0; 
     int g = 0; 
     int b = 0; 

     // iterate over each pixel in the kernel 
     for (int row_offset = 0 ; row_offset < kernel_rows ; row_offset++) { 
     for (int col_offset = 0 ; col_offset < kernel_cols ; col_offset++) { 

      // subtract by half the kernel size to center the kernel 
      // on the pixel in question 
      int row_index = row + row_offset - kernel_row/2; 
      int col_index = col + col_offset - kernel_cols/2 

      r += data[row_index][col_index].red * kernel[row_offset][col_offset]; 
      g += data[row_index][col_index].green * kernel[row_offset][col_offset]; 
      b += data[row_index][col_index].blue * kernel[row_offset][col_offset]; 

     } 
     } 

    data[row][col] = new Pixel(r, g, b); 

    } 
    } 
+0

Ok, so dass ich den Code oben bearbeitet. Ich bekomme den Fehler, dass r, g, b nicht initialisiert werden. Ich habe versucht, sie vor die Schleifen zu setzen, und gab ihr anfangs sogar einen Nullwert. Aber ich werde immer noch nicht initialisiert. –

+0

Sie müssen alle separat initialisieren (bearbeiteter Code oben). – ulmangt

+0

Ok, also ich denke, ich habe das, aber ich bekomme immer noch einen Index außerhalb der Grenzen Ausnahme. Ist mein Kernel-Array richtig eingerichtet? Ich weiß, dass der Rest meines Codes gut ist, weil ich die anderen Filter zum Funktionieren bringen kann. Vielen Dank, dass Sie mir dabei geholfen haben! Ich habe meinen Code oben bearbeitet. –

Verwandte Themen