如何将 cv2 彩色图像(bgr)制作为蓝色范围或绿色范围图像?

发布于 2025-01-16 16:36:44 字数 294 浏览 0 评论 0原文

  1. numpy 数组 (x,y) = (0,10 f.eks.) 之间的未排序数据转换为彩色 cv2 图像 bgr 并保存。

    self.arr = self.arr * 255 #bgr 格式

    cv2.imwrite("img", self.arr)

如何使这个 cv2 彩色图像变为蓝色范围颜色(浅到深蓝色),以及如何使其变为绿色范围颜色(浅绿色到深绿色)?

我的想法是去 image2np 然后对数组做一些事情。然后返回np2image。但我不知道如何改变值以获得预期的颜色。

  1. A numpy array (x,y) = unsorted data between(0,10 f.eks.) is coverted to a colored cv2 image bgr and saved.

    self.arr = self.arr * 255 #bgr format

    cv2.imwrite("img", self.arr)

How to make this cv2 colored image to blue range color (light to dark blue), and how to make it to green range color(light to dark green)?

My thoughts are to go image2np and then do some stuff to the array. Then go back np2image. But I don't know how change values to get expected colours.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生活了然无味 2025-01-23 16:36:44

我不确定我是否理解问题,但我会将 RGB 转换为灰度,然后创建空的 RGB(带零)并放入>灰度作为图层B以获得“蓝色范围”或作为G获得“绿色范围”

import cv2
import numpy as np

img = cv2.imread('test/lenna.png')
cv2.imshow('RGB', img)

h, w = img.shape[:2]  # height, width

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray', gray_img)

blue_img = np.zeros((h,w,3), dtype='uint8') 
blue_img[:,:,0] = gray_img  # cv2 uses `BGR` instead of `RGB`
cv2.imshow('Blue', blue_img)

green_img = np.zeros((h,w,3), dtype='uint8') 
green_img[:,:,1] = gray_img  # cv2 uses `BGR` instead of `RGB`
cv2.imshow('Green', green_img)

red_img = np.zeros((h,w,3), dtype='uint8') 
red_img[:,:,2] = gray_img  # cv2 uses `BGR` instead of `RGB`
cv2.imshow('Red', red_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

图片Lenna 来自维基百科。

输入图片此处描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

I'm not sure if I understand problem but I would convert RGB to grayscale and next create empty RGB (with zeros) and put grayscale as layer B to get "blue range" or as G to get "green range"

import cv2
import numpy as np

img = cv2.imread('test/lenna.png')
cv2.imshow('RGB', img)

h, w = img.shape[:2]  # height, width

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray', gray_img)

blue_img = np.zeros((h,w,3), dtype='uint8') 
blue_img[:,:,0] = gray_img  # cv2 uses `BGR` instead of `RGB`
cv2.imshow('Blue', blue_img)

green_img = np.zeros((h,w,3), dtype='uint8') 
green_img[:,:,1] = gray_img  # cv2 uses `BGR` instead of `RGB`
cv2.imshow('Green', green_img)

red_img = np.zeros((h,w,3), dtype='uint8') 
red_img[:,:,2] = gray_img  # cv2 uses `BGR` instead of `RGB`
cv2.imshow('Red', red_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Image Lenna from Wikipedia.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文