@@ -34,6 +34,7 @@ module OpenCV.ImgProc.ImgFiltering
34
34
( MorphShape (.. )
35
35
, MorphOperation (.. )
36
36
37
+ , bilateralFilter
37
38
, laplacian
38
39
, medianBlur
39
40
, erode
@@ -132,6 +133,84 @@ marshalMorphOperation = \case
132
133
-- Image Filtering
133
134
--------------------------------------------------------------------------------
134
135
136
+ {- | Calculates the bilateralFilter of an image
137
+
138
+ The function applies bilateral filtering to the input image, as described in
139
+ <http://www.dai.ed.ac.uk/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html Bilateral_Filtering>
140
+ bilateralFilter can reduce unwanted noise very well while keeping edges fairly sharp. However, it is very slow compared to most filters.
141
+ Example:
142
+
143
+ @
144
+ bilateralFilterImg
145
+ :: forall (width :: Nat)
146
+ (width2 :: Nat)
147
+ (height :: Nat)
148
+ (channels :: Nat)
149
+ (depth :: *)
150
+ . ( Mat (ShapeT [height, width]) ('S channels) ('S depth) ~ Birds_512x341
151
+ , width2 ~ ((*) width 2) -- TODO (RvD): HSE parse error with infix type operator
152
+ )
153
+ => Mat (ShapeT [height, width2]) ('S channels) ('S depth)
154
+ bilateralFilterImg = exceptError $
155
+ withMatM (Proxy :: Proxy [height, width2])
156
+ (Proxy :: Proxy channels)
157
+ (Proxy :: Proxy depth)
158
+ white $ \imgM -> do
159
+ birdsFiltered <- pureExcept $ bilateralFilter (Just 9) Nothing Nothing Nothing birds_512x341
160
+ matCopyToM imgM (V2 0 0) birds_512x341 Nothing
161
+ matCopyToM imgM (V2 w 0) birdsFiltered Nothing
162
+ where
163
+ w = fromInteger $ natVal (Proxy :: Proxy width)
164
+ @
165
+
166
+ <<doc/generated/examples/bilateralFilterImg.png bilateralFilterImg>>
167
+
168
+ <https://docs.opencv.org/3.0-last-rst/modules/imgproc/doc/filtering.html#bilateralfilter OpenCV Sphinx doc>
169
+ -}
170
+ bilateralFilter
171
+ :: ( depth `In ` '[Word8 , Float , Double ]
172
+ , channels `In ` '[1 , 3 ]
173
+ -- , Length shape <= 2
174
+ )
175
+ => Maybe Int32
176
+ -- ^ Diameter of each pixel neighborhood that is used during filtering.
177
+ -- If it is non-positive, it is computed from sigmaSpace. Default value is 5.
178
+ -> Maybe Double
179
+ -- ^ Filter sigma in the color space. A larger value of the parameter means that farther colors within
180
+ -- the pixel neighborhood (see sigmaSpace) will be mixed together, resulting in larger areas of semi-equal color.
181
+ -- Default value is 50
182
+ -> Maybe Double
183
+ -- ^ Filter sigma in the coordinate space. A larger value of the parameter means that farther pixels will
184
+ -- influence each other as long as their colors are close enough (see sigmaColor ). When d>0, it specifies
185
+ -- the neighborhood size regardless of sigmaSpace. Otherwise, d is proportional to sigmaSpace.
186
+ -- Default value is 50
187
+ -> Maybe BorderMode
188
+ -- ^ Pixel extrapolation method. Default value is BorderReflect101
189
+ -> Mat shape ('S channels ) ('S depth )
190
+ -> CvExcept (Mat shape ('S channels ) ('S depth ))
191
+ bilateralFilter d sigmaColor sigmaSpace borderType src = unsafeWrapException $ do
192
+ dst <- newEmptyMat
193
+ handleCvException (pure $ unsafeCoerceMat dst) $
194
+ withPtr src $ \ srcPtr ->
195
+ withPtr dst $ \ dstPtr ->
196
+ [cvExcept |
197
+ cv::bilateralFilter
198
+ ( *$(Mat * srcPtr )
199
+ , *$(Mat * dstPtr )
200
+ , $(int32_t c'd )
201
+ , $(double c'sigmaColor)
202
+ , $(double c'sigmaSpace)
203
+ , $(int32_t c'borderType)
204
+ );
205
+ |]
206
+ where
207
+ c'd = fromMaybe 5 d
208
+ c'sigmaColor = maybe 50 realToFrac sigmaColor
209
+ c'sigmaSpace = maybe 50 realToFrac sigmaSpace
210
+ c'borderType = fst $ marshalBorderMode $ fromMaybe BorderReflect101 borderType
211
+
212
+
213
+
135
214
{- | Calculates the Laplacian of an image
136
215
137
216
The function calculates the Laplacian of the source image by adding up
0 commit comments