opencv python将不同的频道图像合并为一个

时间:2017-05-22 12:01:32

标签: python image opencv numpy

我的卫星图像很少,每个卫星图像代表主卫星图像的一个通道,这些是总共11个图像,每个都用不同的通道标记,所有图像都是带灰度色彩空间的.tiff格式,现在我只想要&#39 ; s将这些图像合并为一个,将所有通道合并为一个图像,这是可能的,记住这里,我不想连续图像,这可以用这个来完成:

<script>
myFunction() {
 alert('resize working');
}
</script>

我想将所有这些合并到一个单独的图像中,而不会扭曲其中包含的数据,下面附加了几个频道图像。 enter image description here enter image description here enter image description here enter image description here

感谢任何帮助。

5 个答案:

答案 0 :(得分:3)

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)像素。

enter image description here 下面是合并后的图像,有3个频道。

enter image description here

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

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

答案 1 :(得分:1)

合并是什么意思?它们都是灰度图像,因此它们在灰度色彩空间中基本上是相同的通道。当您尝试blendadd图像时,某些信息注定会丢失。使用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()

using Add

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()

Using Blend

答案 2 :(得分: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)

答案 3 :(得分:1)

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

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

答案 4 :(得分:-1)

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

  1. scale:按给定数字缩放最终图像
  2. imgArray:是您要连接的所有图像的数组,无论每个图像中的通道如何
    class _HomesState extends State<Homes> {
      @override
      void initState() {
        print('hai im from initial');
        //payment(); // no need to call it here as simple funcation.
        super.initState();
      }

      Future<List<ApplicationMeta>> payment() async {
        final List<ApplicationMeta> appMetaList =
          await UpiPay.getInstalledUpiApplications();

        return appMetaList;
      }

      @override
      Widget build(BuildContext context) {
        // payment().then((value) => print(value));
        return FutureBuilder(
          future: payment(),
          builder: (context, snapshot){
            //check if connection state is done
            if(snapshot.connectionState == ConnectionState.done) print('loaded ${snapshot.data}');
            print(snapshot.connectionState);
          });
        }
      }

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