Helping using the dilate function OpenCV
Asked Answered
L

2

6

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;
}
Loppy answered 25/6, 2012 at 0:11 Comment(0)
C
19

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));
Causeuse answered 25/6, 2012 at 0:23 Comment(8)
Can you explain what the last one does?Sussna
The documentation of the function explains it well.Causeuse
-1, it is effectively in the documentation, but you are not linking it and like that the answer doesn't satisfy the Stack Overflow standard. Linked from: stackoverflow.com/questions/how-to-answer -> msmvps.com/blogs/jon_skeet/archive/2009/02/17/… Code without an explanation is rarely useful, however. At least provide a sentence or two to explain what's going on.Sussna
I understand that your question in the comment above was not answered, and I respect the fact that you are upset and downvoted it. On the other hand, this answer is from 2 years ago and it was meant to answer the guy who created this thread. In that sense, I believe this answer successfully solved the problem. The answer has 3 lines of code (1 variable declaration and 2 method calls), so it's completely unnecessary to comment what each line does. Maybe I should ping John and take his oppinion on the matter... :PCauseuse
@karlphilip I was not upset, decision to downvote is totally rational, I don't know if this make it worse. I believe you got a hint to make your answer more adherent to the standard, and you didn't follow it. If you edit your answer I will revert my downvote to upvote. Besides, who's John?Sussna
@Sussna Jon is the gentlemen that wrote the quote you made before. This answer was given 2 years ago and has been accepted by the OP officially as the problem solver. In my humble oppinion, becouse of that, there's no need to change anything. Thanks for your suggestions, see you around.Causeuse
Given that Stack Overflow posts are intended to provide future value to other users, the age of a post and whether or not it has been accepted should play no role in whether or not you decide to edit it. If you feel that this answer should remain as is apart from those reasons (i.e. you'd be perfectly happy posting this answer, as is, to this question, if you hadn't done so already), that's one thing, but then you probably shouldn't be using those reasons as the main points in your reasoning.Konstantine
I'm happy with the current answer. If you think you can improve it please feel free to edit it. I believe you have the rep required to do so.Causeuse
J
2

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.

Jephum answered 25/6, 2012 at 8:37 Comment(1)
Does not seem to be te case for Mat::copyTo, as the compiler complains: error: no matching function for call to ‘cv::Mat::copyTo(cv::Mat)’ note: candidates are: /usr/include/opencv2/core/core.hpp:1651:10: note: void cv::Mat::copyTo(cv::OutputArray) constManfred

© 2022 - 2024 — McMap. All rights reserved.