root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / imageAnalysis / erosionDilation / ErosionDilationAlgorithm.java @ 59
History | View | Annotate | Download (5 KB)
1 |
package es.unex.sextante.imageAnalysis.erosionDilation; |
---|---|
2 |
|
3 |
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue; |
4 |
import es.unex.sextante.core.GeoAlgorithm; |
5 |
import es.unex.sextante.core.Sextante; |
6 |
import es.unex.sextante.dataObjects.IRasterLayer; |
7 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
8 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
9 |
|
10 |
public class ErosionDilationAlgorithm |
11 |
extends
|
12 |
GeoAlgorithm { |
13 |
|
14 |
public static final int ERODE = 0; |
15 |
public static final int DILATE = 1; |
16 |
|
17 |
public static final String LAYER = "LAYER"; |
18 |
public static final String OPERATION = "OPERATION"; |
19 |
public static final String RADIUS = "RADIUS"; |
20 |
public static final String RESULT = "RESULT"; |
21 |
|
22 |
protected final byte NO_DATA = 0; |
23 |
|
24 |
private int m_iRadius; |
25 |
private boolean m_bIsValidCell[][]; |
26 |
private boolean m_bIsForegroundCell[][]; |
27 |
private IRasterLayer m_Image;
|
28 |
private int m_iOperationType; |
29 |
|
30 |
|
31 |
@Override
|
32 |
public void defineCharacteristics() { |
33 |
|
34 |
final String[] sMethod = { Sextante.getText("Erosion"), Sextante.getText("Dilation") }; |
35 |
|
36 |
setUserCanDefineAnalysisExtent(false);
|
37 |
setName(Sextante.getText("Erosion-Dilation"));
|
38 |
setGroup(Sextante.getText("Image_processing"));
|
39 |
|
40 |
try {
|
41 |
m_Parameters.addInputRasterLayer(LAYER, Sextante.getText("Image"), true); |
42 |
m_Parameters.addSelection(OPERATION, Sextante.getText("Operation"), sMethod);
|
43 |
m_Parameters.addNumericalValue(RADIUS, Sextante.getText("Radius"), AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER,
|
44 |
1, 1, 20); |
45 |
addOutputRasterLayer(RESULT, this.getName());
|
46 |
} |
47 |
catch (final RepeatedParameterNameException e) { |
48 |
Sextante.addErrorToLog(e); |
49 |
} |
50 |
|
51 |
} |
52 |
|
53 |
|
54 |
@Override
|
55 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
56 |
|
57 |
int x, y;
|
58 |
int iNX, iNY;
|
59 |
int iValidCells = 0; |
60 |
|
61 |
m_iRadius = m_Parameters.getParameterValueAsInt(RADIUS); |
62 |
m_iOperationType = m_Parameters.getParameterValueAsInt(OPERATION); |
63 |
m_Image = m_Parameters.getParameterValueAsRasterLayer(LAYER); |
64 |
|
65 |
m_Image.setFullExtent(); |
66 |
|
67 |
final IRasterLayer result = getNewRasterLayer(RESULT, this.getName(), IRasterLayer.RASTER_DATA_TYPE_BYTE, |
68 |
m_Image.getWindowGridExtent()); |
69 |
|
70 |
result.setNoDataValue(NO_DATA); |
71 |
result.assignNoData(); |
72 |
|
73 |
iNX = m_Image.getNX(); |
74 |
iNY = m_Image.getNY(); |
75 |
|
76 |
m_bIsValidCell = new boolean[2 * m_iRadius + 1][2 * m_iRadius + 1]; |
77 |
m_bIsForegroundCell = new boolean[2 * m_iRadius + 1][2 * m_iRadius + 1]; |
78 |
|
79 |
for (y = -m_iRadius; y < m_iRadius + 1; y++) { |
80 |
for (x = -m_iRadius; x < m_iRadius + 1; x++) { |
81 |
final double dDist = Math.sqrt(x * x + y * y); |
82 |
if (dDist <= m_iRadius) {
|
83 |
m_bIsValidCell[x + m_iRadius][y + m_iRadius] = true;
|
84 |
iValidCells++; |
85 |
} |
86 |
else {
|
87 |
m_bIsValidCell[x + m_iRadius][y + m_iRadius] = false;
|
88 |
} |
89 |
} |
90 |
} |
91 |
|
92 |
for (y = 0; (y < iNY) && setProgress(y, iNY); y++) { |
93 |
for (x = 0; x < iNX; x++) { |
94 |
setNeighborhoodValues(x, y); |
95 |
result.setCellValue(x, y, operate()); |
96 |
} |
97 |
} |
98 |
|
99 |
return !m_Task.isCanceled();
|
100 |
|
101 |
} |
102 |
|
103 |
|
104 |
private byte operate() { |
105 |
|
106 |
switch (m_iOperationType) {
|
107 |
case ERODE:
|
108 |
default:
|
109 |
for (int i = 0; i < m_bIsValidCell.length; i++) { |
110 |
for (int j = 0; j < m_bIsValidCell[0].length; j++) { |
111 |
if ((m_bIsValidCell[i][j] == true) && (m_bIsForegroundCell[i][j] == false)) { |
112 |
return NO_DATA;
|
113 |
} |
114 |
} |
115 |
} |
116 |
return 1; |
117 |
case DILATE:
|
118 |
for (int i = 0; i < m_bIsValidCell.length; i++) { |
119 |
for (int j = 0; j < m_bIsValidCell[0].length; j++) { |
120 |
if ((m_bIsValidCell[i][j] == true) && (m_bIsForegroundCell[i][j] == true)) { |
121 |
return 1; |
122 |
} |
123 |
} |
124 |
} |
125 |
return NO_DATA;
|
126 |
} |
127 |
} |
128 |
|
129 |
|
130 |
private void setNeighborhoodValues(final int iX, |
131 |
final int iY) { |
132 |
|
133 |
int x, y;
|
134 |
int iCell = 0; |
135 |
double dValue;
|
136 |
|
137 |
for (y = -m_iRadius; y < m_iRadius + 1; y++) { |
138 |
for (x = -m_iRadius; x < m_iRadius + 1; x++) { |
139 |
if (m_bIsValidCell[x + m_iRadius][y + m_iRadius]) {
|
140 |
dValue = m_Image.getCellValueAsDouble(iX + x, iY + y); |
141 |
if (!m_Image.isNoDataValue(dValue) && (dValue != 0)) { |
142 |
m_bIsForegroundCell[x + m_iRadius][y + m_iRadius] = true;
|
143 |
} |
144 |
else {
|
145 |
m_bIsForegroundCell[x + m_iRadius][y + m_iRadius] = false;
|
146 |
} |
147 |
iCell++; |
148 |
} |
149 |
} |
150 |
} |
151 |
|
152 |
} |
153 |
|
154 |
|
155 |
} |