【问题标题】:Combining RGB colours of pictures into one picture: Python, Cimpl将图片的RGB颜色组合成一张图片:Python、Cimpl
【发布时间】:2019-11-08 16:12:19
【问题描述】:

问题是把三张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”)

【问题讨论】:

    标签: python rgb


    【解决方案1】:

    让我们澄清一下,您有 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。

    如果您还没有cv2numpy

    pip install numpy
    pip install opencv-python
    

    【讨论】:

    • 我觉得用pip3真的没必要。如果python 命令(不是python3)显示3.7
    • 是的,应该让问题更简洁。另外,有没有办法在没有库的情况下做到这一点?
    • 如果您不想使用外部库,我需要知道 Cimpl 中的内容,但老实说,使用总比不使用好。 Python 本身真的很慢,这些库(numpyopencv)是目前最标准的 Python 库
    • 显示Cimpl
    • 使用 cv2.merge() 代替 np.dstack。这样就不需要 numpy。
    猜你喜欢
    • 2020-03-13
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 2011-11-04
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多