我们使用函数 cv2.normalize() 来规范化 OPenCV 中的图像。此函数接受参数 - src,dst,alpha,beta,norm_type,dtype和掩码。 SRC 和 DST 是输入图像和输出,与输入大小相同,alpha 是范围归一化的下限值,beta 是范围归一化的上限范数值,norm_type是归一化类型,dtype 是输出的数据类型,掩码是可选的操作掩码。
步骤
要规范化图像,我们可以按照以下步骤操作 -
- 导入所需的库。在以下所有示例中,所需的 Python 库是 OpenCV。确保您已经安装了它。
- 使用 cv2.imread() 方法将输入图像读取为灰度图像。使用图像类型(即.png或 jpg)指定图像的完整路径。
- 在输入图像 img 上应用 cv2.normalize() 函数。传递参数 src、dst、alpha、beta、norm_type、dtype 和掩码。
img_normalized = cv2.normalize(img, None, 0, 1.0, cv2.NORM_MINMAX, dtype=cv2.CV_32F)
- 显示归一化的输出图像。
- 打印归一化前后的图像数据。尝试找出这两个图像数据之间的差异。
让我们借助一些 Python 示例来理解这个问题。
在以下示例中,我们将使用此图像作为输入文件 -
例
在这个 Python 程序中,我们使用最小-最大范数对颜色输入图像进行归一化。图像像素值归一化为范围 [0,1]。
# import required library import cv2 # read the input image in grayscale img = cv2.imread('jeep.jpg',0) print("Image data before Normalize:\n", img) # Normalize the image img_normalized = cv2.normalize(img, None, 0, 1.0, cv2.NORM_MINMAX, dtype=cv2.CV_32F) # visualize the normalized image cv2.imshow('Normalized Image', img_normalized) cv2.waitKey(0) cv2.destroyAllWindows() print("Image data after Normalize:\n", img_normalized)
输出
当你运行上面的程序时,它将产生以下输出 -
Image data before Normalize:
[[ 37 37 37 ... 55 55 55]
[ 39 39 39 ... 57 56 56]
[ 39 39 39 ... 56 56 56]
...
[243 244 244 ... 82 85 86]
[242 245 245 ... 83 91 91]
[242 245 245 ... 86 94 93]]
Image data after Normalize:
[[0.14509805 0.14509805 0.14509805 ... 0.21568629 0.21568629 0.21568629]
[0.15294118 0.15294118 0.15294118 ... 0.22352943 0.21960786 0.21960786]
[0.15294118 0.15294118 0.15294118 ... 0.21960786 0.21960786 0.21960786]
...
[0.95294124 0.9568628 0.9568628 ... 0.32156864 0.33333334 0.3372549 ]
[0.9490197 0.9607844 0.9607844 ... 0.3254902 0.35686275 0.35686275]
[0.9490197 0.9607844 0.9607844 ... 0.3372549 0.36862746 0.3647059 ]]
我们得到以下窗口,显示规范化图像 -
例
在这个 Python 程序中,我们使用最小-最大范数规范化二进制输入图像。归一化后的图像像素值为 0 或 1。
# import required library import cv2 # read the input image as grayscale image img = cv2.imread('jeep.jpg',0) print("Image data before Normalize:\n", img) # Apply threshold to create a binary image ret,thresh = cv2.threshold(img,140,255,cv2.THRESH_BINARY) print("Image data after Thresholding:\n", thresh) # normalize the binary image img_normalized = cv2.normalize(thresh, None, 0, 1.0, cv2.NORM_MINMAX, dtype=cv2.CV_32F) # visualize the normalized image cv2.imshow('Normalized Image', img_normalized) cv2.waitKey(0) cv2.destroyAllWindows() print("Image data after Normalize:\n", img_normalized)
输出
当你运行上面的 Python 程序时,它会产生以下输出 -
Image data before Normalize:
[[ 37 37 37 ... 55 55 55]
[ 39 39 39 ... 57 56 56]
[ 39 39 39 ... 56 56 56]
...
[243 244 244 ... 82 85 86]
[242 245 245 ... 83 91 91]
[242 245 245 ... 86 94 93]]
Image data after Thresholding:
[[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
...
[255 255 255 ... 0 0 0]
[255 255 255 ... 0 0 0]
[255 255 255 ... 0 0 0]]
Image data after Normalize:
[[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
...
[1. 1. 1. ... 0. 0. 0.]
[1. 1. 1. ... 0. 0. 0.]
[1. 1. 1. ... 0. 0. 0.]]
我们得到以下窗口,显示规范化的二进制图像 -