-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradientCalculator.java
More file actions
119 lines (97 loc) · 3.45 KB
/
Copy pathGradientCalculator.java
File metadata and controls
119 lines (97 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import ij.ImagePlus;
import ij.gui.NewImage;
import ij.process.Blitter;
import ij.process.FloatProcessor;
import ij.process.ImageProcessor;
/**
* This class provides methods to apply gradient magnitude operator to an input {@code ImageProcessor} and to get results of this operation
* @author Alfonso Ridolfo
* @version 0.2
*/
public class GradientCalculator {
private ImageProcessor IP;
private ImagePlus gradImage;
private int w, h;
private ImageProcessor gradProcessor;
private float[] gradPix;
/**
* initialize calculator on target ImageProcessor
* @param processor target ImageProcessor
*/
public GradientCalculator(ImageProcessor processor) {
IP = processor;
w = IP.getWidth();
h = IP.getHeight();
}
/**
* computes gradient magnitude of the image
*/
public void calc() {
// creating 2 new ImagePlus to copy the input ImagePlus
ImagePlus X_sobel = NewImage.createFloatImage("X-sobel", w, h, 1,
NewImage.FILL_BLACK); // ImagePlus storing orizontal edges convolution result
ImagePlus Y_sobel = NewImage.createFloatImage("Y-sobel", w, h, 1,
NewImage.FILL_BLACK); // ImagePlus storing vertical edges convolution result
ImageProcessor fIP;
fIP = (FloatProcessor) IP.convertToFloat();
// loading and converting the ImageProcessors
ImageProcessor ip_X_sobel =
(FloatProcessor) X_sobel.getProcessor().convertToFloat();
ImageProcessor ip_Y_sobel =
(FloatProcessor) Y_sobel.getProcessor().convertToFloat();
// copying the ImageProcessor
ip_X_sobel.copyBits(fIP, 0, 0, Blitter.COPY);
// copying the ImageProcessor
ip_Y_sobel.copyBits(fIP, 0, 0, Blitter.COPY);
// creating X-Sobel kernel for the convolution
int[] ker_X_sobel = {
-1, -2, -1, 0, 0, 0, 1, 2, 1
};
// creating Y-Sobel kernel for the convolution
int[] ker_Y_sobel = {
-1, 0, 1, -2, 0, 2, -1, 0, 1
};
// calculating horizontal edges of the input image
ip_X_sobel.convolve3x3(ker_X_sobel);
ip_X_sobel.resetMinAndMax();
// calculating vertical edges of the input image
ip_Y_sobel.convolve3x3(ker_Y_sobel);
ip_Y_sobel.resetMinAndMax();
// initializing gradient ImagePlus
gradImage = NewImage.createFloatImage("Gradient", w, h, 1,
NewImage.FILL_BLACK); // ImagePlus storing gradient result
// initializing gradient ImageProcessor
gradProcessor = (FloatProcessor)gradImage.getProcessor().convertToFloat();
// getting pixels values
gradPix = (float[])gradProcessor.getPixels();
float[] XSobelPix = (float[])ip_X_sobel.getPixels();
float[] YSobelPix = (float[])ip_Y_sobel.getPixels();
int l, offset;
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
offset = y*w;
l = offset+x;
gradPix[l] = (float) Math.sqrt((XSobelPix[l]*XSobelPix[l])+(YSobelPix[l]*YSobelPix[l]));
}
}
}
/**
* @return a float map of the gradient magnitude
*/
public float[] getGradFloat() {
return gradPix;
}
/**
* shows gradient magnitude
*/
public void showGradImage() {
gradImage.updateAndDraw();
gradImage.show();
}
/**
* @return gradient magnitude as an ImagePlus
*/
public ImagePlus getImage() {
return gradImage;
}
}