Skip to content

Commit ff66277

Browse files
authored
Merge pull request #2401 from SCIInstitute/2400-fit-region-value
Resolve #2400 - Feature Request: Add padding value parameter for Image::fitRegion
2 parents 64947c1 + abd8fda commit ff66277

File tree

7 files changed

+55
-4
lines changed

7 files changed

+55
-4
lines changed

Libs/Image/Image.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,14 +757,14 @@ Image& Image::crop(PhysicalRegion region, const int padding) {
757757
return *this;
758758
}
759759

760-
Image& Image::fitRegion(PhysicalRegion region) {
760+
Image& Image::fitRegion(PhysicalRegion region, const PixelType value) {
761761
// first pad to make sure the image is large enough to fit the region
762762

763763
// convert PhysicalRegion to IndexRegion
764764
IndexRegion indexRegion(physicalToLogical(region));
765765

766766
// pad the image
767-
pad(indexRegion, 0);
767+
pad(indexRegion, value);
768768

769769
// fix origin and indices
770770
using RegionFilterType = itk::RegionOfInterestImageFilter<ImageType, ImageType>;

Libs/Image/Image.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class Image {
189189
Image& crop(PhysicalRegion region, const int padding = 0);
190190

191191
/// crops (or pads) the image to fit the given region
192-
Image& fitRegion(PhysicalRegion region);
192+
Image& fitRegion(PhysicalRegion region, const PixelType value = 0.0);
193193

194194
/// clips an image using a cutting plane
195195
Image& clip(const Plane plane, const PixelType val = 0.0);

Libs/Python/ShapeworksPython.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ PYBIND11_MODULE(shapeworks_py, m) {
346346
.def("crop", &Image::crop, "crops the image down to the given (physical) region, with optional padding",
347347
"region"_a, "padding"_a = 0)
348348

349-
.def("fitRegion", &Image::fitRegion, "crops or pads the image to fit a region", "region"_a)
349+
.def("fitRegion", &Image::fitRegion, "crops or pads the image to fit a region with optional padding value",
350+
"region"_a, "value"_a = 0)
350351

351352
.def(
352353
"clip",

Testing/PythonTests/PythonTests.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,7 @@ TEST(pythonTests, projectTest) {
346346
TEST(pythonTests, thicknessTest) {
347347
run_test("thickness.py");
348348
}
349+
350+
TEST(pythonTests, fitRegionTest) {
351+
run_test("fitregion.py");
352+
}

Testing/PythonTests/fitregion.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import os
2+
import sys
3+
from shapeworks import *
4+
5+
success = True
6+
7+
def fitRegion1():
8+
img = Image(os.environ["DATA"] + "/seg.ellipsoid_1.nrrd")
9+
10+
region = img.logicalBoundingBox().pad(16)
11+
12+
region.min[0] = 7
13+
region.max[0] = 42
14+
15+
img.fitRegion(img.logicalToPhysical(region))
16+
17+
# img.write(os.environ["DATA"] + "/fitregion1_baseline.nrrd")
18+
compareImg = Image(os.environ["DATA"] + "/fitregion1_baseline.nrrd")
19+
20+
return img.compare(compareImg)
21+
22+
success &= utils.test(fitRegion1)
23+
24+
25+
def fitRegion2():
26+
img = Image(os.environ["DATA"] + "/seg.ellipsoid_1.nrrd")
27+
28+
region = img.logicalBoundingBox().pad(16)
29+
30+
region.min[0] = 7
31+
region.max[0] = 42
32+
33+
img.fitRegion(img.logicalToPhysical(region), -2000)
34+
35+
# img.write(os.environ["DATA"] + "/fitregion2_baseline.nrrd")
36+
compareImg = Image(os.environ["DATA"] + "/fitregion2_baseline.nrrd")
37+
38+
return img.compare(compareImg)
39+
40+
success &= utils.test(fitRegion2)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:260e5facb19fde257f135274ee472d06144314e08c945f236adbcd1a94d873c8
3+
size 2760
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:5f80da8d7c7244a258b28051b6ce8ad5be628ed4a9836c20302e431371555a1a
3+
size 5771

0 commit comments

Comments
 (0)