Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ij.fach.FilterValue;
- import java.awt.Color;
- import java.awt.image.BufferedImage;
- import java.util.ArrayList;
- public class SobelOperator extends ImageProcessing {
- public static String IDENTIFIER = "Sobel-Operator";
- private String ACTIVITY = "Activity";
- private boolean isActiveThreshold = false;
- private String THRESHOLD = "Threshold";
- private int threshold = 40;
- public SobelOperator(BufferedImage input, ArrayList<FilterValue> para) {
- super(input, para);
- Object o = getParameter(THRESHOLD);
- if (o != null)
- threshold = new Integer(o.toString());
- o = getParameter(ACTIVITY);
- if (o != null)
- isActiveThreshold = new Boolean(o.toString());
- }
- public SobelOperator(int width, int height, int type, int maxValue,
- int[] input) {
- super(width, height, type, maxValue, input);
- }
- @Override
- public void processing() {
- int sum, sumX, sumY;
- int[] array = pixelOrg;
- int[] newArray = pixelNew;
- for (int i = 0; i < height; i++) { // Zeilenschleife
- for (int j = 0; j < width; j++) { // Spaltenschleife
- //pixelNew[y * width + x] = pixelOrg[y * width + x];
- if (i > 0 && j > 0 && i < height - 1 && j < width - 1) {
- // 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))
- sumX = array[(i + 1) * width + j - 1] + 2 * array[(i + 1)* width + j] + array[(i + 1)*width + j + 1] -
- (array[(i - 1)*width + j - 1] + 2 * array[(i-1)*width + j] + array[(i - 1)*width + j + 1]);
- // 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))
- sumY = array[(i - 1)*width + j + 1] + 2 * array[i*width + j + 1] + array[(i + 1)*width + j + 1] -
- (array[(i - 1)*width + j - 1] + 2 * array[i*width + j-1] + array[(i + 1)*width + j - 1]);
- sum = (int) Math.sqrt(sumX * sumX + sumY * sumY);
- newArray[i*width + j] = sum > 0xff ? 0xff : sum;
- } else if (i < height - 1 && j < width - 1) {
- //Gx=f(x+1,y+1)-f(x,y),
- //Gy=f(x,y+1)-f(x+1,y)
- sumX = array[(i+1) * width + j + 1] - array[i*width + j];
- sumY = array[i*width + j + 1] - array[(i+1)*width + j];
- sum = (int) Math.sqrt(sumX * sumX + sumY * sumY);
- newArray[i*width + j] = sum > 0xff ? 0xff : sum;
- } else if (j == width - 1) {
- //last row
- newArray[i*width + j] = newArray[i*width + j - 1];
- } else if (i == height - 1) {
- //the last line
- newArray[i*width + j] = newArray[(i - 1)*width + j];
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment