对于Lab颜色空间,OpenCV同样实现了BGR和Lab之间的转换,如果BGR的值在[0,1]之间,那么转换为Lab的取值范围为:


通过以下程序调节L分量,观察其效果:

#include<opencv2/core.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>
using namespace cv;
#include<string>
using namespace std;
//显示Lab颜色的色卡
Mat colorMap;
//L的取值范围 [0,100]
int L = 50;
int MAX_L = 100;
//a的取值范围[-127,128]
int MAX_a = 255;
//b的取值范围[-127,128]
int MAX_b = 255;
//颜色显示窗口
string showColor = "Lab";
//回调函数
void callback(int, void*);
int main(int argc, char*argv[])
{
	//分配内存
	colorMap.create(Size(MAX_a, MAX_b), CV_32FC3);
	//命名窗口
	namedWindow(showColor, WINDOW_GUI_EXPANDED);
	//调节 L 分量
	createTrackbar("L分量", showColor, &L, MAX_L, callback);
	callback(0, 0);
	waitKey(0);
	return 0;
}
void callback(int, void*)
{
	//MAX_a x MAX_b种颜色
	for (int a = 0; a < MAX_a; a++)
	{
		for (int b = 0; b < MAX_b; b++)
		{
			colorMap.at<Vec3f>(a, b) = Vec3f(L, a - 127, b - 127);
		}
	}
	//颜色空间转换
	cvtColor(colorMap, colorMap, COLOR_Lab2BGR);
	//显示颜色空间
	imshow(showColor, colorMap);
}

显示结果如下:

   


如果输入的RGB时CV_8U,那么转换的Lab也是CV_8U,值的范围是:

#include<opencv2/core.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>
using namespace cv;
#include<string>
using namespace std;
//显示颜色
Mat colorMap;
//L的取值范围 [0,100]
int L = 0;
int MAX_L = 100;
//a的取值范围[-127,128]
int a = 0;
int MAX_a = 255;
//b的取值范围[-127,128]
int b = 0;
int MAX_b = 255;
//颜色显示窗口
string showColor = "Lab";
//回调函数
void callback(int, void*);
int main(int argc, char*argv[])
{
	//分配内存
	colorMap.create(Size(500, 500), CV_32FC3);
	//命名窗口
	namedWindow(showColor, WINDOW_GUI_EXPANDED);
	//调节 L 分量
	createTrackbar("L分量", showColor, &L, MAX_L, callback);
	//调节 a 分量
	createTrackbar("a分量", showColor, &a, MAX_a, callback);
	//调节 b 分量
	createTrackbar("b分量", showColor, &b, MAX_b, callback);
	callback(0, 0);
	waitKey(0);
	return 0;
}
void callback(int, void*)
{
	//Lab颜色
	colorMap.setTo(Vec3f(L, a-127, b-127));
	//颜色空间转换
	cvtColor(colorMap, colorMap, COLOR_Lab2BGR);
	//显示颜色空间
	imshow(showColor, colorMap);
}

运行结果如下:

  

可以通过调节这三个分量观察颜色的变换。

---------------------------------------------------------------------------------------------------------------------

把CV_8UC3的BGR图像(不进行归一化处理),Lab的取值范围也会被归到【0,255】,分别显示三个通道的图像:

#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
using namespace cv;
#include<iostream>
using namespace std;
int main(int argc, char*argv[])
{
	//输入图像
	Mat img = imread(argv[1],IMREAD_ANYCOLOR);
	if (!img.data || img.channels() != 3)
		return -1;
	//输入图像的归一化
	//img.convertTo(img, CV_32FC3, 1.0 / 255, 0);
	cout << img.depth() << endl;
	/*--------BGR 转 Lab--------------*/
	Mat lab;
	cvtColor(img, lab, COLOR_BGR2Lab);
	cout << lab.depth() << endl;
	vector<Mat> labPlane;
	/*-------分离Lab的三个通道---------*/
	split(lab, labPlane);
	//显示各个通道
	imshow("Lab-L", labPlane[0]);
	imshow("Lab_a", labPlane[1]);
	imshow("Lab-b", labPlane[2]);
	waitKey(0);
	return 0;
}




GitHub 加速计划 / opencv31 / opencv
216
19
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:8 天前 )
5b1d3255 build: fix OpenBLAS cmake search 4 天前
a7943cef features2d: performance optimization of detect function on Windows-ARM64 #27776 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - This PR improves the performance of the detect function on Windows ARM64 targets. - Added the defined(_M_ARM64) macro in agast.cpp and agast_score.cpp files, aligning ARM64 behavior with how x64 selects internal functions for computation. - As a result, ARM64 now executes the same internal functions as x64 where applicable, leading to measurable performance improvements in detect function. - This changes is limited to Windows ARM64 and does not affect other architectures **Performance impact:** - Detect function shows improved runtime on ARM64 targets due to reuse of existing efficient computation paths. <img width="1419" height="408" alt="image" src="https://github.com/user-attachments/assets/feab411a-d256-4bff-bec2-22b2583f63d1" /> 4 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐