OpenCV  4.2.0
Open Source Computer Vision
Image Segmentation with Distance Transform and Watershed Algorithm

Prev Tutorial: Point Polygon Test
Next Tutorial: Out-of-focus Deblur Filter

Goal

In this tutorial you will learn how to:

  • Use the OpenCV function cv::filter2D in order to perform some laplacian filtering for image sharpening
  • Use the OpenCV function cv::distanceTransform in order to obtain the derived representation of a binary image, where the value of each pixel is replaced by its distance to the nearest background pixel
  • Use the OpenCV function cv::watershed in order to isolate objects in the image from the background

Theory

Code

Explanation / Result

  • Load the source image and check if it is loaded without any problem, then show it:
  • Then if we have an image with a white background, it is good to transform it to black. This will help us to discriminate the foreground objects easier when we will apply the Distance Transform:
  • Afterwards we will sharpen our image in order to acute the edges of the foreground objects. We will apply a laplacian filter with a quite strong filter (an approximation of second derivative):
  • Now we transform our new sharpened source image to a grayscale and a binary one, respectively:
  • We are ready now to apply the Distance Transform on the binary image. Moreover, we normalize the output image in order to be able visualize and threshold the result:
  • We threshold the dist image and then perform some morphology operation (i.e. dilation) in order to extract the peaks from the above image:
  • From each blob then we create a seed/marker for the watershed algorithm with the help of the cv::findContours function:
  • Finally, we can apply the watershed algorithm, and visualize the result:
cv::RNG::uniform
int uniform(int a, int b)
returns uniformly distributed integer random number from [a,b) range
cv::Vec3b
Vec< uchar, 3 > Vec3b
Definition: matx.hpp:401
cv::Mat::rows
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition: mat.hpp:2086
cv::THRESH_OTSU
flag, use Otsu algorithm to choose the optimal threshold value
Definition: imgproc.hpp:323
cv::String
std::string String
Definition: cvstd.hpp:150
cv::DIST_L2
the simple euclidean distance
Definition: imgproc.hpp:299
cv::watershed
void watershed(InputArray image, InputOutputArray markers)
Performs a marker-based image segmentation using the watershed algorithm.
cv::Mat::clone
Mat clone() const CV_NODISCARD
Creates a full copy of the array and the underlying data.
cv::filter2D
void filter2D(InputArray src, OutputArray dst, int ddepth, InputArray kernel, Point anchor=Point(-1,-1), double delta=0, int borderType=BORDER_DEFAULT)
Convolves an image with the kernel.
cv::theRNG
RNG & theRNG()
Returns the default random number generator.
cv::NORM_MINMAX
flag
Definition: base.hpp:207
cv::cvtColor
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
cv::THRESH_BINARY
Definition: imgproc.hpp:317
cv::samples::findFile
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
cv::Mat::zeros
static MatExpr zeros(int rows, int cols, int type)
Returns a zero array of the specified size and type.
cv::distanceTransform
void distanceTransform(InputArray src, OutputArray dst, OutputArray labels, int distanceType, int maskSize, int labelType=DIST_LABEL_CCOMP)
Calculates the distance to the closest zero pixel for each pixel of the source image.
cv::Mat::at
_Tp & at(int i0=0)
Returns a reference to the specified array element.
cv::normalize
void normalize(const SparseMat &src, SparseMat &dst, double alpha, int normType)
cv::RETR_EXTERNAL
Definition: imgproc.hpp:413
cv::threshold
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
Applies a fixed-level threshold to each array element.
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
CV_8U
#define CV_8U
Definition: interface.h:73
cv::distanceTransform
void distanceTransform(InputArray src, OutputArray dst, int distanceType, int maskSize, int dstType=CV_32F)
highgui.hpp
core.hpp
cv::findContours
void findContours(InputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
Finds contours in a binary image.
cv::Mat::convertTo
void convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const
Converts an array to another data type with optional scaling.
CV_32F
#define CV_32F
Definition: interface.h:78
cv::dilate
void dilate(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar &borderValue=morphologyDefaultBorderValue())
Dilates an image by using a specific structuring element.
cv::imread
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
cv::Mat::empty
bool empty() const
Returns true if the array has no elements.
cv::Vec
Template class for short numerical vectors, a partial case of Matx.
Definition: matx.hpp:332
cv::Mat::cols
int cols
Definition: mat.hpp:2086
CV_8UC3
#define CV_8UC3
Definition: interface.h:90
cv::Mat::size
MatSize size
Definition: mat.hpp:2108
cv::dnn::print
static void print(const MatShape &shape, const String &name="")
Definition: shape_utils.hpp:188
cv::findContours
void findContours(InputArray image, OutputArrayOfArrays contours, int mode, int method, Point offset=Point())
uchar
unsigned char uchar
Definition: interface.h:51
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::Scalar
Scalar_< double > Scalar
Definition: types.hpp:669
cv::drawContours
void drawContours(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar &color, int thickness=1, int lineType=LINE_8, InputArray hierarchy=noArray(), int maxLevel=INT_MAX, Point offset=Point())
Draws contours outlines or filled contours.
cv::Point
Point2i Point
Definition: types.hpp:194
cv::datasets::index
Definition: gr_skig.hpp:77
CV_32S
#define CV_32S
Definition: interface.h:77
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:791
cv::imshow
void imshow(const String &winname, const ogl::Texture2D &tex)
Displays OpenGL 2D texture in the specified window.
cv::hal::filter2D
void filter2D(int stype, int dtype, int kernel_type, uchar *src_data, size_t src_step, uchar *dst_data, size_t dst_step, int width, int height, int full_width, int full_height, int offset_x, int offset_y, uchar *kernel_data, size_t kernel_step, int kernel_width, int kernel_height, int anchor_x, int anchor_y, double delta, int borderType, bool isSubmatrix)
cv::CommandLineParser
Designed for command line parsing.
Definition: utility.hpp:796
cv::COLOR_BGR2GRAY
convert between RGB/BGR and grayscale, color conversions
Definition: imgproc.hpp:542
cv
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:51
imgproc.hpp
cv::Mat::ones
static MatExpr ones(int rows, int cols, int type)
Returns an array of all 1's of the specified size and type.
cv::bitwise_not
void bitwise_not(InputArray src, OutputArray dst, InputArray mask=noArray())
Inverts every bit of an array.
cv::Mat_< float >
cv::CHAIN_APPROX_SIMPLE
Definition: imgproc.hpp:434
cv::normalize
static Vec< _Tp, cn > normalize(const Vec< _Tp, cn > &v)
cv::datasets::circle
Definition: gr_skig.hpp:62
cv::circle
void circle(InputOutputArray img, Point center, int radius, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a circle.