提取 RGB 图像的 RGB 和 Lab 值

问题描述 投票:0回答:1

tomato 我拍摄了一张白色背景的番茄图像。

我想测量(仅限番茄)的rgb值,然后将值转换为lab

我是Python新手,我想了解该过程的完整细节。 我尝试了很多不同的方法,但仍然!!

除了获取值 RGB 和 Lab

python rgb
1个回答
0
投票
  • 安装所需的库

首先,确保您已经安装了 OpenCV 和 NumPy。您可以使用 pip 安装它们:

pip install opencv-python
pip install numpy

代码示例

import cv2
import numpy as np

# Read the image
image = cv2.imread('tomato.jpg')

# Convert the image to RGB (OpenCV uses BGR by default)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Convert the image to LAB color space
image_lab = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)

# Create a mask for the tomato
lower_bound = np.array([20, 20, 100])  # Lower RGB bound for tomato color
upper_bound = np.array([255, 255, 255])  # Upper RGB bound for tomato color

mask = cv2.inRange(image_rgb, lower_bound, upper_bound)

# Get RGB values where the mask is not zero
tomato_rgb_values = image_rgb[mask != 0]

# Get LAB values where the mask is not zero
tomato_lab_values = image_lab[mask != 0]

print("Tomato RGB values:", tomato_rgb_values)
print("Tomato LAB values:", tomato_lab_values)

备注:

  1. 创建蒙版:可能需要调整

    lower_bound
    函数中的
    upper_bound
    cv2.inRange
    值,以更好地隔离特定图像中的番茄。

  2. 色彩空间转换:OpenCV 默认读取 BGR 格式的图像,所以你会

© www.soinside.com 2019 - 2024. All rights reserved.