KirilGeorgiev

Class SobelOperator extends ImageProcessing

May 7th, 2021 (edited)
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. import ij.fach.FilterValue;
  2.  
  3. import java.awt.Color;
  4. import java.awt.image.BufferedImage;
  5. import java.util.ArrayList;
  6.  
  7. public class SobelOperator extends ImageProcessing {
  8.  
  9.     public static String IDENTIFIER = "Sobel-Operator";
  10.  
  11.     private String ACTIVITY = "Activity";
  12.     private boolean isActiveThreshold = false;
  13.     private String THRESHOLD = "Threshold";
  14.     private int threshold = 40;
  15.  
  16.     public SobelOperator(BufferedImage input, ArrayList<FilterValue> para) {
  17.         super(input, para);
  18.  
  19.         Object o = getParameter(THRESHOLD);
  20.         if (o != null)
  21.             threshold = new Integer(o.toString());
  22.  
  23.         o = getParameter(ACTIVITY);
  24.         if (o != null)
  25.             isActiveThreshold = new Boolean(o.toString());
  26.     }
  27.  
  28.     public SobelOperator(int width, int height, int type, int maxValue,
  29.             int[] input) {
  30.         super(width, height, type, maxValue, input);
  31.     }
  32.  
  33.     @Override
  34.     public void processing() {
  35.         int sum, sumX, sumY;
  36.  
  37.         int[] array = pixelOrg;
  38.         int[] newArray = pixelNew;
  39.  
  40.         for (int i = 0; i < height; i++) { // Zeilenschleife
  41.             for (int j = 0; j < width; j++) { // Spaltenschleife
  42.                 //pixelNew[y * width + x] = pixelOrg[y * width + x];
  43.  
  44.  
  45.                 if (i > 0 && j > 0 && i < height - 1 && j < width - 1) {
  46.                    // Gx=f(x+1,y-1)+2f(x+1,y)+f(x+1,y+1)-(f(x-1,y-1)+2f(x-1,y)+f(x-1,y+1))
  47.                
  48.                     sumX = array[(i + 1) * width + j - 1] + 2 * array[(i + 1)* width + j] + array[(i + 1)*width + j + 1] -
  49.                             (array[(i - 1)*width + j - 1] + 2 * array[(i-1)*width + j] + array[(i - 1)*width + j + 1]);
  50.  
  51.                     // Gy=f(x-1,y+1)+2f(x,y+1)+f(x+1,y+1)-(f(x-1,y-1)+2f(x,y-1)+f(x+1,y-1))
  52.  
  53.                     sumY = array[(i - 1)*width + j + 1] + 2 * array[i*width + j + 1] + array[(i + 1)*width + j + 1] -
  54.                             (array[(i - 1)*width + j - 1] + 2 * array[i*width + j-1] + array[(i + 1)*width + j - 1]);
  55.  
  56.                     sum = (int) Math.sqrt(sumX * sumX + sumY * sumY);
  57.  
  58.                     newArray[i*width + j] = sum > 0xff ? 0xff : sum;
  59.                    
  60.                 } else if (i < height - 1 && j < width - 1) {
  61.                     //Gx=f(x+1,y+1)-f(x,y),
  62.                     //Gy=f(x,y+1)-f(x+1,y)
  63.                     sumX = array[(i+1) * width + j + 1] - array[i*width + j];
  64.                     sumY = array[i*width + j + 1] - array[(i+1)*width + j];
  65.                     sum = (int) Math.sqrt(sumX * sumX + sumY * sumY);
  66.                     newArray[i*width + j] = sum > 0xff ? 0xff : sum;
  67.                 } else if (j == width - 1) {
  68.                                          //last row
  69.                     newArray[i*width + j] = newArray[i*width + j - 1];
  70.                 } else if (i == height - 1) {
  71.                                          //the last line
  72.                     newArray[i*width + j] = newArray[(i - 1)*width + j];
  73.                 }
  74.  
  75.             }
  76.         }
  77.     }
  78. }
Add Comment
Please, Sign In to add comment