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.
- C++
- Python
- C#
#include "VizionSDK.h"
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
import pyvizionsdk
from pyvizionsdk import VX_ISP_IMAGE_PROPERTIES, VX_IMAGE_FORMAT
import cv2
import numpy as np
using OpenCvSharp;
using VizionSDKSharp;
Start the device
Discover and initialize the device. Then, set the device format to the minimum resolution supported for either MJPG or UYVY format.
By default, MJPG is selected. You can choose UYVY if needed.
- C++
- Python
- C#
// initial the camera
int dev_idx = 0;
auto cam = VxInitialCameraDevice(dev_idx);
VxOpen(cam);
// set format to 640*480 for UYVY or MJPG format
VX_IMAGE_FORMAT selectFormat;
std::cout << "Choose a format: \n1. UYVY\n2. MJPG(Default)" << std::endl;
std::cout << "Please enter format(1 or 2): ";
int fmtId;
std::cin >> fmtId;
if (fmtId == 1) {
selectFormat = VX_IMAGE_FORMAT::UYVY;
} else {
selectFormat = VX_IMAGE_FORMAT::MJPG;
}
std::vector<VxFormat> fmt_list;
VxGetFormatList(cam, fmt_list);
int cap_width = 640;
int cap_height = 480;
VxFormat cap_fmt;
for (auto fmt : fmt_list) {
// find UYVY 640*480 format
if (fmt.format == selectFormat && fmt.width * fmt.height == cap_width * cap_height) {
cap_width = fmt.width;
cap_height = fmt.height;
cap_fmt = fmt;
}
}
if (VxSetFormat(cam, cap_fmt) == 0) {
std::cout << "Set Capture Format Success!" << std::endl;
} else {
std::cout << "Set Capture Format Failed!" << std::endl;
}
# initialize camera device
idx = 0
camera = pyvizionsdk.VxInitialCameraDevice(idx)
# open camera
result = pyvizionsdk.VxOpen(camera)
print("Open camera return code:", result)
print("Choose a format:\n1. UYVY\n2. MJPG(Default)")
fmt_id = int(input("Please enter format (1 or 2): ") or "1")
select_format = VX_IMAGE_FORMAT.VX_IMAGE_FORMAT_UYVY if fmt_id == 1 else VX_IMAGE_FORMAT.VX_IMAGE_FORMAT_MJPG
# get format
result, format_list = pyvizionsdk.VxGetFormatList(camera)
cap_format = None
min_width = 640
min_height = 480
for format in format_list:
if format.format == select_format:
resolution = format.width * format.height
if (format.width * format.height) == (min_width * min_height) :
cap_format = format
// initial the camera
int dev_idx = 0;
VxCamera cam = CSVizionSDK.VxInitialCameraDevice(dev_idx);
CSVizionSDK.VxOpen(cam);
Console.WriteLine("Choose a format:\n1. UYVY\n2. MJPG(Default)");
Console.Write("Please enter format (1 or 2): ");
int fmtId = int.Parse(Console.ReadLine() ?? "1");
VX_IMAGE_FORMAT selectFormat = (fmtId == 1)
? VX_IMAGE_FORMAT.UYVY
: VX_IMAGE_FORMAT.MJPG;
// Get available formats
VxFormatVector fmtList = new VxFormatVector();
CSVizionSDK.VxGetFormatList(cam, fmtList);
int minWidth = 640;
int minHeight = 480;
VxFormat imgFmt = new VxFormat();
foreach (var fmt in fmtList)
{
// Find minimum resolution in UYVY/MJPG format
if (fmt.format == selectFormat &&
(fmt.width * fmt.height == minWidth * minHeight))
{
minWidth = fmt.width;
minHeight = fmt.height;
imgFmt = fmt;
}
}
// Set capture format
if (CSVizionSDK.VxSetFormat(cam, imgFmt) == 0)
Console.WriteLine("Set Capture Format Success!");
else
Console.WriteLine("Set Capture Format Failed!");
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 image data, and display it in a window.
- C++
- Python
- C#
// 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;
if (selectFormat == VX_IMAGE_FORMAT::UYVY) {
matImg = cv::Mat(min_height, min_width, CV_8UC2, raw_data);
// convert from UYVY to BGR
cv::Mat bgrImg;
cv::cvtColor(matImg, bgrImg, cv::COLOR_YUV2BGR_UYVY);
cv::imshow("UYVY Image", bgrImg);
} else if (selectFormat == VX_IMAGE_FORMAT::MJPG) {
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);
# start streaming
result = pyvizionsdk.VxStartStreaming(camera)
# get the MJPG format image
result, image = pyvizionsdk.VxGetImage(camera, 1000, mjpg_format)
# retrieve the data to opencv and display with cv2.imshow()
if select_format == VX_IMAGE_FORMAT.VX_IMAGE_FORMAT_UYVY:
uyvy = nparr.reshape((min_height, min_width, 2))
bgr = cv2.cvtColor(uyvy, cv2.COLOR_YUV2BGR_UYVY)
cv2.imshow("UYVY Image", bgr)
elif select_format == VX_IMAGE_FORMAT.VX_IMAGE_FORMAT_MJPG:
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
cv2.imshow("MJPG Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# stop streaming
pyvizionsdk.VxStopStreaming(camera)
# close camera
pyvizionsdk.VxClose(camera)
// start streaming
CSVizionSDK.VxStartStreaming(cam);
// get the min format image
int dataSize;
byte[] buffer = new byte[minWidth * minHeight * 2];
VX_CAPTURE_RESULT result = CSVizionSDK.VxGetImage(cam, buffer, out dataSize, 3000);
if (selectFormat == VX_IMAGE_FORMAT.UYVY)
{
// Create Mat from raw buffer
using var matImg = new Mat(minHeight, minWidth, MatType.CV_8UC2);
// Copy byte array into Mat
Marshal.Copy(buffer, 0, matImg.Data, buffer.Length);
// Convert from UYVY to BGR
Mat bgrImg = new Mat();
Cv2.CvtColor(matImg, bgrImg, ColorConversionCodes.YUV2BGR_UYVY);
Cv2.ImShow("UYVY Image", bgrImg);
}
else if (selectFormat == VX_IMAGE_FORMAT.MJPG)
{
// Create Mat from raw buffer
using var matImg = new Mat(1, buffer.Length, MatType.CV_8UC1);
// Copy byte array into Mat
Marshal.Copy(buffer, 0, matImg.Data, buffer.Length);
using var decoded = Cv2.ImDecode(matImg, ImreadModes.Color);
Cv2.ImShow("MJPG Image", decoded);
}
CSVizionSDK.VxStopStreaming(cam);
CSVizionSDK.VxClose(cam);
OpenCV VideoCapture with VizionSDK
Adjust the image properties using VizionSDK and capture frames with OpenCV's VideoCapture.
This example demonstrates how to open a video window displaying a live camera stream. While the stream is active, you can press keys to increase or decrease the brightness.
-
Initialize VideoCapture with opencv:
- C++
- Python
- C#
// Capture image with opencv VideoCapture API
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);# import the platform library for video capture backend selection with cross-platform compatibility
import platform
# Capture image with opencv VideoCapture API
if platform.system() == 'Windows':
cap = cv2.VideoCapture(idx, cv2.CAP_DSHOW)
else:
cap = cv2.VideoCapture(idx)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, min_width)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, min_height)// Capture image with opencv VideoCapture API
using var cap = new VideoCapture(devIdx, VideoCaptureAPIs.DSHOW);
cap.Set(VideoCaptureProperties.FrameWidth, minWidth);
cap.Set(VideoCaptureProperties.FrameHeight, minHeight); -
Retrieve the current brightness value:
The code below shows how to handle brightness, and you can follow the same structure for other properties, such as:
-
ISP_EHDR_EXPOSURE_MAX_TIME
-
ISP_IMAGE_EXPOSURE_TIME
-
ISP_IMAGE_GAIN
For more image property examples (e.g., eHDR), refer to the full example here
- C++
- Python
- C#
// Get the brightness range
int min_brightness, max_brightness, step, def;
VxGetISPImageProcessingRange(cam, VX_ISP_IMAGE_PROPERTIES::ISP_IMAGE_BRIGHTNESS, min_brightness,
max_brightness, step, def);
std::cout << "Brightness min: " << min_brightness << ", max: " << max_brightness
<< ", step: " << step << ", default: " << def << std::endl;
// Get the brightness
int brightness, flag;
VxGetISPImageProcessing(cam, VX_ISP_IMAGE_PROPERTIES::ISP_IMAGE_BRIGHTNESS, brightness, flag);
std::cout << "Intial ISP brightness: " << brightness << ", flag: " << flag << std::endl;# Get the brightness range
result, min_brightness, max_brightness, step, def_brightness = pyvizionsdk.VxGetISPImageProcessingRange(camera, VX_ISP_IMAGE_PROPERTIES.ISP_IMAGE_BRIGHTNESS)
print(f"Brightness min: {min_brightness}, max: {max_brightness}, step: {step}, default: {def_brightness}, return code: {result}")
# Get the brightness
result, brightness, flag = pyvizionsdk.VxGetISPImageProcessing(camera, VX_ISP_IMAGE_PROPERTIES.ISP_IMAGE_BRIGHTNESS)
print(f"Initial ISP brightness: {brightness}, flag: {flag}, return code: {result}")// Get the brightness range
int minBrightness, maxBrightness, step, def;
CSVizionSDK.VxGetISPImageProcessingRange(cam,
VX_ISP_IMAGE_PROPERTIES.ISP_IMAGE_BRIGHTNESS,
out minBrightness, out maxBrightness, out step, out def);
Console.WriteLine($"Brightness min: {minBrightness}, max: {maxBrightness}, step: {step}, default: {def}");
// Get the brightness
int brightness, flag;
CSVizionSDK.VxGetISPImageProcessing(cam,
VX_ISP_IMAGE_PROPERTIES.ISP_IMAGE_BRIGHTNESS,
out brightness, out flag); -
-
Capture and display video in a window. Use the following keys during playback:
- u: Increase brightness
- d: Decrease brightness
- q: Stop playing video and exit the program.
The draw_label helper function (included in the sample code) is used to overlay text labels directly onto the video display window.
- C++
- Python
- C#
cv::Mat frame;
while (true) {
bool ret = cap.read(frame);
if (ret) {
std::string brightness_label = "brightness: " + std::to_string(brightness);
std::string label_str = "u: increase, d: decrease, q: exit";
draw_label(frame, brightness_label, cv::Point(20, 20));
draw_label(frame, label_str, cv::Point(20, 40));
cv::imshow("Adjust brightness", frame);
int key = cv::waitKey(1) & 0xFF;
if (key == 'u') {
if (brightness < max_brightness) {
brightness = brightness + step;
}
VxSetISPImageProcessing(cam, VX_ISP_IMAGE_PROPERTIES::ISP_IMAGE_BRIGHTNESS,
brightness);
std::cout << "Set brightness to: " << brightness << std::endl;
}
if (key == 'd') {
if (brightness > min_brightness) {
brightness = brightness - step;
}
VxSetISPImageProcessing(cam, VX_ISP_IMAGE_PROPERTIES::ISP_IMAGE_BRIGHTNESS,
brightness);
std::cout << "Set brightness to: " << brightness << std::endl;
}
if (key == 'q') {
break;
}
} else {
std::cout << "Can't receive frame (stream end?). Exiting ..." << std::endl;
break;
}
}# Capture video and display
while True:
ret, frame = cap.read()
if ret:
__draw_label(frame, f"brightness: {brightness}", (20,20))
__draw_label(frame, "u: increase, d: decrease, q: exit", (20,40))
cv2.imshow(f"Adjust brightness", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('u'):
if brightness < max_brightness:
brightness = brightness + step
result = pyvizionsdk.VxSetISPImageProcessing(camera, VX_ISP_IMAGE_PROPERTIES.ISP_IMAGE_BRIGHTNESS, brightness)
print(f"Set brightness to: {brightness}")
if key == ord('d'):
if brightness > min_brightness:
brightness = brightness - step
result = pyvizionsdk.VxSetISPImageProcessing(camera, VX_ISP_IMAGE_PROPERTIES.ISP_IMAGE_BRIGHTNESS, brightness)
print(f"Set brightness to: {brightness}")
if key == ord('q'):
break
else:
print("Can't receive frame (stream end?). Exiting ...")
breakusing var window = new Window("Adjust brightness");
Mat frame = new Mat();
while (true)
{
if (!cap.Read(frame) || frame.Empty())
{
Console.WriteLine("Can't receive frame (stream end?). Exiting...");
break;
}
// Draw overlay labels
DrawLabel(frame, $"Brightness: {brightness}", new Point(20, 20));
DrawLabel(frame, "u: increase, d: decrease, q: exit", new Point(20, 40));
window.ShowImage(frame);
int key = Cv2.WaitKey(1) & 0xFF;
if (key == 'u')
{
if (brightness < maxBrightness)
brightness += step;
CSVizionSDK.VxSetISPImageProcessing(cam,
VX_ISP_IMAGE_PROPERTIES.ISP_IMAGE_BRIGHTNESS,
brightness);
Console.WriteLine($"Set brightness to: {brightness}");
}
else if (key == 'd')
{
if (brightness > minBrightness)
brightness -= step;
CSVizionSDK.VxSetISPImageProcessing(cam,
VX_ISP_IMAGE_PROPERTIES.ISP_IMAGE_BRIGHTNESS,
brightness);
Console.WriteLine($"Set brightness to: {brightness}");
}
else if (key == 'q')
{
break;
}
}