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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
import java.math.BigInteger;
import org.itk.simple.*;
/* This class is needed because on some systems uint64_t gets converted to long
* while on others it becomes a BigInteger. */
class BigIntegerFix
{
public static long Convert( long value )
{
return value;
}
public static long Convert( BigInteger value )
{
return value.longValue();
}
}
class sitkImageTests
{
public static void main(String argv[])
{
int ntests = 2;
int npass = 0;
int nfail = 0;
System.out.println("[==========] Running Java.ImageTests");
System.out.println("[----------]");
System.out.println("[ RUN ] Java.BasicImageTest");
if (BasicImageTest())
{
System.out.println("[ OK ] Java.BasicImageTest");
npass++;
}
else
{
System.out.println("[ FAIL ] Java.BasicImageTest");
nfail++;
}
System.out.println("[----------]");
System.out.println("[----------]");
System.out.println("[ RUN ] Java.LabelShapeStatisticsTest");
if (LabelShapeStatisticsTest())
{
System.out.println("[ OK ] Java.LabelShapeStatisticsTest");
npass++;
}
else
{
System.out.println("[ FAIL ] Java.LabelShapeStatisticsTest");
nfail++;
}
System.out.println("[----------]");
System.out.println("[==========]");
if (npass == ntests)
{
System.out.println("[ PASSED ]");
}
else
{
System.out.println("[ FAILED ]");
System.exit(1);
}
}
public static boolean BasicImageTest()
{
int[] v = {1,1};
VectorUInt32 idx = new VectorUInt32( v.length );
for (int i = 0; i < v.length; i++)
{
idx.set( i, v[i] );
}
int size = 10;
short val = 42;
Image image = new Image(size, size, PixelIDValueEnum.sitkUInt8);
image.setPixelAsUInt8(idx, val);
try
{
if (size != image.getWidth())
{
throw new Exception("Bad width");
}
}
catch (Exception e)
{
return false;
}
short newval;
newval = image.getPixelAsUInt8(idx);
try
{
if (newval != val)
{
throw new Exception("Bad pixel value");
}
}
catch (Exception e)
{
return false;
}
return true;
}
public static boolean LabelShapeStatisticsTest()
{
int size = 10;
int i;
long j;
short val = 1;
/* Make a 10x10 test image */
Image image = new Image(size, size, PixelIDValueEnum.sitkUInt8);
VectorUInt32 idx = new VectorUInt32( 2 );
/* Fill in a 4x4 square in the middle of the image */
for (j=4; j<8; j++)
{
idx.set(1, j);
for (i=4; i<8; i++)
{
idx.set(0, i);
image.setPixelAsUInt8(idx, val);
}
}
try
{
/* Run statistics on the test image */
LabelShapeStatisticsImageFilter filter = new LabelShapeStatisticsImageFilter();
filter.execute(image);
VectorInt64 labels = filter.getLabels();
j = BigIntegerFix.Convert( filter.getNumberOfLabels() );
if (j != 1)
{
throw new Exception("Wrong number of labels");
}
long npix;
double perim;
System.out.println("Label,\t#pix,\tperimeter");
for (i=0; i<j; i++)
{
npix = BigIntegerFix.Convert( filter.getNumberOfPixels(labels.get(i)) );
perim = filter.getPerimeter( labels.get(i) );
System.out.format( "%d,\t%d,\t%f\n", labels.get(i), npix, perim );
/* The first (and only) label should have 16 pixels */
if (i==0)
{
if (npix != 16)
{
throw new Exception("Wrong number of pixels");
}
}
}
}
catch (Exception e)
{
return false;
}
return true;
}
}
|