[英]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
浮点值时,你这样做是因为laplacian
和sobelx
是浮点数,那么它假定范围为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.