python - 将图片的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”)
解决方案
让我们说清楚,您有 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
推荐阅读
- amazon-web-services - 用于自动扩展的 AWS Codestar 设置 Beanstalk 策略错误
- php - 从重复的 Wordpress 元框创建和显示内容
- android - 即使应用程序在前台运行,onNotificationPosted 方法也不会被调用
- python - Python:虽然不是异常
- assembly - 源和目标的大小必须相同 (8086)
- c - C - 使用数组打印从 1 到 100 的所有素数
- java - 使用 Java 客户端检测 Prometheus 指标
- node.js - 使用谷歌云存储库的问题
- hook - Ionic 4/Angular 7 生命周期钩子每页只调用一次
- android - Dagger 2 - 了解进行字段和方法注入的不同方式