Skip to main content

OpenCV with VizionSDK

Introduction

This article will introduce how to display with OpenCV and control the camera using VizionSDK.
You can download the Sample Code here.

OpenCV Version

These examples use OpenCV version 4.9 or newer.

Load OpenCV and VizionSDK

Import the OpenCV and VizionSDK libraries to support the following examples.

#include "VizionSDK.h"
#include <opencv2/highgui.hpp>

Start the device

Discover the device and initial it. Then, set the device format to the minimum size for MJPG format.

// initial the camera
int dev_idx = 0;
auto cam = VxInitialCameraDevice(dev_idx);
VxOpen(cam);

// set format to min size for mjpg format
std::vector<VxFormat> fmt_list;
VxGetFormatList(cam, fmt_list);
int min_width = 1920;
int min_height = 1080;
VxFormat min_fmt;
for (auto fmt : fmt_list) {
// find MJPG smallest size format
if (fmt.format == VX_IMAGE_FORMAT::MJPG &&
fmt.width * fmt.height < min_width * min_height) {
min_width = fmt.width;
min_height = fmt.height;
min_fmt = fmt;
}
}

if (VxSetFormat(cam, min_fmt) == 0) {
std::cout << "Set Capture Format Success!" << std::endl;
} else {
std::cout << "Set Capture Format Failed!" << std::endl;
}

Display the image

Use VizionSDK VxGetImage function to retrieve the image data and convert it into OpenCV mat to display in the window.

This example demonstrates how to retrieve the format, set it to a specific format, and display the image in a window.

// start streaming
VxStartStreaming(cam);

// get the min format image
uint8_t* raw_data = new uint8_t[3840 * 2160 * 2];
int raw_size = 0;
VxGetImage(cam, raw_data, &raw_size, 2500);

// retrieve the data into the mat array and display with cv2.imshow()
cv::Mat matImg;
matImg = cv::imdecode(cv::Mat(1, raw_size, CV_8UC1, raw_data), cv::IMREAD_COLOR);
cv::imshow("MJPG Image", matImg);
cv::waitKey(0);
cv::destroyAllWindows();

VxStopStreaming(cam);
VxClose(cam);

OpenCV VideoCapture with VizionSDK

Adjust the image properties by VizionSDK and capture with OpenCV VideoCapture.

This example will show how to set the brightness and capture with the frame displayed in the window.

  1. Capture the image before adjust brightness

    // capture the video and display with cv2.imshow()
    cv::VideoCapture cap;
    #ifdef _WIN32
    cap = cv::VideoCapture(dev_idx, cv::CAP_DSHOW);
    #else
    cap = cv::VideoCapture(dev_idx);
    #endif
    cap.set(cv::CAP_PROP_FRAME_WIDTH, min_width);
    cap.set(cv::CAP_PROP_FRAME_HEIGHT, min_height);
    cv::Mat frameBefore;
    cap >> frameBefore;
    cv::imshow("Before setting", frameBefore);
    cv::waitKey(0);
    cv::destroyAllWindows();
    cap.release();
  2. Adjust brightness to 16

    // get the brightness
    long value;
    int flag;
    VxGetUVCImageProcessing(cam, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_BRIGHTNESS, value, flag);
    std::cout << "UVC brightness: " << value << std::endl;

    // set the brightness
    int result;
    result = VxSetUVCImageProcessing(cam, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_BRIGHTNESS, 16, flag);
    std::cout << "Set UVC brightness return code: " << result << std::endl;

    // get the brightness
    VxGetUVCImageProcessing(cam, VX_UVC_IMAGE_PROPERTIES::UVC_IMAGE_BRIGHTNESS, value, flag);
    std::cout << "After setting UVC brightness: " << value << std::endl;
  3. Capture the image after adjusting brightness

    // capture the video after setting the property and display with cv2.imshow()
    #ifdef _WIN32
    cap = cv::VideoCapture(dev_idx, cv::CAP_DSHOW);
    #else
    cap = cv::VideoCapture(dev_idx);
    #endif
    cap.set(cv::CAP_PROP_FRAME_WIDTH, min_width);
    cap.set(cv::CAP_PROP_FRAME_HEIGHT, min_height);
    cv::Mat frameAfter;
    cap >> frameAfter;
    cv::imshow("After setting", frameAfter);
    cv::waitKey(0);
    cv::destroyAllWindows();
    cap.release();

    VxClose(cam);