Tuesday, July 5, 2011

OPENCV2 C++ basics (OOP) edge detection using CANNY

if you have an OK background on C++ you can follow easily:

ive seperated the camera capturing and the edge detection to separate classes.
the main entry point remains the same.
you can use the code to add a new class to process images using the atscameraCapture class.

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

//for atsEdges
#include <opencv2/imgproc/imgproc.hpp>

class atscameraCapture
{
public:
    cv::Mat ats_getImage()
    {
        //check if Camera is still open
        if (!cap.isOpened())
        {
            std::cout< "Cant Capture";
            exit(0);
        }
        //camera is open so get a frame
            cv::Mat frame;
            cap>> frame;
        //return the frame
            return frame;
    }
    atscameraCapture()
    {
        //create a camera capture and assign it to a local private variable
        cv::VideoCapture capture(0);
        cap = capture;
    }
private:
    cv::VideoCapture cap;
};


class atsEdges
{
public:
    cv::Mat getEdges(cv::Mat image)
    {
        cv::Mat edges;
        cvtColor(image,edges,CV_BGR2GRAY); //convert from RGB color space to GRAY
        Canny(edges,edges,
            0,    // double threshold1
            30,    //double threshold2
            3); //int apertureSize=3, bool L2gradient=false)
        return edges;

    }
   
private:
};

int main()
{
    atscameraCapture mine; //initialize
    for(;;)
        {
        cv::Mat image = mine.ats_getImage();
        cv::imshow("image",image);
       
        atsEdges pl;

        cv::imshow("pl image",pl.getEdges(image));

        if(cv::waitKey(30) >= 0) break;
        }
    return 0;
}

No comments:

Post a Comment