Ask Your Question
0

How to call a function in a main function With OpenCv and C++?

asked 2017-09-13 04:10:51 -0600

louis89 gravatar image

Hi.I use below code for video capture from my webcam:

Mat frame;
int threshval =120;
static void on_trackbar(int, void*)
{


 }
 int main(int argc, char* argv[])
 {

     VideoCapture cap(0);
     if (!cap.isOpened())
     {
         cout << "Cannot open the video cam" << endl;
         return  0;

     }

     while (1)
     {
         bool bSuccess = cap.read(frame); // Khandane yek frame jadid dar video


         if (!bSuccess)
         {
             cout << "Cannot read a frame from video stream" << endl;
             break;
         }
    createTrackbar("Threshold", "Image", &threshval, 255, on_trackbar);
    on_trackbar(threshval, 0);}
             return 0; }

I want to use a function that takes the image from video capture class and convert that frame to a gray level image and use Canny Edge Detector for that image.for this reason I use this function:

int my_cannyEdgeDetector(){

         cvtColor(frame, frame2, COLOR_BGR2GRAY);
         Canny(frame2, frame2, threshval, threshval * 2, 3);}

I know I wrote the wrong function but I'm begginer in opencv and c++ and I want to get pictures from the main function and put it in the my_cannyEdgeDetector function and imshow frame and frame2 in the end of my main function.I have no idea to set input and output for my_cannyEdgeDetector function.Thank you very much if you help me do this

edit retag flag offensive close merge delete

Comments

one example could be:

static void my_cannyEdgeDetector(const cv::Mat& frame, cv::Mat& frame2);

function call:

cv::Mat frame2;
my_cannyEdgeDetector(frame, frame2)
VxW gravatar imageVxW ( 2017-09-13 04:43:08 -0600 )edit

@Volkmar .thanks for your reply.but where should I add your code to my program?another question is that Is not it necessary to use a loop for read frame in my_cannyEdgeDetector function?and is not it necessary to set threshval as an input in my_cannyEdgeDetector?

louis89 gravatar imagelouis89 ( 2017-09-13 05:08:04 -0600 )edit

For information: cv::Canny can handle 3 channel images. Edge detection should be more stable than converting the image to grayscale before. But processing could be slower in this case.

matman gravatar imagematman ( 2017-09-13 11:20:44 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2017-09-13 05:39:03 -0600

VxW gravatar image

try this:

#include "stdafx.h"
#include <opencv2/opencv.hpp>

static void my_cannyEdgeDetector(const cv::Mat& frame, cv::Mat& frame2, int threshval)
{
    cvtColor(frame, frame2, cv::COLOR_BGR2GRAY);
    Canny(frame2, frame2, threshval, threshval * 2, 3);

}

int main(int argc, char* argv[])
{
    cv::VideoCapture cap;
    std::string path = "video.avi";

    cap.open(path);

    if (!cap.isOpened())
    {
        std::cout << "Could not open reference "  << std::endl;
        return -1;
    }
    else
    {
        cv::Mat frame, frame2;

        for (;;)
        {
            cap >> frame;

            my_cannyEdgeDetector(frame, frame2, 128);

            cv::imshow("test", frame2);

            if((cv::waitKey(2) & 0xEFFFFF) == 27)//esc
                break;
        }
    }
    return 0;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-13 04:10:51 -0600

Seen: 1,730 times

Last updated: Sep 13 '17