Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - OpenCV mat::convertTo in python

Tags:

python

opencv

Is there any function in the OpenCV python wrapper that does the same thing as Mat's convertTo method in OpenCV 2?

I basically want to call this function in python

out.convertTo( out, CV_32F, 1.0/255, 0 ); 

where out is a grayscale image.

I have already made use of cv.ConvertScale by keeping my dst argument as type CV_32FC1, but I am trying to keep my python code as cv2 conformant as possible. Any clues?

like image 644
varagrawal Avatar asked Apr 23 '13 06:04

varagrawal


People also ask

What is a CV :: mat?

In OpenCV the main matrix class is called Mat and is contained in the OpenCV-namespace cv. This matrix is not templated but nevertheless can contain different data types. These are indicated by a certain type-number. Additionally, OpenCV provides a templated class called Mat_, which is derived from Mat.

How do you create a mat in Python?

Creates a matrix header and allocates the matrix data. Python: cv. CreateMat(rows, cols, type) → mat Parameters: rows – Number of rows in the matrix cols – Number of columns in the matrix type – The type of the matrix elements in the form CV_<bit depth><S|U|F>C<number of channels> , where S=signed, U=unsigned, F=float.

What is Mat image in OpenCV?

The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc. This class comprises of two data parts: the header and a pointer.

How do you define Imread in Python?

imread() method loads an image from the specified file. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format) then this method returns an empty matrix. Parameters: path: A string representing the path of the image to be read.


2 Answers

You can simply use Numpy functions for this.

eg :

res = np.float32(out) 

scaling, you will have to do separately:

res = res*scaling_factor 
like image 180
Abid Rahman K Avatar answered Sep 23 '22 08:09

Abid Rahman K


If you are not trying to convert the data type, use this:

cv2.convertScaleAbs(image, result, alpha, beta) 

where alpha is you scale factor and beta is a shift value. More details in the OpenCV docs.

like image 21
knipknap Avatar answered Sep 26 '22 08:09

knipknap