opencv相关问题

  • cv2.imshow()显示图像时,一闪而过
cv.imshow("img", img)
cv.waitKey(0)   # 无限循环,等待用户按键触发跳出循环
  •  cv2.imwrite()保存图像为全黑
img = img * 255
cv2.imwrite('img.jpg', img)

原因:原本img的数值是在0-255,但在保存img被标准化了,使得img的值0-1之间,所以在保存时img值需要乘上255。

  • 两图像加权融合并保存
img_ori = cv2.imread(Ori_img_path)
# img_ori = adaptContrastEnhancement(img_ori, 15, 10)  # 颜色增强有杂质
hmap_viridis = cv2.imread(heatmap_path+'_viridis.png')
# img_ori = io.imread(Ori_img_path)
# gam = exposure.adjust_gamma(img_ori, 0.1)  # 调亮如果gamma<1, 新图像比原图像亮,数值越小越亮

# hmap = io.imread(heatmap_path)
# gam = exposure.adjust_gamma(hmap, 0.1)

img_ori_resize = cv2.resize(img_ori, (hmap_viridis.shape[1], hmap_viridis.shape[0]))
combined = cv2.addWeighted(img_ori_resize, 0.6, hmap_viridis, 0.4, 0)
# combined = cv2.add(img_ori_resize, hmap)  # 用cv2.add方法两图像融合后会变色

# ############ cv2.imwrite方法保存图像 
CombinedImgSave_path = savePath + '_combinedImg.png'
# cv2.imshow('combined', combined)  
# cv2.waitKey(0)
cv2.imwrite(filename=CombinedImgSave_path, img=combined) # 保存后不变色

# ############ PIL方法保存 
from PIL import Image
combined = Image.fromarray(combined)
combined.save(CombinedImgSave_path)  # 会变色

# ############ plt.savefig方法保存 
plt.imshow(combined)
plt.savefig(CombinedImgSave_path)  # 会变色

# ############ imageio.imsave方法保存--待验证
import imageio 
imageio.imsave(CombinedImgSave_path, combined) # 会变色

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值