Convert Scikit Image to OpenCV Image using Python

Convert Scikit Image to OpenCV Image using Python

In some cases, it may be necessary to convert Scikit image to an OpenCV image because OpenCV provides a wider range of image processing functions and computer vision algorithms compared to scikit-image library. Fortunately, the conversion process is pretty simple. This tutorial shows how to do that using Python.

Prepare environment

  • Install the following packages using pip:
pip install scikit-image
pip install opencv-python

Code

In the following code, we load the image using the function provided by scikit-image library. In scikit-image and OpenCV, image is represented in NumPy array. However, there is difference between data types. In the scikit-image, the image is stored in the floating-point format, with values in [0, 1]. In the OpenCV, the image is stored in the unsigned 8-bit integer format, with values in [0, 255]. So, we do data type conversion. The image stored in the floating-point format is converted to an unsigned 8-bit integer format that is compatible with OpenCV. Also, there is difference between color space. OpenCV uses BGR as its default color space, while scikit-image uses RGB. So, we convert the color space of the loaded image from RGB to BGR. That's it, now OpenCV image object can be used for further image processing tasks.

import cv2
from skimage.util import img_as_ubyte
from skimage import io

skiImg = io.imread('test.jpg')
arrImg = img_as_ubyte(skiImg)

cvImg = cv2.cvtColor(arrImg, cv2.COLOR_RGB2BGR)

Leave a Comment

Cancel reply

Your email address will not be published.