在imsh中用黑光束分离两张图片

2024-05-16 09:35:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想显示两个图像,并用Python中的黑色光束直观地将它们分开。 我的问题是,我不能用Plot窗口中的cv2.imshow()函数获得原始颜色。 这是我的密码:

import cv2
import numpy as np

imgloc = 'path\Dosen_py.png'
img = cv2.imread(imgloc)
hight = np.shape(img)[0]
beam = np.zeros((hight,10,3))   

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray_3_channel = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)

horizontal = np.hstack((img,beam,gray_3_channel))

small = cv2.resize(horizontal, (0,0), fx=0.5, fy=0.5)  

cv2.imwrite("combi.png",small)

cv2.imshow("Combi",small)

cv2.waitKey()

运行代码后,我在plot窗口中看到以下图片: enter image description here

保存的“combi.png”文件显示正确的颜色:enter image description here

如果我在没有黑光束的情况下绘制这两张照片,我也会得到原来的颜色。 有人知道这黑光束有什么毛病吗

系统:Windows 10

IDE:Spyder(Python 2.7)


Tags: importimgpng颜色npcv2colorsmall
1条回答
网友
1楼 · 发布于 2024-05-16 09:35:44

默认的np.ndarray.dtypenp.float64,而对于image,它应该是np.uint8

这条线:

beam = np.zeros((hight,10,3)) 

然后,beamhorixxxsmall都是np.float64。所以显示float64。但写入时,会被截断为np.uint8


应更改为:

beam = np.zeros((hight,10,3), np.uint8) 

相关问题 更多 >