#include <stdio.h>
#include <cstdlib>
return dilate(erode(img, mask), mask);
}
return erode(dilate(img, mask), mask);
}
}
return (img - morphopen(img, mask));
}
return (morphclose(img, mask) - img);
}
array border(
const array& img,
const int left,
const int right,
const int top,
const int bottom, const float value = 0.0) {
if ((
int)img.
dims(0) < (top + bottom))
printf("input does not have enough rows\n");
if ((
int)img.
dims(1) < (left + right))
fprintf(stderr, "input does not have enough columns\n");
ret(
seq(top, imgDims[0] - bottom),
seq(left, imgDims[1] - right), span,
span) = img(
seq(top, imgDims[0] - bottom),
seq(left, imgDims[1] - right), span, span);
return ret;
}
array border(
const array& img,
const int w,
const int h,
const float value = 0.0) {
return border(img, w, w, h, h, value);
}
array border(
const array& img,
const int size,
const float value = 0.0) {
return border(img, size, size, size, size, value);
}
for (
int i = 0; i < (int)blurred.
dims(2); i++)
blurred(span, span, i) =
convolve(img(span, span, i), mask);
return blurred;
}
static void morphing_demo() {
af::Window wnd(1280, 720,
"Morphological Operations");
array img_rgb =
loadImage(ASSETS_DIR
"/examples/images/man.jpg",
true) /
255.f;
array op = morphopen(img_rgb, mask);
array cl = morphclose(img_rgb, mask);
array gr = morphgrad(img_rgb, mask);
array th = tophat(img_rgb, mask);
array bh = bottomhat(img_rgb, mask);
array bp = border(img_rgb, 20, 30, 40, 50, 0.5);
array bo = border(img_rgb, 20);
while (!wnd.close()) {
wnd.grid(3, 4);
wnd(0, 0).image(img_rgb, "Input");
wnd(1, 0).image(er, "Erosion");
wnd(2, 0).image(di, "Dilation");
wnd(0, 1).image(op, "Opening");
wnd(1, 1).image(cl, "Closing");
wnd(2, 1).image(gr, "Gradient");
wnd(0, 2).image(th, "TopHat");
wnd(1, 2).image(bh, "BottomHat");
wnd(2, 2).image(bl, "Blur");
wnd(0, 3).image(bp, "Border to Gray");
wnd(1, 3).image(bo, "Border to black");
wnd.show();
}
}
int main(int argc, char** argv) {
int device = argc > 1 ? atoi(argv[1]) : 0;
try {
printf("** ArrayFire Image Morphing Demo **\n\n");
morphing_demo();
fprintf(stderr,
"%s\n", e.
what());
throw;
}
return 0;
}
Window object to render af::arrays.
A multi dimensional data container.
dim4 dims() const
Get dimensions of the array.
dtype type() const
Get array data type.
Generic object that represents size and shape.
An ArrayFire exception class.
virtual const char * what() const
Returns an error message for the exception in a string format.
seq is used to create sequences for indexing af::array
array constant(T val, const dim4 &dims, const dtype ty=(af_dtype) dtype_traits< T >::ctype)
C++ Interface to generate an array with elements set to a specified value.
AFAPI void setDevice(const int device)
Sets the current device.
AFAPI array dilate(const array &in, const array &mask)
C++ Interface for image dilation (max filter)
AFAPI array erode(const array &in, const array &mask)
C++ Interface for image erosion (min filter)
AFAPI array gaussianKernel(const int rows, const int cols, const double sig_r=0, const double sig_c=0)
C++ Interface for generating gausian kernels.
AFAPI array loadImage(const char *filename, const bool is_color=false)
C++ Interface for loading an image.
AFAPI array convolve(const array &signal, const array &filter, const convMode mode=AF_CONV_DEFAULT, const convDomain domain=AF_CONV_AUTO)
C++ Interface for convolution any(one through three) dimensional signals.