Skip to content

Commit 3381ed4

Browse files
committed
Check for valid data in addPairwiseBilateral and bail.
1 parent a9d7be1 commit 3381ed4

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ d.addPairwiseGaussian(sxy=3, compat=3)
101101
d.addPairwiseBilateral(sxy=80, srgb=13, rgbim=im, compat=10)
102102
```
103103

104+
An important caveat is that `addPairwiseBilateral` only works for RGB images, i.e. three channels.
105+
If your data is of different type than this simple but common case, you'll need to compute your
106+
own pairwise energy using `utils.create_pairwise_bilateral`; see the [generic non-2D case](https://github.com/lucasb-eyer/pydensecrf#generic-non-2d) for details.
107+
104108
### Compatibilities
105109

106110
The `compat` argument can be any of the following:

pydensecrf/densecrf.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,5 @@ cdef class DenseCRF:
9090

9191
cdef class DenseCRF2D(DenseCRF):
9292
cdef c_DenseCRF2D *_this2d
93+
cdef int _w
94+
cdef int _h

pydensecrf/densecrf.pyx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ cdef class DenseCRF2D(DenseCRF):
9696
if type(self) is DenseCRF2D:
9797
self._this = self._this2d = new c_DenseCRF2D(w, h, nlabels)
9898

99+
# Unfortunately, self._this2d.W_ and .H_ are protected in C++ and thus
100+
# we cannot access them from here for sanity-checks, so keep our own...
101+
self._w = w
102+
self._h = h
103+
99104
def addPairwiseGaussian(self, sxy, compat, KernelType kernel=DIAG_KERNEL, NormalizationType normalization=NORMALIZE_SYMMETRIC):
100105
if isinstance(sxy, Number):
101106
sxy = (sxy, sxy)
@@ -109,6 +114,11 @@ cdef class DenseCRF2D(DenseCRF):
109114
if isinstance(srgb, Number):
110115
srgb = (srgb, srgb, srgb)
111116

117+
if rgbim.shape[0] != self._h or rgbim.shape[1] != self._w:
118+
raise ValueError("Bad shape for pairwise bilateral (Need {}, got {})".format((self._h, self._w, 3), rgbim.shape))
119+
if rgbim.shape[2] != 3:
120+
raise ValueError("addPairwiseBilateral only works for RGB images. For other types, use `utils.create_pairwise_bilateral` to construct your own pairwise energy and add it through `addPairwiseEnergy`.")
121+
112122
self._this2d.addPairwiseBilateral(
113123
sxy[0], sxy[1], srgb[0], srgb[1], srgb[2], &rgbim[0,0,0], _labelcomp(compat), kernel, normalization
114124
)

0 commit comments

Comments
 (0)