使用 OpenCV 和 matplotlib 将图像打印到屏幕上有什么区别?

发布于 2025-01-15 00:22:54 字数 802 浏览 1 评论 0原文

当使用各自的 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:
enter image description here
OpenCV:
enter image description here

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:
enter image description here
OpenCV:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的影子我的梦 2025-01-22 00:22:54

主要区别(也是导致您显示的差异的原因)是,默认情况下,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文