机器学习与深度学习数据蛙数据分析每周作业

2019-10-08-OpenCV在jupyter notebo

2019-10-08  本文已影响0人  yimengtianya1

1、OpenCV在jupyter notebook中显示图像

  import numpy as np
  import cv2
  from matplotlib import pyplot as plt
  %matplotlib inline

  def show(picture_name):
      picture_name_full = picture_name + ".jpg"
      img = cv2.imread(picture_name_full)
      img2 = img[:,:,::-1]                  # 必须为 ::-1
      plt.imshow(img)

2、为什么画出的图像和原图有色差呢?

opencv的颜色通道顺序为[B,G,R],而matplotlib的颜色通道顺序为[R,G,B]。
解决方案:把R和B的位置调换一下
img = img[:,:,(2,1,0)]

因此最终代码为:

  import numpy as np
  import cv2
  from matplotlib import pyplot as plt
  %matplotlib inline

  def show(picture_name):
      picture_name_full = picture_name + ".jpg"
      img = cv2.imread(picture_name_full)
      img = img[:,:,(2,1,0)]                # opencv中的通道顺序是BGR,与Python的RGB刚好相反。因此此处需要调整顺序。
      img2 = img[:,:,::-1]                  # 必须为 ::-1
      plt.imshow(img)

参考文献:
https://blog.csdn.net/iamcfb_/article/details/89738434

上一篇 下一篇

猜你喜欢

热点阅读