OpenCV Python 中的等效 im2double 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29100722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:05:14  来源:igfitidea点击:

Equivalent im2double function in OpenCV Python

pythonimagematlabopencvimage-processing

提问by Naveen Raja

In MATLAB, the following code reads in an image and normalizes the values between [0.0,1.0]:

在 MATLAB 中,以下代码读入图像并对 之间的值进行归一化[0.0,1.0]

img=im2double(imread('image.jpg')) 

I would like to perform this in OpenCV Python. Is there an equivalent function to do this?

我想在 OpenCV Python 中执行此操作。是否有等效的功能来执行此操作?

I have tried the following code, but its asking for source IplImage. Also, what would be the equivalent to imreadin Python?

我尝试了以下代码,但它要求提供 source IplImage。另外,imread在 Python 中的等价物是什么?

def im2double(im):
    mat = cvGetMat(im);
    if CV_MAT_DEPTH(mat.type)==CV_64F:
       return mat
    im64f = array(size(im), 'double')
    cvConvertScale(im, im64f, 1.0, 0.0)
    return im64f

采纳答案by rayryeng

I would avoid using the old cvmodule and use cv2instead as these use numpyarrays. numpyarrays operate very similar to arrays and matrices in MATLAB.

我会避免使用旧cv模块,cv2而是使用它们,因为这些使用numpy数组。 numpy数组的操作与 MATLAB 中的数组和矩阵非常相似。

In any case, im2doublein MATLAB normalizes an image such that the minimum intensity is 0 and the maximum intensity is 1. You can achieve that by the following relationship, given a pixel infrom the image img:

在任何情况下, im2double在 MATLAB 中对图像进行归一化,使得最小强度为 0,最大强度为 1。您可以通过以下关系来实现,给定in图像中的一个像素img

out = (in - min(img)) / (max(img) - min(img))

Therefore, you would need to find the minimum and maximum of the image and apply the above operation to every pixel in the image. In the case of multi-channel images, we would find the globalminimum and maximum over all channels and apply the same operation to all channels independently.

因此,您需要找到图像的最小值和最大值,并将上述操作应用于图像中的每个像素。在多通道图像的情况下,我们会在所有通道上找到全局最小值和最大值,并对所有通道独立应用相同的操作。

The short answer to your question is to use cv2.normalizelike so:

对您问题的简短回答是这样使用cv2.normalize

out = cv2.normalize(img.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)

The first input is the source image, which we convert to float. The second input is the output image, but we'll set that to Noneas we want the function call to return that for us. The third and fourth parameters specify the minimum and maximum values you want to appear in the output, which is 0 and 1 respectively, and the last output specifies howyou want to normalize the image. What I described falls under the NORM_MINMAXflag.

第一个输入是源图像,我们将其转换为float。第二个输入是输出图像,但我们将其设置None为我们希望函数调用为我们返回它。第三个和第四个参数指定要出现在输出,分别为0和1的最小值和最大值,最后输出指定如何要标准化图像。我所描述的属于NORM_MINMAX旗帜之下。

Your other question is with regards to reading in an image. To read in an image with cv2, use cv2.imread. The input into this function is a string that contains the file you want to load in. Therefore, you'd call the above function like so:

你的另一个问题是关于阅读图像。要使用 读入图像cv2,请使用cv2.imread。此函数的输入是一个字符串,其中包含您要加载的文件。因此,您可以像这样调用上述函数:

img = cv2.imread('....') # Read image here
out = cv2.normalize(img.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX) # Convert to normalized floating point


However, if you'd like to write something yourself, we can very easily do that using numpyoperations.

然而,如果你想自己写一些东西,我们可以很容易地使用numpy操作来做到这一点。

As such, write your function like so:

因此,像这样编写你的函数:

import cv2
import numpy as np

def im2double(im):
    min_val = np.min(im.ravel())
    max_val = np.max(im.ravel())
    out = (im.astype('float') - min_val) / (max_val - min_val)
    return out

You'd then use the code like so:

然后你可以像这样使用代码:

img = cv2.imread('...') # Read in your image
out = im2double(img) # Convert to normalized floating point

Edit - September 29, 2016

编辑 - 2016 年 9 月 29 日

More recent versions of MATLAB now simply divide all of the numbers by the largest value supported by that datatype. For example, for uint8the largest value is 255 while for uint16the largest value is 65535.

最新版本的 MATLAB 现在只需将所有数字除以该数据类型支持的最大值。例如,uint8最大值为 255,uint16最大值为 65535。

If you wanted to reimplement this for more recent versions of MATLAB, you can use the numpy.iinfofunction to infer what the smallest and largest values of the datatype are and convert accordingly. Simply access the largest value and divide all elements in your image by this number. Make sure you convert the image to a floating-point representation first:

如果您想为更新版本的 MATLAB 重新实现它,您可以使用该numpy.iinfo函数来推断数据类型的最小值和最大值是什么,并进行相应的转换。只需访问最大值并将图像中的所有元素除以该数字即可。确保首先将图像转换为浮点表示:

import cv2
import numpy as np

def im2double(im):
    info = np.iinfo(im.dtype) # Get the data type of the input image
    return im.astype(np.float) / info.max # Divide all values by the largest possible value in the datatype