34 #ifndef _vpImageMorphology_h_
35 #define _vpImageMorphology_h_
42 #include <visp3/core/vpImage.h>
43 #include <visp3/core/vpImageException.h>
44 #include <visp3/core/vpMatrix.h>
90 static void imageOperation(
vpImage<T> &I,
const T &null_value,
const T &(*operation)(
const T &,
const T &),
const vpConnexityType &connexity = CONNEXITY_4);
101 template <
typename T>
102 static void imageOperation(
vpImage<T> &I,
const T &(*operation)(
const T &,
const T &),
const int &size = 3);
105 template <
class Type>
106 static void erosion(
vpImage<Type> &I, Type value, Type value_out, vpConnexityType connexity = CONNEXITY_4);
108 template <
class Type>
109 static void dilatation(
vpImage<Type> &I, Type value, Type value_out, vpConnexityType connexity = CONNEXITY_4);
111 template <
typename T>
112 static void erosion(
vpImage<T> &I,
const vpConnexityType &connexity = CONNEXITY_4);
114 template <
typename T>
115 static void dilatation(
vpImage<T> &I,
const vpConnexityType &connexity = CONNEXITY_4);
117 template <
typename T>
118 static void erosion(
vpImage<T> &I,
const int &size);
120 template <
typename T>
121 static void dilatation(
vpImage<T> &I,
const int &size);
123 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
141 vpImageMorphology::erosion<unsigned char>(I, connexity);
158 vpImageMorphology::dilatation<unsigned char>(I, connexity);
181 template <
class Type>
185 std::cerr <<
"Input image is empty!" << std::endl;
191 for (
unsigned int i = 0; i < J.
getHeight(); i++) {
193 for (
unsigned int j = 0; j < J.
getWidth(); j++) {
199 memcpy(J[i] + 1, I[i - 1],
sizeof(
unsigned char) * I.
getWidth());
205 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
206 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
207 if (J[i + 1][j + 1] == value) {
209 if ((J[i][j + 1] == value_out) ||
210 (J[i + 2][j + 1] == value_out) ||
211 (J[i + 1][j] == value_out) ||
212 (J[i + 1][j + 2] == value_out)) {
220 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
221 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
222 if (J[i + 1][j + 1] == value) {
224 if ((J[i][j] == value_out) || (J[i][j + 1] == value_out) || (J[i][j + 2] == value_out) ||
225 (J[i + 1][j] == value_out) || (J[i + 1][j + 2] == value_out) || (J[i + 2][j] == value_out) ||
226 (J[i + 2][j + 1] == value_out) || (J[i + 2][j + 2] == value_out))
251 template <
class Type>
255 std::cerr <<
"Input image is empty!" << std::endl;
261 for (
unsigned int i = 0; i < J.
getHeight(); i++) {
263 for (
unsigned int j = 0; j < J.
getWidth(); j++) {
269 memcpy(J[i] + 1, I[i - 1],
sizeof(
unsigned char) * I.
getWidth());
275 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
276 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
277 if (J[i + 1][j + 1] == value_out) {
279 if ((J[i][j + 1] == value) ||
280 (J[i + 2][j + 1] == value) ||
281 (J[i + 1][j] == value) ||
282 (J[i + 1][j + 2] == value)) {
290 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
291 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
292 if (J[i + 1][j + 1] == value_out) {
294 if ((J[i][j] == value) || (J[i][j + 1] == value) || (J[i][j + 2] == value) || (J[i + 1][j] == value) ||
295 (J[i + 1][j + 2] == value) || (J[i + 2][j] == value) || (J[i + 2][j + 1] == value) ||
296 (J[i + 2][j + 2] == value)) {
306 void vpImageMorphology::imageOperation(
vpImage<T> &I,
const T &null_value,
const T &(*operation)(
const T &,
const T &),
const vpConnexityType &connexity)
310 const int width_dilat = width_in + 2;
311 const int height_dilat = height_in + 2;
312 vpImage<T> J(height_dilat, width_dilat, null_value);
318 const int nbOffset = 5;
319 int offset_x[nbOffset] = { 0, -1, 0, 1, 0 };
320 int offset_y[nbOffset] = { -1, 0, 0, 0, 1 };
322 for (
int i = 0; i < height_in; i++) {
323 for (
int j = 0; j < width_in; j++) {
324 T value = null_value;
325 for (
int k = 0; k < nbOffset; k++) {
326 value = operation(value, J[i + 1 + offset_y[k]][j + 1 + offset_x[k]]);
334 const int nbOffset = 9;
335 int offset_x[nbOffset] = { -1, 0, 1,-1, 0, 1,-1, 0, 1 };
336 int offset_y[nbOffset] = { -1,-1,-1, 0, 0, 0, 1, 1, 1 };
338 for (
int i = 0; i < height_in; i++) {
339 for (
int j = 0; j < width_in; j++) {
340 T value = null_value;
341 for (
int k = 0; k < nbOffset; k++) {
342 value = operation(value, J[i + 1 + offset_y[k]][j + 1 + offset_x[k]]);
374 template <
typename T>
377 const T &(*operation)(
const T & a,
const T & b) = std::min;
378 vpImageMorphology::imageOperation(I, std::numeric_limits<T>::max(), operation, connexity);
404 template <
typename T>
407 const T &(*operation)(
const T & a,
const T & b) = std::max;
408 vpImageMorphology::imageOperation(I, std::numeric_limits<T>::min(), operation, connexity);
412 void vpImageMorphology::imageOperation(
vpImage<T> &I,
const T &(*operation)(
const T &,
const T &),
const int &size)
420 int halfKernelSize = size / 2;
423 for (
int r = 0; r < height_in; r++) {
425 int r_iterator_start = -halfKernelSize, r_iterator_stop = halfKernelSize + 1;
426 if (r - halfKernelSize < 0) {
427 r_iterator_start = -r;
429 else if (r + halfKernelSize >= height_in) {
430 r_iterator_stop = height_in - r;
432 for (
int c = 0; c < width_in; c++) {
435 int c_iterator_start = -halfKernelSize, c_iterator_stop = halfKernelSize + 1;
436 if (c - halfKernelSize < 0) {
437 c_iterator_start = -c;
439 else if (c + halfKernelSize >= width_in) {
440 c_iterator_stop = width_in - c;
442 for (
int r_iterator = r_iterator_start; r_iterator < r_iterator_stop; r_iterator++) {
443 for (
int c_iterator = c_iterator_start; c_iterator < c_iterator_stop; c_iterator++) {
444 value = operation(value, J[r + r_iterator][c + c_iterator]);
478 template <
typename T>
481 const T &(*operation)(
const T & a,
const T & b) = std::min;
482 vpImageMorphology::imageOperation(I, operation, size);
513 const T &(*operation)(
const T & a,
const T & b) = std::max;
514 vpImageMorphology::imageOperation(I, operation, size);
error that can be emitted by ViSP classes.
@ badValue
Used to indicate that a value is not in the allowed range.
Various mathematical morphology tools, erosion, dilatation...
static void dilatation(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
static vp_deprecated void erosion(vpImage< unsigned char > &I, const vpConnexityType &connexity=CONNEXITY_4)
An erosion is performed with a flat structuring element . The erosion using such a structuring elemen...
static void erosion(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
static vp_deprecated void dilatation(vpImage< unsigned char > &I, const vpConnexityType &connexity=CONNEXITY_4)
A dilatation is performed with a flat structuring element . The erosion using such a structuring elem...
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
unsigned int getWidth() const
unsigned int getSize() const
unsigned int getHeight() const