【问题标题】:opencv python merge different channel images into oneopencv python将不同的通道图像合并为一个
【发布时间】:2017-10-22 01:57:14
【问题描述】:

我有几张卫星图像,每张都代表一个主要卫星图像的通道,总共有 11 张图像,每张都标有不同的通道,所有图像都是 .tiff 格式的灰度色彩空间,现在我只想合并这些图像合而为一,将所有通道表示为一张图像,这可能吗,记住这里,我不想连接图像,可以使用以下方法完成:

vis = np.concatenate((img1, img2), axis=1)

我想将它们全部合并到一张图片中,而不会扭曲其中包含的数据,下面附上了几张通道图片。

感谢任何帮助。

【问题讨论】:

  • 你能上传没有白边吗?
  • 一个典型的RGB图像最多只能有3个通道,你想如何将11个通道合并为一个图像?您的意思是添加它们吗?
  • 它们不是 RGB 图像,它们是灰度图像,每个像素有 3 个元组值我想通过合并所有 11 个图像来创建一个图像。

标签: python image opencv numpy


【解决方案1】:

首先,您需要carefully 考虑您拥有的number of channels,以便创建有用的图像。在下面的示例中,我假设您有可以组合成 RGB 图像的三个通道(红色、绿色和蓝色)。

import numpy as np
import cv2

"""Read each channel into a numpy array. 
Of course your data set may not be images yet.
So just load them into three different numpy arrays as neccessary"""
a = cv2.imread('chanel_1.jpg', 0)
b = cv2.imread('chanel_2.jpg', 0)
c = cv2.imread('chanel_3.jpg', 0)

"""Create a blank image that has three channels 
and the same number of pixels as your original input"""
needed_multi_channel_img = np.zeros((a.shape[0], a.shape[1], 3))

"""Add the channels to the needed image one by one"""
needed_multi_channel_img [:,:,0] = a
needed_multi_channel_img [:,:,1] = b
needed_multi_channel_img [:,:,2] = c

"""Save the needed multi channel image"""
cv2.imwrite('needed_multi_channel_img.png',needed_multi_channel_img)

【讨论】:

  • 更简单:img=cv2.merge((b,g,r))
【解决方案2】:

由于OpenCV 3.x将图像存储为numpy数组,我们可以简单地平均每张图像并将它们相加,前提是图像的高度和宽度完全相同。

img_1 = cv2.imread('./imagesStackoverflow/sat_1_331-442.png')
img_2 = cv2.imread('./imagesStackoverflow/sat_2_331-442.png')
img_3 = cv2.imread('./imagesStackoverflow/sat_3_331-442.png')
img_4 = cv2.imread('./imagesStackoverflow/sat_4_331-442.png')

no_img = 4
img = img_1/no_img + img_2/no_img + img_3/no_img + img_4/no_img

为了快速获得结果,我手动将四个图像的大小编辑为442(h) x 331(w) 像素。

下面是合并后的图像,有 3 个通道。

要合并 11 张图片,您可以将代码扩展为:

img = img_1/no_img + img_2/no_img + img_3/no_img + ... + img_11/no_img

【讨论】:

  • 这种方法可以正常工作,正如预期的那样,从 opencv 2.2.x 开始,所有图像本身都用作 numpy 数组。这也适用于 OpenCV 2.2.x。
【解决方案3】:

我已经连续尝试了 2 天,但我无法理解的一件事是 opencv 的合并函数需要一个元组,而不是 3 个不同的参数。

image = cv2.imread(image_file, 1)
R, G, B = cv2.split(image)
output_image = cv2.merge ( (R, G, B) )

【讨论】:

    【解决方案4】:

    合并是什么意思?它们都是灰度图像,因此它们在灰度色彩空间中基本上是相同的通道。当您尝试blend 或add 图像时,某些信息注定会丢失。使用 OpenCV 或 Pillow 中的函数尝试以上两种方法。

    mul1 = ImageChops.add(img1, img2, scale=2)
    mul2 = ImageChops.add(img3, img4, scale = 2)
    mul3 = ImageChops.add(mul1, mul2, scale = 2)
    
    mul3.show()
    

    R1 = img1.convert('RGBA').resize([456,512])
    R2 = img2.convert('RGBA').resize([456,512])
    R3 = img3.convert('RGBA')
    R4 = img4.convert('RGBA')
    
    S1 = ImageChops.blend(R1,R2,0.5)
    S2 = ImageChops.blend(R3,R4,0.5)
    S3 = ImageChops.blend(S1,S2,0.5)
    S3.show()
    

    【讨论】:

      【解决方案5】:

      这是相同的 Python 代码。它可以接收任何通道的图像,但必须像 hcontat、vcontat、hstack、vstack 一样保持宽度和高度。

      1. scale:按给定数字缩放最终图像
      2. imgArray:是您要连接的所有图像的数组,无论每个图像中的通道如何
      import numpy as np
      import cv2
      def stackImages(scale,imgArray):
          rows = len(imgArray)
          cols = len(imgArray[0])
          rowsAvailable = isinstance(imgArray[0],list)
          width = imgArray[0][0].shape[1]
          height = imgArray[0][0].shape[0]
          if rowsAvailable:
              for x in range(0,rows):
                  for y in range(0, cols):
                      if imgArray[x][y].shape[:2] == imgArray[x][y].shape[:2]:
                          imgArray[x][y] = cv2.resize(imgArray[x][y],(0,0),None,scale,scale)
                      else:
                          imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[0]), None, scale, scale)
                      if len(imgArray[x][y].shape) == 2: imgArray[x][y] =cv2.cvtColor(imgArray[x][y],cv2.COLOR_GRAY2BGR)
              imageBlank = np.zeros((height,width,3), np.uint8)
              hor = [imageBlank]*rows
              hor_con = [imageBlank]*rows
              for x in range(0, rows):
                  hor[x] = np.hstack(imgArray[x])
              ver = np.vstack(hor)
          else:
              for x in range(0, rows):
                  if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
                      imgArray[x] = cv2.resize(imgArray[x],(0,0),None,scale,scale)
                  else:
                      imgArray[x] = cv2.resize(imgArray[x],(imgArray[0].shape[0],imgArray[0].shape[1]),None,scale,scale)
                  if len(imgArray[x].shape) == 2: imgArray[x] =cv2.cvtColor(imgArray[x],cv2.COLOR_GRAY2BGR)
              hor = np.hstack(imgArray)
              ver = hor
          return ver
      

      函数 stackedImages(scale, imageArray) 属于提供轨迹栏和活体检测教程的人创建的包。在这里查看:https://www.youtube.com/watch?v=Fchzk1lDt7Q&t=25s

      【讨论】:

      • 这是一团莫名其妙的代码,没有提到频道或cv2.split和cv2.merge
      • 仔细阅读我在代码上方和下方编写的内容?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 2020-05-07
      • 2022-07-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      相关资源
      最近更新 更多