Convert OpenCV Image to Scikit Image using Python

Convert OpenCV Image to Scikit Image using Python

There may be cases where you need to convert an OpenCV image to a Scikit image to take advantage of the image processing functions provided by scikit-image library. Thankfully, the conversion process is straightforward. 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 OpenCV. It uses BGR as its default color space, while scikit-image uses RGB. So, we convert the color space of the loaded image from BGR to RGB. However, there is also difference between data types. In the OpenCV, the image is stored in the unsigned 8-bit integer format, with values in [0, 255]. In the scikit-image, the image is stored in the floating-point format, with values in [0, 1]. So, we perform data type conversion. We convert the image from unsigned 8-bit integer format to a floating-point format. That's it, now image object can be used with functions provided by scikit-image library.

import cv2
from skimage.util import img_as_float

cvImg = cv2.imread('test.jpg')
cvImg = cv2.cvtColor(cvImg, cv2.COLOR_BGR2RGB)

skiImg = img_as_float(cvImg)

Leave a Comment

Cancel reply

Your email address will not be published.