简体   繁体   English

如何使 `cv2.imshow` 输出与 `plt.imshow` 输出相同?

[英]How can I make the `cv2.imshow` output the same as the `plt.imshow` output?

How can I make the cv2.imshow output the same as the plt.imshow output?我怎样才能让cv2.imshow输出一样plt.imshow输出?

# loading image
img0 = cv2.imread("image.png")
# converting to gray scale
gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)

# remove noise
img = cv2.GaussianBlur(gray, (3, 3), 0)

# convolute with proper kernels
laplacian = cv2.Laplacian(img, cv2.CV_64F)
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)  # x
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)  # y
imgboth = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)

plt.imshow(imgboth, cmap='gray')
plt.show()

cv2.imshow("img", cv2.resize(imgboth, (960, 540)))
cv2.waitKey(0)
cv2.destroyAllWindows()
       

original image原图

在此处输入图片说明

plt.output

在此处输入图片说明

cv2.imshow

在此处输入图片说明

# ...
canvas = imgboth.astype(np.float32)
canvas /= np.abs(imgboth).max()
canvas += 0.5
cv.namedWindow("canvas", cv.WINDOW_NORMAL)
cv.imshow("canvas", canvas)
cv.waitKey()
cv.destroyWindow("canvas")

帆布

only looks different because you posted thumbnails, not the original size image.只是看起来不同,因为您发布了缩略图,而不是原始大小的图像。

when you give imshow floating point values, which you do because laplacian and sobelx are floating point, then it assumes a range of 0.0 .. 1.0 as black .. white.当你给出imshow浮点值时,你这样做是因为laplaciansobelx是浮点数,那么它假定范围为0.0 .. 1.0为黑色 .. 白色。

matplotlib automatically scales data. matplotlib 自动缩放数据。 OpenCV's imshow doesn't. OpenCV 的 imshow 没有。 both behaviors have pros and cons.这两种行为各有利弊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM