Parag Jain
Parag Jain

Reputation: 662

How to convert any image to CV_32FC1 in Python

I have code in C++ for converting an image to "CV_32FC1" but I want to do same thing in Python. Here is my C++ code.

img.convertTo(img, CV_32FC1);

How can I do same thing in Python?

Upvotes: 2

Views: 5153

Answers (2)

ZdaR
ZdaR

Reputation: 22954

Python uses numpy to manage the image pixel data, you can do something like this in numpy to change the data type of a matrix:

img = img.astype("float32")

As added by @HansHirse in the comments, this operation will just change the data type of the matrix, but if you want to change the scale of pixel values then you should use img = img.astype("float32")/255, to convert all the pixel values in range 0.0 - 1.0

Upvotes: 4

Related Questions