使用 OpenCV 和 matplotlib 将图像打印到屏幕上有什么区别?
当使用各自的 imshow 函数显示图像时,OpenCV 和 matplotlib 有时会给出相同的输出,有时会给出不同的输出。它们之间有什么区别?
例如,它为以下代码提供相同的输出:
img = cv2.imread("sudoku.jpg")
plt.figure()
plt.imshow(img, cmap="gray")
plt.axis("off")
plt.title("orjinal")
plt.show()
cv2.imshow("orjinal",img)
matplotlib: 开放式CV:
它为此代码提供了不同的输出:
laplacian = cv2.Laplacian(img,ddepth=cv2.CV_16S)
plt.figure()
plt.imshow(laplacian, cmap="gray")
plt.axis("off")
plt.title("laplacian")
plt.show()
cv2.imshow("laplacian",laplacian)
matplotlib: 开放式CV:
OpenCV and matplotlib sometimes give the same output, sometimes they give different output, when displaying an image with their respective imshow
functions. What is the difference between them?
For example it gives the same output for this code:
img = cv2.imread("sudoku.jpg")
plt.figure()
plt.imshow(img, cmap="gray")
plt.axis("off")
plt.title("orjinal")
plt.show()
cv2.imshow("orjinal",img)
matplotlib:
OpenCV:
It gives different output for this code:
laplacian = cv2.Laplacian(img,ddepth=cv2.CV_16S)
plt.figure()
plt.imshow(laplacian, cmap="gray")
plt.axis("off")
plt.title("laplacian")
plt.show()
cv2.imshow("laplacian",laplacian)
matplotlib:
OpenCV:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
主要区别(也是导致您显示的差异的原因)是,默认情况下,matplotlib 会缩放图像,使其最小值为黑色,最大值为白色。 OpenCV 始终根据图像的数据类型显示图像,例如对于 uint8 图像,0 是黑色,255 是白色。在您的例子中,您有一个签名的 16 位图像,其中 -32768 为黑色,32767 为白色。您的像素值占据的范围要小得多,因此全部显示为相同的中灰色。
The main difference, and the one that causes the difference you showed, is that matplotlib will, by default, scale the image so that its minimum value is black and its maximum value is white. OpenCV always shows images according to their data type, for example for a uint8 image, 0 is black and 255 is white. In your case, you have a signed 16-bit image, which has -32768 as black and 32767 as white. Your pixel values occupy a way smaller range, and so all appear as the same middle-gray color.