C++:
void dilate(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar& borderValue=morphologyDefaultBorderValue() )
Python:
cv2.dilate(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) → dst
Parameters: |
|
The function dilates the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the maximum is taken:
The function supports the in-place mode. Dilation can be applied several ( iterations ) times. In case of multi-channel images, each channel is processed independently.
Note: An example using the morphological dilate operation can be found at opencv_source_code/samples/cpp/morphology2.cpp
Example
This is a sample code (C++) with images for opencv box filter. Since dilate is a most common feature for optical characters, the source image includes some texts and shapes.
string imgFileName = "lenaWithText.jpg";
cv::Mat src = cv::imread(imgFileName);
if (!src.data){
cout << "Unable to open file" << endl;
getchar();
return 1;
}
//---------------- create kernals ----------------
int dilationSize = 2;
cv::Mat kernalMorphCross = cv::getStructuringElement(cv::MORPH_CROSS,
cv::Size(2 * dilationSize + 1, 2 * dilationSize + 1),
cv::Point(dilationSize, dilationSize));
cv::Mat kernalMorphDilate = cv::getStructuringElement(cv::MORPH_DILATE,
cv::Size(2 * dilationSize + 1, 2 * dilationSize + 1),
cv::Point(dilationSize, dilationSize));
cv::Mat kernalMorphEllipse = cv::getStructuringElement(cv::MORPH_ELLIPSE,
cv::Size(2 * dilationSize + 1, 2 * dilationSize + 1),
cv::Point(dilationSize, dilationSize));
cv::Mat kernalMorphErode = cv::getStructuringElement(cv::MORPH_ERODE,
cv::Size(2 * dilationSize + 1, 2 * dilationSize + 1),
cv::Point(dilationSize, dilationSize));
cv::Mat kernalMorphOpen = cv::getStructuringElement(cv::MORPH_OPEN,
cv::Size(2 * dilationSize + 1, 2 * dilationSize + 1),
cv::Point(dilationSize, dilationSize));
cv::Mat kernalMorphRect = cv::getStructuringElement(cv::MORPH_RECT,
cv::Size(2 * dilationSize + 1, 2 * dilationSize + 1),
cv::Point(dilationSize, dilationSize));
//---------------- apply dilate kernal to source seperately ----------------
cv::Mat dstMorphCross;
cv::dilate(src, dstMorphCross, kernalMorphCross);
cv::Mat dstMorphDilate;
cv::dilate(src, dstMorphDilate, kernalMorphDilate);
cv::Mat dstMorphEllipse;
cv::dilate(src, dstMorphEllipse, kernalMorphEllipse);
cv::Mat dstMorphErode;
cv::dilate(src, dstMorphErode, kernalMorphErode);
cv::Mat dstMorphOpen;
cv::dilate(src, dstMorphOpen, kernalMorphOpen);
cv::Mat dstMorphRect;
cv::dilate(src, dstMorphRect, kernalMorphRect);
//---------------- Show filtered images ----------------
cv::namedWindow("Source");
cv::namedWindow("DilateMorphCross");
cv::namedWindow("DilateMorphDilate");
cv::namedWindow("DilateMorphEllipse");
cv::namedWindow("DilateMorphErode");
cv::namedWindow("DilateMorphOpen");
cv::namedWindow("DilateMorphRect");
cv::imshow("Source", src);
cv::imshow("DilateMorphCross", dstMorphCross);
cv::imshow("DilateMorphDilate", dstMorphDilate);
cv::imshow("DilateMorphEllipse", dstMorphEllipse);
cv::imshow("DilateMorphErode", dstMorphErode);
cv::imshow("DilateMorphOpen", dstMorphOpen);
cv::imshow("DilateMorphRect", dstMorphRect);
cv::waitKey(0);
//---------------- Save filtered images ----------------
cv::imwrite("DilateMorphCross.jpg", dstMorphCross);
cv::imwrite("DilateMorphDilate.jpg", dstMorphDilate);
cv::imwrite("DilateMorphEllipse.jpg", dstMorphEllipse);
cv::imwrite("DilateMorphErode.jpg", dstMorphErode);
cv::imwrite("DilateMorphOpen.jpg", dstMorphOpen);
cv::imwrite("DilateMorphRect.jpg", dstMorphRect);
Filtered Image | Source Image |
Kernel shape - MORPH_CROSS Kernel shape - MORPH_DILATE Kernel Shape - MORPH_ELLIPSE Kernel Shape - MORPH_ERODE Kernel Shape - MORPH_OPEN Kernel Shape - MORPH_RECT |
Download complete Visual Studio project.