且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

OpenCV Python中的等效im2double函数

更新时间:2021-08-22 01:51:42

我会避免使用旧的 cv 模块,然后使用 cv2 ,因为这些使用 numpy 数组。 numpy 数组与MATLAB中的数组和矩阵非常相似。

I would avoid using the old cv module and use cv2 instead as these use numpy arrays. numpy arrays operate very similar to arrays and matrices in MATLAB.

无论如何, im2double MATLAB规范化图像,使最小强度为0,最大强度为1.您可以通过以下关系实现这一点,给定图像 img 中的像素

In any case, im2double in 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 in from the image 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 global minimum and maximum over all channels and apply the same operation to all channels independently.

对你的问题的简短回答是使用 cv2.normalize ,如下所示:

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

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

第一个输入是源图像,我们将其转换为 float 。第二个输入是输出图像,但我们将其设置为,因为我们希望函数调用为我们返回。第三个和第四个参数指定要在输出中显示的最小值和最大值,分别为0和1,最后一个输出指定要对图像进行标准化的 。我所描述的内容属于 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 None as 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 how you want to normalize the image. What I described falls under the NORM_MINMAX flag.

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

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:

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






但是,如果你我想自己写点东西,我们可以很容易地使用 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



编辑 - 2016年9月29日



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

Edit - September 29, 2016

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

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

If you wanted to reimplement this for more recent versions of MATLAB, you can use the numpy.iinfo function 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:

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