Using WSL and OpenCV: A Laptop Project

Dufresne Danny
4 min readFeb 3, 2023

--

Let’s get straight into it, WLS is a great tool; Much like docker containers to run native OS on top or alongside of host OS.

Windows Subsystem for Linux (WSL) is a compatibility layer in Microsoft Windows that allows users to run Linux binaries natively on Windows 10 and Windows Server 2019. WSL enables users to run a full Linux user environment, including the ability to run Linux applications and tools, on a Windows machine without the need for a virtual machine or dual boot setup.

WSL provides a Linux-compatible kernel interface, which allows users to run native Linux command-line tools and utilities, as well as full Linux graphical applications, on Windows. The Linux environment runs in a separate lightweight virtual machine and integrates with the Windows file system, allowing users to access files and directories on both the Windows and Linux file systems.

Installing dependencies and WLS2 can be found in a previous publication.

https://medium.com/@dufresne.danny/access-usb-devices-via-wsl2-kali-linux-2dbd105cdfd9

The main bases to allow USB senor devices to be recognized within the WSL environment is the kernel being rebuilt from source.

The process of rebuilding the WSL kernel involves the following steps:

  • Obtain the Linux kernel source code: You can either download the latest stable release of the Linux kernel from kernel.org, or obtain the source code for the version of the Linux kernel currently used by your WSL environment.
sudo git clone https://github.com/microsoft/WSL2-Linux-Kernel.git
  • Install build dependencies: You will need to install various development tools and libraries in order to build the kernel, including the GCC compiler, make, and other packages. These packages can be installed using the package manager for your Linux distribution in the WSL environment.
sudo apt update && sudo apt upgrade -y && \
sudo apt install -y build-essential flex bison \
libgtk2.0-dev libelf-dev libncurses-dev autoconf \
libudev-dev libtool zip unzip v4l-utils libssl-dev \
python3-pip cmake git iputils-ping net-tools
  • Configure the kernel: Before building the kernel, you will need to configure the options and settings for the WSL environment. This is done using the ‘sudo make menuconfig’.
cd WSL2-Linux-Kernel
sudo cp /proc/config.gz config
sudo mv config .config
sudo make menuconfig
  • Build the kernel: after making recommended selections.
Device Drivers

[ ] Network device support -> [*] Network device support
[ ] USB Network Adapters -> [*] USB Network Adapters
[ ] Multi-purpose USB Networking Framework -> [*] Multi-purpose USB Networking Framework
-M- CDC Ethernet support (smart devices such as cable modems)
[ ] Host for RNDIS and ActiveSync devices -> [*] Host for RNDIS and ActiveSync devices
<ESC><ESC><ESC><ESC>

Device Drivers

[ ] Multimedia support -> [*] Multimedia support
[ ] Filter Media Drivers -> [*] Filter Media Drivers
[ ] Autoselect ancillary devices -> [*] Autoselect ancillary devices
Media Device Types -> [ ] Cameras/video grabbers support -> [*] Cameras/video grabbers support
[ ] Media USB Adapters -> [*] Media USB Adapters
Video4linux options -> [*] v4L2 sub-device userspace API
Media drivers
[*] Media USB Drivers ->
< > USB Video Class (UVC) -> <*> USB Video Class (UVC)
[*] UVC input events device support
[*] GSPCA based webcams
< Save > -> < Exit > -> < Exit > -> < Exit > -> < Exit >
  • Compiling the kernel will produce a vmlinux image that can then be loaded at startup.
sudo make -j$(nproc)
sudo make modules_install -j$(nproc)
sudo make install -j$(nproc)
  • Install the new kernel: After the build is complete, you will need to install the new kernel and its modules in the WSL environment. This involves copying the relevant files to the appropriate locations and updating the boot loader configuration.
//Copy vmlinux image to C drive
sudo cp vmlinux /mnt/c
vmlinux image
//create text file named .wslconfig, which points to vmlinux image.
C://Users/<user directory>
.wslconfig contents

Load an instance of powershell and issue the following commands to reload with new kernel.

wsl --shutdown
wsl

Install v4l-utils, guvcview, cheese to verify installation.

sudo apt-get install v4l2-utils
sudo apt-get install guvcview
sudo apt-get install cheese

To verify installation, use the following commands.

// you will need to re attach busid to linux distro

//verify new loaded kernal
uname -a
//verify webcam is detected
v4l2-ctl --list-devices
//change permissions of video modules
sudo chmod 777 /dev/video0
sudo chmod 777 /dev/video1
//verify file can be opened
v4l2-ctl -d /dev/video0 --all
//use guvcview to check webcam accessibility
guvcview -d /dev/video0 //note I did have to downscale resolution to 640X480 and FPS to 15/1 from 30/1

Thats pretty much it, you can use the following example code from Learning Opencv3(Authors: Adrian Kaehler, Gary Bradski) to test with.

Adjusted code to scale resolution to 320x260 and cap FPS to 30.

#include <opencv2/opencv.hpp>
#include <iostream>


using namespace cv;
int main( int argc, char** argv ){

cv::namedWindow( "Example 2-10",
cv::WINDOW_AUTOSIZE );
cv::VideoCapture cap;
if (argc==1) {
cap.open(0);
cap.set(CAP_PROP_FRAME_WIDTH, 320);
cap.set(CAP_PROP_FRAME_HEIGHT, 260);
cap.set(CAP_PROP_FPS, 30);
cap.set(CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
}else {
cap.open(argv[1]);
cap.set(CAP_PROP_FRAME_WIDTH, 320);
cap.set(CAP_PROP_FRAME_HEIGHT, 260);
cap.set(CAP_PROP_FPS, 30);
cap.set(CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
}
if( !cap.isOpened() ) {
std::cerr << "Couldn't open capture." << std::endl;
return -1;
}
cv::Mat frame;
for(;;) {
cap >> frame;
if( frame.empty() ) break;
cv::imshow( "Example 2-3", frame );
if( (char) cv::waitKey(5) >= 0 )break;
}
cap.release();
}

References( article written with the help and power of chatGPT)

Install v4l-utils on Debian-based distros | by Pete Houston | Medium

Advice on camera and microphone in WSL2 Ubuntu — Version 2 (version-2.com)

[Deprecated] Windows 10 + WSL2 + USB Camera + Serial (zenn.dev)

--

--

Dufresne Danny

Cloud computing Hobbyists, interest in data analytics, machine learning, programming. On a lighter note aquarium fish keeping, hiking, anime, and gaming.