Files
J2S1-Kobuki/docs/code/OpenCV.md

1.8 KiB

OpenCV

Requirements

We want that the camera we want it to detect what is happening on the video feed and identify it so it can identify dangers.

Issues

Installation

Dependencies

  • glew (for openGL)
  • opencv C++ lib

How to install OpenCV

 sudo apt-get install libopencv-dev

Code explanation

Opening the camera with OpenCV

    VideoCapture cap(0);     //Open the default camera (0), points to /dev/video0. You could also change the number to the preferred camera
    if (!cap.isOpened()) { //if camera is not opened throw a error message
        cerr << "Error: Could not open camera" << endl;
        return;
    }

Taking a picture and storing it in a variable

    Mat frame; //create a new Matrix variable called frame
    while (true) {
        cap >> frame; // Capture a new image frame. 
        if (frame.empty()) { //if the variable frame is not filled return a error
            cerr << "Error: Could not capture image" << endl;
            continue;
        }

Encoding the image for sending it over MQTT

        vector<uchar> buf; //create a dyanmic buffer for the image 
        imencode(".jpg", frame, buf); //encode the image to the buffer
        auto* enc_msg = reinterpret_cast<unsigned char*>(buf.data());

void CapnSend() {




        // Convert the image to a byte array


        // Publish the image data
        client.publishMessage("kobuki/cam", string(enc_msg, enc_msg + buf.size()));
        cout << "Sent image" << endl;

        std::this_thread::sleep_for(std::chrono::milliseconds(300)); // Send image every 1000ms
    }
}

Sources