如何将cv2图像转换为skimage?
How to convert cv2 image to skimage?
我正在从相机读取 cv2.COLOR_RGB2BGR
格式的图像。以下是我要实现的临时解决方法:
import cv2
from skimage import transform, io
...
_, img = cam.read()
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imwrite("temp.png", img)
img = io.imread("temp.png", as_gray=True)
img = transform.resize(img, (320, 240), mode='symmetric', preserve_range=True)
我找到了一种从 进行这种转换的方法,但是,图像数据似乎与我从路径读取相同图像时不一样?
我还从 this documentation 中发现我可以使用 img_as_float(cv2_img)
,但此转换产生的结果与 io.imread("temp.png", as_gray=True)
返回的结果不同
有效进行此转换的正确方法是什么?我应该先将图像转换回 RGB 然后使用 img_as_float()?
我想,您遇到的基本问题是 OpenCV 和 scikit-image 使用的不同 luma 计算:
- OpenCV 使用:
Y = 0.299 * R + 0.587 * G + 0.114 * B
- scikit-image 使用:
Y = 0.2125 * R + 0.7154 * G + 0.0721 * B
让我们进行一些测试——使用下图为例:
import cv2
import numpy as np
from skimage import io
# Assuming we have some kind of "OpenCV image", i.e. BGR color ordering
cv2_bgr = cv2.imread('paddington.png')
# Convert to grayscale
cv2_gray = cv2.cvtColor(cv2_bgr, cv2.COLOR_BGR2GRAY)
# Save BGR image
cv2.imwrite('cv2_bgr.png', cv2_bgr)
# Save grayscale image
cv2.imwrite('cv2_gray.png', cv2_gray)
# Convert to grayscale with custom luma
cv2_custom_luma = np.uint8(0.2125 * cv2_bgr[..., 2] + 0.7154 * cv2_bgr[..., 1] + 0.0721 * cv2_bgr[..., 0])
# Load BGR saved image using scikit-image with as_gray; becomes np.float64
sc_bgr_w = io.imread('cv2_bgr.png', as_gray=True)
# Load grayscale saved image using scikit-image without as_gray; remains np.uint8
sc_gray_wo = io.imread('cv2_gray.png')
# Load grayscale saved image using scikit-image with as_gray; remains np.uint8
sc_gray_w = io.imread('cv2_gray.png', as_gray=True)
# OpenCV grayscale = scikit-image grayscale loaded image without as_gray? Yes.
print('Pixel mismatches:', cv2.countNonZero(cv2.absdiff(cv2_gray, sc_gray_wo)))
# Pixel mismatches: 0
# OpenCV grayscale = scikit-image grayscale loaded image with as_gray? Yes.
print('Pixel mismatches:', cv2.countNonZero(cv2.absdiff(cv2_gray, sc_gray_w)))
# Pixel mismatches: 0
# OpenCV grayscale = scikit-image BGR loaded (and scaled) image with as_gray? No.
print('Pixel mismatches:', cv2.countNonZero(cv2.absdiff(cv2_gray, np.uint8(sc_bgr_w * 255))))
# Pixel mismatches: 131244
# OpenCV grayscale with custom luma = scikit-image BGR loaded (and scaled) image with as_gray? Almost.
print('Pixel mismatches:', cv2.countNonZero(cv2.absdiff(cv2_custom_luma, np.uint8(sc_bgr_w * 255))))
# Pixel mismatches: 1
你看:
- 当打开灰度图像时,scikit-image 只使用
np.uint8
值,无论是否使用 as_gray=True
。
- 当用
as_gray=True
打开彩色图像时,scikit-image 应用 rgb2gray
,将所有值缩放到 0.0 ... 1.0
,因此使用 np.float64
。由于不同的亮度计算,即使缩小到 0 ... 255
和 np.uint8
也会在该图像和 OpenCV 灰度图像之间产生很多像素不匹配。
- 根据
rgb2gray
手动计算亮度时,OpenCV灰度图像几乎完全相同。一个像素不匹配可能是由于浮点不准确。
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.9.1
NumPy: 1.20.1
OpenCV: 4.5.1
scikit-image: 0.18.1
----------------------------------------
我正在从相机读取 cv2.COLOR_RGB2BGR
格式的图像。以下是我要实现的临时解决方法:
import cv2
from skimage import transform, io
...
_, img = cam.read()
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imwrite("temp.png", img)
img = io.imread("temp.png", as_gray=True)
img = transform.resize(img, (320, 240), mode='symmetric', preserve_range=True)
我找到了一种从
我还从 this documentation 中发现我可以使用 img_as_float(cv2_img)
,但此转换产生的结果与 io.imread("temp.png", as_gray=True)
有效进行此转换的正确方法是什么?我应该先将图像转换回 RGB 然后使用 img_as_float()?
我想,您遇到的基本问题是 OpenCV 和 scikit-image 使用的不同 luma 计算:
- OpenCV 使用:
Y = 0.299 * R + 0.587 * G + 0.114 * B
- scikit-image 使用:
Y = 0.2125 * R + 0.7154 * G + 0.0721 * B
让我们进行一些测试——使用下图为例:
import cv2
import numpy as np
from skimage import io
# Assuming we have some kind of "OpenCV image", i.e. BGR color ordering
cv2_bgr = cv2.imread('paddington.png')
# Convert to grayscale
cv2_gray = cv2.cvtColor(cv2_bgr, cv2.COLOR_BGR2GRAY)
# Save BGR image
cv2.imwrite('cv2_bgr.png', cv2_bgr)
# Save grayscale image
cv2.imwrite('cv2_gray.png', cv2_gray)
# Convert to grayscale with custom luma
cv2_custom_luma = np.uint8(0.2125 * cv2_bgr[..., 2] + 0.7154 * cv2_bgr[..., 1] + 0.0721 * cv2_bgr[..., 0])
# Load BGR saved image using scikit-image with as_gray; becomes np.float64
sc_bgr_w = io.imread('cv2_bgr.png', as_gray=True)
# Load grayscale saved image using scikit-image without as_gray; remains np.uint8
sc_gray_wo = io.imread('cv2_gray.png')
# Load grayscale saved image using scikit-image with as_gray; remains np.uint8
sc_gray_w = io.imread('cv2_gray.png', as_gray=True)
# OpenCV grayscale = scikit-image grayscale loaded image without as_gray? Yes.
print('Pixel mismatches:', cv2.countNonZero(cv2.absdiff(cv2_gray, sc_gray_wo)))
# Pixel mismatches: 0
# OpenCV grayscale = scikit-image grayscale loaded image with as_gray? Yes.
print('Pixel mismatches:', cv2.countNonZero(cv2.absdiff(cv2_gray, sc_gray_w)))
# Pixel mismatches: 0
# OpenCV grayscale = scikit-image BGR loaded (and scaled) image with as_gray? No.
print('Pixel mismatches:', cv2.countNonZero(cv2.absdiff(cv2_gray, np.uint8(sc_bgr_w * 255))))
# Pixel mismatches: 131244
# OpenCV grayscale with custom luma = scikit-image BGR loaded (and scaled) image with as_gray? Almost.
print('Pixel mismatches:', cv2.countNonZero(cv2.absdiff(cv2_custom_luma, np.uint8(sc_bgr_w * 255))))
# Pixel mismatches: 1
你看:
- 当打开灰度图像时,scikit-image 只使用
np.uint8
值,无论是否使用as_gray=True
。 - 当用
as_gray=True
打开彩色图像时,scikit-image 应用rgb2gray
,将所有值缩放到0.0 ... 1.0
,因此使用np.float64
。由于不同的亮度计算,即使缩小到0 ... 255
和np.uint8
也会在该图像和 OpenCV 灰度图像之间产生很多像素不匹配。 - 根据
rgb2gray
手动计算亮度时,OpenCV灰度图像几乎完全相同。一个像素不匹配可能是由于浮点不准确。
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.9.1
NumPy: 1.20.1
OpenCV: 4.5.1
scikit-image: 0.18.1
----------------------------------------