An alternative to read image from scipy to cv2

Viewed 973

the following python script I want to convert using opencv python, how do I make it converted?

script: scipy.misc.imread(path, mode='RGB').astype(np.float)

I want to convert it using cv2 and what would be alternative for astype(np.float) with this?

import cv2
import scipy.misc

img = scipy.misc.imread(path, mode='RGB').astype(np.float)
2 Answers

You may use cv2.imread, convert color format from BGR to RGB, and convert to float:

path = 'chelsea.png'

ref_img = scipy.misc.imread(path, mode='RGB').astype(np.float)

img = cv2.imread(path)  # Read input imgae
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # Convert color format form BGR to RGB (OpenCV default is BGR).
img = img.astype(np.float)  # Convert ot float

print(np.all(img == ref_img))

This should do the trick:

img = cv2.imread(path, -1).astype(np.float)
Related