Home > Back-end >  What's the difference in printing an image to the screen using OpenCV and matplotlib?
What's the difference in printing an image to the screen using OpenCV and matplotlib?

Time:03-18

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

CodePudding user response:

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.

  • Related