【OpenCV实战】3.OpenCV颜色空间实战

OpenCV颜色空间实战

  • 〇、Coding实战内容
  • 一、imread
    • [1.1 函数介绍](#1.1 函数介绍)
    • [1.2 Flags](#1.2 Flags)
    • [1.3 Code](#1.3 Code)
  • [二. 色彩空间](#二. 色彩空间)
    • [2.1 获取单色空间](#2.1 获取单色空间)
    • [2.2. HSV、YUV、RGB](#2.2. HSV、YUV、RGB)
    • [2.3. 不同颜色空间应用场景](#2.3. 不同颜色空间应用场景)

〇、Coding实战内容

  1. OpenCV imread()方法不同的flags差异性
  2. 获取单色通道【R通道、G通道、B通道】
  3. HSV、YUV、RGB

一、imread

1.1 函数介绍

java 复制代码
/**
The function imread loads an image from the specified file and returns it
@param filename Name of file to be loaded.
@param flags Flag that can take values of cv::ImreadModes
**/
CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );

1.2 Flags

复制代码
// enum ImreadModes {
//        IMREAD_UNCHANGED            = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation.
//        IMREAD_GRAYSCALE            = 0,  //!< If set, always convert image to the single channel grayscale image (codec internal conversion).
//        IMREAD_COLOR                = 1,  //!< If set, always convert image to the 3 channel BGR color image.
//        IMREAD_ANYDEPTH             = 2,  //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
//        IMREAD_ANYCOLOR             = 4,  //!< If set, the image is read in any possible color format.
//        IMREAD_LOAD_GDAL            = 8,  //!< If set, use the gdal driver for loading the image.
//        IMREAD_REDUCED_GRAYSCALE_2  = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
//        IMREAD_REDUCED_COLOR_2      = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
//        IMREAD_REDUCED_GRAYSCALE_4  = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
//        IMREAD_REDUCED_COLOR_4      = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
//        IMREAD_REDUCED_GRAYSCALE_8  = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
//        IMREAD_REDUCED_COLOR_8      = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
//        IMREAD_IGNORE_ORIENTATION   = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
//      };

常用的有三种

a. -1 IMREAD_UNCHANGED:忽视alpha通道

b. 0 IMREAD_GRAYSCALE:灰度图

c. 1 IMREAD_COLOR 不填默认值,且格式为BGR

1.3 Code

assign_2.cpp

java 复制代码
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <string>

using namespace cv;
using namespace std;

#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <string>
using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{
    std::string filePath = std::string(__FILE__);
    size_t pos = filePath.find_last_of("/\\");
    std::string rootPath = filePath.substr(0, pos); // string path = string(__BASE_FILE__)+"/img.webp";
    cout << rootPath;
    
    //IMREAD_COLOR BGR
    Mat image = imread(rootPath+"/img.webp",IMREAD_COLOR);
    //IMREAD_UNCHANGED, 无alpha通道
    Mat image1 = imread(rootPath+"/img.webp",IMREAD_UNCHANGED);
    //IMREAD_GRAYSCALE 灰度图
    Mat image2 = imread(rootPath+"/img.webp",IMREAD_GRAYSCALE);
  
 	namedWindow("imread imread_unchanged");    // 创建一个标题为 "hello" 的窗口
    imshow("hello", image); // image1, image2 //在窗口 "hello" 中显示图片
    waitKey(0);              // 等待用户按下键盘
    destroyWindow("hello");  // 销毁窗口 "hello"
    return 0;
}

输出结果:

二. 色彩空间

2.1 获取单色空间

java 复制代码
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <string>
using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{
    /**
     * 二、色彩空间
     * */
    //红色
    vector<Mat> channels;
    split(image, channels);//bgr
    channels[0] = Mat::zeros(image.rows, image.cols, CV_8UC1); // blue
    channels[1] = Mat::zeros(image.rows, image.cols, CV_8UC1); // green
    Mat red;
    merge(channels, red);
	
	//蓝色
	vector<Mat> channels_1;
	split(image, channels_1);//bgr
    channels[1] = Mat::zeros(image.rows, image.cols, CV_8UC1); // green
    channels[2] = Mat::zeros(image.rows, image.cols, CV_8UC1); // red
    Mat blue;
    merge(channels, blue);
    
	//绿色
	vector<Mat> channels_2;
	split(image, channels_2);//bgr
    channels[0] = Mat::zeros(image.rows, image.cols, CV_8UC1); // green
    channels[2] = Mat::zeros(image.rows, image.cols, CV_8UC1); // red
    Mat green;
    merge(channels, green);
}

输出结果

2.2. HSV、YUV、RGB

java 复制代码
int main(int argc, char *argv[])
{
    std::string filePath = std::string(__FILE__);
    size_t pos = filePath.find_last_of("/\\");
    std::string rootPath = filePath.substr(0, pos); // string path = string(__BASE_FILE__)+"/img.webp";
    cout << rootPath;
    Mat image = imread(rootPath+"/img.webp",IMREAD_COLOR);
    
    /**
     * 三、色彩空间
     **/
    Mat hsv;
    cvtColor(image,hsv,COLOR_BGR2HSV);

    Mat rgb;
    cvtColor(image,hsv,COLOR_BGR2RGB);
    
    Mat yuv;
    cvtColor(image,yuv,COLOR_BGR2YUV);
    
    namedWindow("hsv");    
    imshow("hsv", hsv); 
    waitKey(0);            
    destroyWindow("hsv"); 
    return 0;
}

输出结果

复制代码
颜色空间:
具体可搜索wikipedia,有很详细的介绍
1. HSV vs HSB:https://en.wikipedia.org/wiki/HSL_and_HSV
2. YUV:可参考本人以前的一篇文章,https://blog.csdn.net/Scott_S/article/details/118525159?spm=1001.2014.3001.5501

2.3. 不同颜色空间应用场景

  1. RGB:视频监视器,彩色摄像机
  2. HSV [色调、饱和度、亮度]:彩色处理为目的
  3. CMYK :印刷行业,如果用过小米照片打印机,就会发现一张照片需要渲染4次,按照如下流程
    • Cyan:蓝青色;
    • magenta:品红、杨红;
    • Yellow:黄色;
    • Key: (black)
  4. YUV :电视信号传输,占用极少的带宽
相关推荐
king王一帅6 分钟前
Incremark Solid 版本上线:Vue/React/Svelte/Solid 四大框架,统一体验
前端·javascript·人工智能
泰迪智能科技2 小时前
分享|职业技术培训|数字技术应用工程师快问快答
人工智能
Dxy12393102164 小时前
如何给AI提问:让机器高效理解你的需求
人工智能
少林码僧4 小时前
2.31 机器学习神器项目实战:如何在真实项目中应用XGBoost等算法
人工智能·python·算法·机器学习·ai·数据挖掘
钱彬 (Qian Bin)4 小时前
项目实践15—全球证件智能识别系统(切换为Qwen3-VL-8B-Instruct图文多模态大模型)
人工智能·算法·机器学习·多模态·全球证件识别
没学上了5 小时前
CNNMNIST
人工智能·深度学习
宝贝儿好5 小时前
【强化学习】第六章:无模型控制:在轨MC控制、在轨时序差分学习(Sarsa)、离轨学习(Q-learning)
人工智能·python·深度学习·学习·机器学习·机器人
智驱力人工智能5 小时前
守护流动的规则 基于视觉分析的穿越导流线区检测技术工程实践 交通路口导流区穿越实时预警技术 智慧交通部署指南
人工智能·opencv·安全·目标检测·计算机视觉·cnn·边缘计算
AI产品备案5 小时前
生成式人工智能大模型备案制度与发展要求
人工智能·深度学习·大模型备案·算法备案·大模型登记
AC赳赳老秦5 小时前
DeepSeek 私有化部署避坑指南:敏感数据本地化处理与合规性检测详解
大数据·开发语言·数据库·人工智能·自动化·php·deepseek