Skip to content

Experimental Vision Code

Kira Noël edited this page Mar 10, 2017 · 2 revisions

EDIT PAGE AND THEN COPY EVERYTHING BELOW

#include

#include <CameraServer.h> #include <IterativeRobot.h> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/core/core.hpp> #include <opencv2/core/types.hpp>

/**

  • This is a demo program showing the use of OpenCV to do vision processing. The

  • image is acquired from the USB camera, then a rectangle is put on the image and

  • sent to the dashboard. OpenCV has many methods for different types of

  • processing. */ class Robot: public frc::IterativeRobot { private: static void VisionThread() { // Get the USB camera from CameraServer cs::UsbCamera camera = CameraServer::GetInstance()->StartAutomaticCapture(0); // Set the resolution camera.SetResolution(160, 120);

     // Get a CvSink. This will capture Mats from the Camera
     cs::CvSink cvSink = CameraServer::GetInstance()->GetVideo();
     // Setup a CvSource. This will send images back to the Dashboard
     cs::CvSource outputStream = CameraServer::GetInstance()->
     		PutVideo("Rectangle", 160, 120);
    
     // Mats are very memory expensive. Lets reuse this Mat.
     cv::Mat mat;
     cv::Mat out;
    
     while (true) {
     	// Tell the CvSink to grab a frame from the camera and put it
     	// in the source mat.  If there is an error notify the output.
     	if (cvSink.GrabFrame(mat) == 0) {
     		// Send the output the error.
     		outputStream.NotifyError(cvSink.GetError());
     		// skip the rest of the current iteration
     		continue;
     	}
     	// Resize the image
     	cv::resize(mat, mat, cv::Size(160, 120), 0.0, 0.0, cv::INTER_CUBIC);
    
     	// HSL threshold
     	double hue[] = {30, 110};
     	double saturation[] = {30, 255};
     	double luminance[] = {25, 255};
     	cv::cvtColor(mat, out, cv::COLOR_BGR2HLS);
     	cv::inRange(out, cv::Scalar(hue[0], luminance[0], saturation[0]),
     			cv::Scalar(hue[1], luminance[1], saturation[1]), out);
    
     	// Find contours
     	//std::vector<cv::Vec4i> hierarchy;
     	//std::vector<std::vector<cv::Point> > contours;
     	//contours.clear();
     	//int mode = false ? cv::RETR_EXTERNAL : cv::RETR_LIST;
     	//int method = cv::CHAIN_APPROX_SIMPLE;
     	//cv::findContours(mat, contours, hierarchy, mode, method);
    
     	//cv::Mat output = cv::Mat(contours);
    
     	// Output to SmartDashboard
     	outputStream.PutFrame(out);
     }
    

    }

    void RobotInit() { // We need to run our vision program in a separate Thread. // If not, our robot program will not run std::thread visionThread(VisionThread); visionThread.detach(); }

};

START_ROBOT_CLASS(Robot)

Clone this wiki locally