【问题标题】:How do I use Gimp / OpenCV Color to separate images into coloured RGB layers?如何使用 Gimp / OpenCV Color 将图像分成彩色 RGB 层?
【发布时间】:2013-10-13 08:26:24
【问题描述】:

我有一张 JPG 图片,我想找到一种方法:

  • 将图像分解为红色、绿色和蓝色强度层(每个通道 8 位)。
  • 用适当的颜色为这些现在的“灰度”图像上色
  • 为每个通道生成 3 个颜色适当的输出图像。

例如,如果我有一张图片: 狗.jpg

我想制作: dog_blue.jpg dog_red.jpg 和 dog_green.jpg

我不想要每个通道的灰度图像。我希望每张图片都用正确的颜色表示。

我已经设法使用 gimp 中的分解功能来获取图层,但是每个图层都是灰度的,我似乎无法为其添加颜色。

我目前正在为其他项目使用 OpenCV 和 Python 绑定,因此如果使用 gimp 不容易做到,那么任何合适的代码都可能有用

【问题讨论】:

    标签: python opencv rgb gimp


    【解决方案1】:

    在 BGR 图像中,您有三个通道。当您使用 split() 函数分割通道时,例如 B,G,R=cv2.split(img),则 B,G,R 变为单通道或单通道图像。因此,您需要添加两个带零的额外通道以使其成为 3 通道图像,但为特定颜色通道激活。

    【讨论】:

      【解决方案2】:

      也许您已经弄清楚了这一点,但这里是为那些想要以自己的颜色“看到”他们分离的通道的人(即 - 红色中的红色,绿色中的绿色等)。

      每个通道只是一个单值图像,可以解释为单色图像。但是你可以通过添加两个假的空通道(下面的zero_channel)给它“添加颜色”,并将cv2.merge它变成一个 多通道图像。

      #!/usr/bin/env python
      
      import cv2
      import numpy as np
      import os
      import sys
      
      SHOW = True
      SAVE = True
      
      
      def split_channels(filename):
          img = cv2.imread(filename)
          if len(img.shape) != 3 or img.shape[2] != 3:
              sys.stderr.write('{0}: not a correct color image'.format(filename))
              return
          channels = cv2.split(img)
          zero_channel = np.zeros_like(channels[0])
          red_img = cv2.merge([zero_channel, zero_channel, channels[2]])
          green_img = cv2.merge([zero_channel, channels[1], zero_channel])
          blue_img = cv2.merge([channels[0], zero_channel, zero_channel])
          if SHOW:
              cv2.imshow('Red channel', red_img)
              cv2.imshow('Green channel', green_img)
              cv2.imshow('Blue channel', blue_img)
              cv2.waitKey(0)
          if SAVE:
              name, extension = os.path.splitext(filename)
              cv2.imwrite(name+'_red'+extension, red_img)
              cv2.imwrite(name+'_green'+extension, green_img)
              cv2.imwrite(name+'_blue'+extension, blue_img)
      
      
      def main():
          if len(sys.argv) < 2:
              print('Usage: {0} <rgb_image>...'.format(sys.argv[0]))
          map(split_channels, sys.argv[1:])
      
      
      if __name__ == '__main__':
          main()
      

      【讨论】:

        【解决方案3】:

        您需要分割图像的通道。为此,您可以使用拆分功能source

         // "channels" is a vector of 3 Mat arrays:
         vector<Mat> channels(3);
         // split img:
         split(img, channels);
         // get the channels (dont forget they follow BGR order in OpenCV)
        
         namedWindow("channelR",1);
         namedWindow("channelB",1);
         namedWindow("channelG",1);
        
         imshow("channelB",channels[0]);
         imshow("channelG",channels[1]);
         imshow("channelR",channels[2]);
        
         imwrite( "channelR.jpg", channels[2]);
         imwrite( "channelG.jpg", channels[1]);
         imwrite( "channelB.jpg", channels[0]);
        

        【讨论】:

          【解决方案4】:

          由于蓝色、绿色、红色图像每个只有1个通道。所以,这基本上是一个灰度图像。 例如,如果您想在 dog_blue.jpg 中添加颜色,则创建 3 通道图像 并复制所有通道中的内容或执行 cvCvtColor(src,dst,CV_GRAY2BGR)。现在您可以为其添加颜色,因为它已成为 3 通道图像。

          【讨论】:

          • 如何为每张新的 3 通道图像着色
          • 如果你想制作例如 dog_blue.jpg 它是 1 通道到 3 通道,那么只需使用 cvCreateImage 创建一个 3 通道图像,然后执行 cvCvtColor(src,dst, CV_GRAY2BGR)。我相信你在问这个。
          猜你喜欢
          • 2019-07-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-26
          • 1970-01-01
          • 2011-06-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多