Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Helping using the dilate function OpenCV

In the following code I want to use the dilate function but I don't know how to cast a Mat class into a InputArray and OutputArray. Can you help me?

Using this prototype function:

void dilate(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar& borderValue=morphologyDefaultBorderValue() )

Here's my code:

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{
    Mat edges;

    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;


    for(;;)
    {

        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, CV_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        //dilate(edges,edges,NULL);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", frame);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}
like image 542
andrestoga Avatar asked Jun 25 '12 00:06

andrestoga


People also ask

What is dilate in OpenCV?

Dilation is a technique where we expand the image. It adds the number of pixels to the boundaries of objects in an image. The structuring element controls it.

Why we use cv2 dilate?

The main effect of the dilation on a binary image is to continuously increase the boundaries of regions of foreground pixels (for example, white pixels, typically). Thus areas of foreground pixels expand in size while holes within those regions become smaller.

How does dilation work in image processing?

Dilation adds pixels to the boundaries of objects in an image, while erosion removes pixels on object boundaries. The number of pixels added or removed from the objects in an image depends on the size and shape of the structuring element used to process the image.

Why do we use dilation?

Dilation is used to make the objects larger or smaller. This transformation produces an image that is the same as the original shape. But there is a difference in the size of the shape.


2 Answers

There are examples all around Stack Overflow, like this:

int erosion_size = 6;   
cv::Mat element = cv::getStructuringElement(cv::MORPH_CROSS,
                      cv::Size(2 * erosion_size + 1, 2 * erosion_size + 1), 
                      cv::Point(erosion_size, erosion_size) );

cv::dilate(edges, edges, element); 

Or this:

cv::dilate(edges, edges, cv::Mat(), cv::Point(-1,-1));
like image 175
karlphillip Avatar answered Sep 30 '22 14:09

karlphillip


in the following code I want to use the dilate function but I don't know how to cast a Mat class into a InputArray and OutputArray. Can you help me?

Well, you can use Mat as Inputarray/Outputarray parameter without casting. See official docs.

And also here's offificial OpenCV erode/delate tutorial. Or you can you can use samples from karlphillip's post.

like image 29
ArtemStorozhuk Avatar answered Sep 30 '22 14:09

ArtemStorozhuk