OpenCV Filters - dilate

Dilates an image by using a specific structuring element.

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:
  • src – input image; the number of channels can be arbitrary, but the depth should be one of CV_8U, CV_16U, CV_16S, CV_32F` or ``CV_64F.
  • dst – output image of the same size and type as src.
  • element – structuring element used for dilation; if element=Mat() , a 3 x 3 rectangular structuring element is used.
  • anchor – position of the anchor within the element; default value (-1, -1) means that the anchor is at the element center.
  • iterations – number of times dilation is applied.
  • borderType – pixel extrapolation method (see borderInterpolate() for details).
  • borderValue – border value in case of a constant border (see createMorphologyFilter() for details).


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.

1 comment:

  1. one can seek the best technical support assistance on issue regarding how to unlock hotmail account without phone then don't be hassle just fix it with phone call 1-888-576-1584

    ReplyDelete