c++ - 我们可以将阈值应用于颜色空间模型的单个组件,如 RGB 和 LAB 吗?
问题描述
试图将 Otsu 阈值应用于 LAB 颜色空间的单个分量“L”。但我不知道如何在 OpenCV 语法上指定它。
解决方案
C++ 代码将 Lab 图像拆分为单独的通道。
#include <iostream>
using namespace std;
#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world340.lib")
int main(void)
{
Mat img = imread("star.png", 1);
if (img.empty())
{
cout << "Could not read image file." << endl;
return 1;
}
Mat Lab;
Mat Lab_channels[3];
cvtColor(img, Lab, COLOR_BGR2Lab);
split(Lab, Lab_channels);
threshold(Lab_channels[0], Lab_channels[0], 127, 255, THRESH_OTSU);
return 0;
}
此 C++ 代码使用提取通道来获取第一个通道(通道 0)。
#include <iostream>
using namespace std;
#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world340.lib")
int main(void)
{
Mat img = imread("star.png", 1);
if (img.empty())
{
cout << "Could not read image file." << endl;
return 1;
}
Mat Lab;
Mat Lab_channel_0;
cvtColor(img, Lab, COLOR_BGR2Lab);
extractChannel(Lab, Lab_channel_0, 0);
threshold(Lab_channel_0, Lab_channel_0, 127, 255, THRESH_OTSU);
return 0;
}
尝试这个。
推荐阅读
- mysql - MySql Join with Sum 返回错误的输出
- c# - 如何更新cs文件中按钮的样式?
- automator - Mac 和自动化新手
- android - Context.startForegroundService 没有然后调用 Service.startForeground:替代解决方案
- python - 如何修复“ModuleNotFoundError:没有名为 'azure.storage' 的模块”?
- sql-server - 选择表中的特定记录
- excel - 与 Unix for Excel 中的 Head 和 Tail 命令等效的命令
- python - Listdir 路径应该是一个字符串。但我不知道该怎么办?
- algorithm - 就范围大小而言,调用 get(Range) 的 big-O 性能是什么?为什么?
- python - Python Creating a 7zip archive with command line