【问题标题】:How to get a-channel from LAB (l*a*b) color space in python如何在 python 中从 LAB (l*a*b) 颜色空间中获取 a 通道
【发布时间】:2017-10-10 19:50:03
【问题描述】:

我是 opencv 新手,正在尝试将 RGB 图像转换为 LAB 色彩空间。我正在使用下面的代码来做到这一点。

data_path = 'D:/Images/'
image_name= '1.png'
img = cv2.imread(os.path.join(data_path, image_name),cv2.IMREAD_COLOR) # Reads image from disk 
img = cv2.cvtColor(img, cv2.COLOR_BGR2LAB) # changes RGB to LAB color space

img = img [127.5, 1, 127.5]  # This i was trying to get a-channel only but gives error

现在我只想使用 LAB Image 的 a 通道​​作为 1 通道输入提供给我的程序。如何只使用 LAB 色彩空间图像的 a 通道​​?

【问题讨论】:

  • 您的意思是要将 L 通道传输到单独的程序 - 可能使用不同的语言?
  • @Mark Setchell 我想分离每个通道并且只想使用一个通道输出。我已经使用拆分功能做到了这一点。感谢 l_channel, a_channel, b_channel = cv2.split(img)
  • 人们为什么要对所有内容都投反对票?这是一个真正的问题。
  • 也打败了我。我几乎从不投反对票,除非某些事情非常不正确并且可能具有破坏性或危险性。无论如何,好的答案自然会通过投票而浮出水面,而不会出现令人讨厌的、报复性的投票。但是创造一个世界需要各种方式......

标签: python python-3.x opencv


【解决方案1】:

我正在使用opencv2和python来解决这个问题

    import cv2
    input = cv2.imread('path_to_image.png')
    cv2.imshow('Hello World', input)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    lab = cv2.cvtColor(input,cv2.COLOR_BGR2LAB)
    cv2.imshow("l*a*b",lab)

    L,A,B=cv2.split(lab)
    cv2.imshow("L_Channel",L) # For L Channel
    cv2.imshow("A_Channel",A) # For A Channel (Here's what You need)
    cv2.imshow("B_Channel",B) # For B Channel

    cv2.waitKey(0)
    cv2.destroyAllWindows()

希望这可以帮助您解决问题

【讨论】:

    【解决方案2】:

    我已经用下面的代码行解决了我的问题

    l_channel, a_channel, b_channel = cv2.split(img) #splits the image into 3 channles l, a and b
    

    它将图像分成我想要的 l、a 和 b 通道。这很容易,但由于我是 opencv 新手,所以我不知道。

    【讨论】:

      【解决方案3】:

      这是在 python 中绘制 Lab 通道的方法。您可以在我用作参考的this article 中找到更多详细信息。

          from keras.preprocessing.image import  img_to_array, load_img
          from skimage.color import rgb2lab, lab2rgb
          import matplotlib.pyplot as plt
          import numpy as np
          
          def extract_single_dim_from_LAB_convert_to_RGB(image,idim):
              '''
              image is a single lab image of shape (None,None,3)
              '''
              z = np.zeros(image.shape)
              if idim != 0 :
                  z[:,:,0]=80 ## I need brightness to plot the image along 1st or 2nd axis
              z[:,:,idim] = image[:,:,idim]
              z = lab2rgb(z)
              return(z)
          
          def plot_lab_spectrums():
              # Get image
              img = img_to_array(load_img("<image_path>",target_size=(400,400)))
              lab = rgb2lab(img/255.0)
              lab_l = extract_single_dim_from_LAB_convert_to_RGB(lab,0)
              lab_a = extract_single_dim_from_LAB_convert_to_RGB(lab,1)
              lab_db = extract_single_dim_from_LAB_convert_to_RGB(lab,2)
          
              # Plot the results
              fig, axes = plt.subplots(ncols=3, figsize=(12, 4))
              data = [('L: lightness', lab_l), ('a: green-magenta channel', lab_a), ('b: blue-yellow channel', lab_db)]
          
              for ax, (title, img) in zip(axes, data):
                  ax.set_title(title)
                  ax.imshow(img)
                  ax.axis('off')
          
              fig.tight_layout()
              plt.show()
          
          plot_lab_spectrums()
      

      【讨论】:

        猜你喜欢
        • 2015-04-29
        • 2017-07-16
        • 2012-06-18
        • 2014-02-08
        • 1970-01-01
        • 2015-03-11
        • 2011-04-14
        • 2021-09-21
        • 2020-01-02
        相关资源
        最近更新 更多