首页 > 解决方案 > 将图片的RGB颜色组合成一张图片:Python、Cimpl

问题描述

问题是将三张RGB图片组合成一张原始图片。我需要将同一张图片的 RGB 中的三张过滤图片作为输入。一红一绿一蓝。我试图获取图像中的每个像素并将其添加到一个元组中,该元组存储它的值。

from Cimpl import *

red_image = load_image("red_image.jpg")
blue_image = load_image("blue_image.jpg")
green_image = load_image("green_image.jpg")

new_image = copy(red_image)

for pixel in new_image:

    x, y, (r, g, b) = pixel

for bluePixel in blue_image:
    xBlue, yBlue, (rBlue, gBlue, bBlue) = bluePixel
    new_colour = create_color(r+rBlue,g+gBlue,b+bBlue)
    set_color (new_image, x, y, new_colour)


for greenPixel in green_image:
    xGreen, yGreen, (rGreen, gGreen, bGreen) = greenPixel
    new_colour = create_color(r+rGreen,g+gGreen,b+bGreen)
    set_color (new_image, x, y, new_colour)    

show(red_image)
show(new_image)

我似乎又得到了相同的图片,而不是红色、蓝色和绿色滤镜的“组合图像”(red_image.jpg,因为我将它用作“new_image”)

标签: pythonrgb

解决方案


让我们说清楚,您有 3 张图像,每张图像都有 1 个通道,您想将它们组合成 1 个 3 通道图像吗?如果我是对的,那么试试这个。

import cv2
import numpy as np

red = cv2.imread('red.jpg')
green = cv2.imread('green.jpg')
blue = cv2.imread('blue.jpg')

image = np.dstack((blue, green, red)) # combine them, I'm not sure should I use red or blue first here though

cv2.imwrite('image.jpg')  # save it

cv2.imshow('img', image)  # show it
cv2.waitKey()

请注意,opencv 使用 BGR 而不是 RGB。

如果你还cv2没有numpy

pip install numpy
pip install opencv-python

推荐阅读