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;
}
karlphillip
  • 92,053
  • 36
  • 243
  • 426
andrestoga
  • 619
  • 3
  • 9
  • 19

2 Answers2

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));
Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Can you explain what the last one does? – Antonio Feb 25 '14 at 08:42
  • The documentation of the function explains it well. – karlphillip Feb 25 '14 at 13:46
  • 5
    -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: http://stackoverflow.com/questions/how-to-answer -> http://msmvps.com/blogs/jon_skeet/archive/2009/02/17/answering-technical-questions-helpfully.aspx `Code without an explanation is rarely useful, however. At least provide a sentence or two to explain what's going on.` – Antonio Feb 25 '14 at 22:51
  • 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... :P – karlphillip Feb 26 '14 at 01:36
  • @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? – Antonio Feb 26 '14 at 08:55
  • @Antonio 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. – karlphillip Feb 26 '14 at 13:22
  • 3
    Given that [so] 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. – Bernhard Barker Feb 27 '14 at 11:57
  • 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. – karlphillip Feb 27 '14 at 14:02
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.

ArtemStorozhuk
  • 8,715
  • 4
  • 35
  • 53
  • 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) const` – pbond Aug 06 '13 at 13:35