opencv: 颜色空间转换(cv2.cvtColor) 探究(图示+源码)

 API Definition

我们从 OpenCV官网 的Miscellaneous Image Transformations 上,可查到 cv2.cvtColor 这个api的定义如下:

cvtColor

Converts an image from one color space to another.

C++: void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )
Python: cv2.cvtColor(src, code[, dst[, dstCn]]) → dst
C: void cvCvtColor(const CvArr* src, CvArr* dst, int code)
Python: cv.CvtColor(src, dst, code) → None

Parameters:
src – input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC… ), or single-precision floating-point.
dst – output image of the same size and depth as src.
code – color space conversion code (see the description below).
dstCn – number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code.

The function converts an input image from one color space to another. In case of a transformation to-from RGB color space, the order of the channels should be specified explicitly (RGB or BGR). Note that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on.

参数探究

在探究的过程中,我发现 code 参数的 输入类型int 型,于是写代码进行验证:

import cv2
color_types = [cv2.COLOR_BGR2RGB, cv2.COLOR_BGR2GRAY]
for color_type in color_types:
    print ('{}    {}'.format(color_type, type(color_type)))

结果证明了,原来 code 参数的 输入 不管是cv2.COLOR_BGR2RGBcv2.COLOR_BGR2GRAY,或是其他 颜色转换空间(color space conversion),均是 int 型的:

4    <type 'int'>
6    <type 'int'>

颜色空间转换探究

于是我另外编写了一小段代码,探究哪些整数可以作为 cv2.cvtColorcode 参数的 替代输入值 ,并看看在 转换了颜色空间 后,会生成什么样的图像。

(自己写的实验源码附在文章末尾)

验证得知,以下整数可以作为 cv2.cvtColorcode 参数的 替代输入值

Valid index in cv2.cvtColor:
[0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 44, 45, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 127, 128, 129, 130, 131, 132, 133, 134]

效果图

原图像

在进行 转换颜色空间 之前的原图(./pic/origin_pic.jpg):
这里写图片描述

生成的图像

./generated_pics/1.jpg:
这里写图片描述

./generated_pics/2.jpg:
这里写图片描述

./generated_pics/6.jpg:
这里写图片描述

./generated_pics/32.jpg:
这里写图片描述

./generated_pics/34.jpg:
这里写图片描述

./generated_pics/35.jpg:
这里写图片描述

./generated_pics/36.jpg:
这里写图片描述

./generated_pics/38.jpg:
这里写图片描述

./generated_pics/41.jpg:
这里写图片描述

./generated_pics/53.jpg:
这里写图片描述

./generated_pics/54.jpg:
这里写图片描述

./generated_pics/55.jpg:
这里写图片描述

./generated_pics/69.jpg:
这里写图片描述

./generated_pics/72.jpg:
这里写图片描述

./generated_pics/73.jpg:
这里写图片描述

./generated_pics/79.jpg:
这里写图片描述

./generated_pics/82.jpg:
这里写图片描述

./generated_pics/85.jpg:
这里写图片描述

Code

附上自己写的实验代码:

# coding=utf-8

origin_pic = './pic/origin_pic.jpg'
save_folder = './generated_pics'

import os
try:
    os.makedirs(save_folder)
except OSError:
    pass

import cv2
img = cv2.imread(origin_pic)
valid_index = []
for color_type in range(-300, 1000, 1):
    try:
        img_new = cv2.cvtColor(img, color_type)
        cv2.imwrite(os.path.join(save_folder, str(color_type)+'.jpg'), img_new)
        valid_index.append(color_type)
    except:
        pass
print ('Valid index in cv2.cvtColor:\n{}'.format(valid_index))
<think>嗯,用户想了解如何使用vLLM部署DeepSeek大模型。首先,我需要回顾一下用户提供的引用内容,确保回答准确。根据引用[1]和[3],部署步骤包括安装Python环境、下载模型、安装vLLM,以及启动服务。同时,要注意可能的安装错误和系统兼容性问题,比如vLLM不支持Windows,只能在Linux或macOS上运行。 用户可能需要详细的步骤指导,所以需要分步骤说明。首先,安装Python环境,推荐使用3.8以上版本,并用conda创建虚拟环境。然后,下载DeepSeek模型,需要注意模型格式是否为vLLM兼容的HuggingFace格式。如果用户下载的是原始仓库的模型,可能需要转换格式,这里可能需要提醒用户检查。 接下来安装vLLM,引用[3]提到建议从源码安装以获得最新优化,所以需要给出两种安装方式:pip直接安装和源码安装。这里要注意用户可能遇到的安装问题,比如依赖项缺失或版本冲突,可能需要安装CUDA工具包和正确版本的PyTorch。 在启动服务部分,需要给出示例代码,说明如何导入模型和启动服务,包括参数设置如tensor并行和端口号。同时,用户可能需要知道如何测试API,所以提供curl命令示例会很有帮助。 另外,用户提供的引用中提到了一些常见问题,比如安装报错、启动报错和Windows不支持,这些都需要在注意事项里强调。特别是Windows用户可能需要切换到Linux环境,或者使用WSL,但官方不支持可能会有问题,需要提醒。 最后,生成相关问题时要围绕部署过程中的其他方面,比如性能优化、兼容性处理、监控方法等,帮助用户进一步解决问题。需要确保回答结构清晰,步骤详细,避免遗漏关键点,同时引用正确的来源。</think># 使用vLLM部署DeepSeek大模型的实践指南 ## 一、环境准备 1. **Python环境配置** 推荐使用Python 3.8及以上版本,建议通过conda创建虚拟环境: ```bash conda create -n vllm_env python=3.10 conda activate vllm_env ``` 需安装CUDA 11.8或更高版本,并验证PyTorch与CUDA的兼容性[^3]。 2. **模型文件准备** 从官方渠道下载DeepSeek模型权重(如`DeepSeek-7B`),确保模型文件为HuggingFace格式: ``` /path/to/deepseek_model/ ├── config.json ├── model.safetensors └── tokenizer/ ``` ## 二、vLLM安装与验证 ### 标准安装(推荐) ```bash pip install vllm==0.3.3 # 确认版本兼容性 ``` ### 源码安装(启用最新优化) ```bash git clone https://github.com/vllm-project/vllm.git cd vllm && pip install -e . # 开发模式安装 ``` 安装后验证: ```python import vllm print(vllm.__version__) # 应显示正确版本 ``` ## 三、模型部署 ### 启动推理服务 ```bash python -m vllm.entrypoints.openai.api_server \ --model /path/to/deepseek_model \ --tensor-parallel-size 2 \ --port 8000 ``` 关键参数说明: - `--tensor-parallel-size`:GPU并行数量 - `--trust-remote-code`:当模型需要自定义代码时添加 - `--max-model-len`:设置最大上下文长度[^2] ### API调用测试 ```bash curl http://localhost:8000/v1/completions \ -H "Content-Type: application/json" \ -d '{ "model": "deepseek_model", "prompt": "如何解释量子纠缠?", "max_tokens": 150 }' ``` ## 四、关键注意事项 1. **系统兼容性** vLLM仅支持Linux/macOS系统,Windows用户需使用WSL2或容器化部署[^1] 2. **常见报错处理** - CUDA版本不匹配:`nvidia-smi`验证驱动版本,重装对应CUDA工具包 - 模型加载失败:检查模型目录结构是否符合HuggingFace格式 - 内存不足:启用`--swap-space 8`参数增加交换空间[^2] 3. **性能优化建议** - 启用`--quantization awq`进行8bit量化 - 使用`vLLM-Engine`实现动态批处理 - 配合NVIDIA Triton提升吞吐量 ```python # 示例:Python客户端调用 from vllm import LLM llm = LLM(model="deepseek_model") outputs = llm.generate(["AI的未来发展方向是"]) print(outputs[0].text) ```
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值