我目前正在进行一个项目,将车牌从图像中分离出来。
当我使用 cv2.imread("filename",0)
导入图像时,我获得的灰度图像或多或少是相同的(由于我转换的事实,可能有一些舍入差异它到整数。)当我使用 cv2.imread("filename")
导入它然后使用 np.dot(original_image[...,:3], [0.299, 0.587, 0.144])
.
但是,当我将 ndarrays
与 255
相乘时,我没有获得相同的输出矩阵。两个灰度图像具有相同的尺寸,当我将它们转换为图形时产生相同的输出,具有相同的类型并产生相同的 otsu
阈值。为什么会这样? OpenCV 是否以不同方式显示和保存图像 ndarrays
?
在将第二张灰度图像乘以 255 后,如何操作它以产生与第一张灰度图像相同的输出?
def func():
rgb_image=cv2.imread('filename')
gray_image=cv2.imread('filename',0)
rgb_converted_to_gray_image=np.dot(rgb_image[...,:3], [0.299, 0.587, 0.144])
print("Before multiplying with 255")
print(gray_image)
print("------------")
print(rgb_converted_to_gray_image)
gray_image=gray_image*255
rgb_converted_to_gray_image=rgb_converted_to_gray_image*255
print("After multiplying with 255")
print(gray_image)
print("------------")
print(rgb_converted_to_gray_image)
输出如下:
乘以255之前
[[32 29 34 ... 92 88 86]
[33 28 32 ... 85 85 86]
[35 29 28 ... 85 93 99]
...
[ 8 8 8 ... 32 32 32]
[ 8 8 8 ... 32 32 32]
[ 8 8 8 ... 33 33 33]]
------------
[[ 27.512 24.721 29.129 ... 105.014 100.894 98.989]
[ 29.14 23.99 27.069 ... 97.804 97.804 99.432]
[ 30.912 25.02 23.547 ... 98.701 106.797 112.977]
...
[ 9.292 9.292 9.292 ... 33.558 33.558 33.558]
[ 9.292 9.292 9.292 ... 33.558 33.558 33.558]
[ 9.292 9.292 9.292 ... 34.588 34.588 34.588]]
乘以 255 后:
[[224 227 222 ... 164 168 170]
[223 228 224 ... 171 171 170]
[221 227 228 ... 171 163 157]
...
[248 248 248 ... 224 224 224]
[248 248 248 ... 224 224 224]
[248 248 248 ... 223 223 223]]
------------
[[ 7015.56 6303.855 7427.895 ... 26778.57 25727.97 25242.195]
[ 7430.7 6117.45 6902.595 ... 24940.02 24940.02 25355.16 ]
[ 7882.56 6380.1 6004.485 ... 25168.755 27233.235 28809.135]
...
[ 2369.46 2369.46 2369.46 ... 8557.29 8557.29 8557.29 ]
[ 2369.46 2369.46 2369.46 ... 8557.29 8557.29 8557.29 ]
[ 2369.46 2369.46 2369.46 ... 8819.94 8819.94 8819.94 ]]69.46 ... 8819.94 8819.94 8819.94 ]]
因此,我希望最后一个矩阵看起来与上面的矩阵相同。
最佳答案
观察到结果差异的原因有两个。
- 数据类型的差异
- channel 顺序
正如@Cris Luengo 在评论中指出的,第一个原因是 gray_image
和 rgb_converted_to_gray_image
之间的数据类型差异。 gray_image
的类型为 uint8
而 rgb_converted_to_gray
的类型为 float32
。作为乘以 255 的结果,gray_image
的值被缩放到 uint8
的范围内。要解决此问题,只需将 255
更改为 255.0
即可进行浮点乘法。
gray_image = gray_image * 255.0
现在是第二期。即使我们进行浮点乘法,结果也会不同,因为 OpenCV 图像默认按 channel 顺序 BGR
存储,而您以 RGB
提供灰度转换系数> 订单。此外,蓝色值的系数不正确。它应该是 0.114
而不是 0.144
。为了验证RGB系数值的逻辑正确性,检查它们的和应该等于1。校正后的系数数组应该是这样的:
[0.114, 0.587, 0.299]
最终代码可能是这样的:
def func():
rgb_image=cv2.imread('filename')
gray_image=cv2.imread('filename',0)
rgb_converted_to_gray_image=np.dot(rgb_image[...,:3], [0.114, 0.587, 0.299])
print("Before multiplying with 255")
print(gray_image)
print("------------")
print(rgb_converted_to_gray_image)
gray_image=gray_image*255.0
rgb_converted_to_gray_image=rgb_converted_to_gray_image*255
print("After multiplying with 255")
print(gray_image)
print("------------")
print(rgb_converted_to_gray_image)
关于python - 将图像作为灰度导入并将其转换为灰度在乘以 255 时不会产生相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56713228/