首页 > 解决方案 > 我们可以将阈值应用于颜色空间模型的单个组件,如 RGB 和 LAB 吗?

问题描述

试图将 Otsu 阈值应用于 LAB 颜色空间的单个分量“L”。但我不知道如何在 OpenCV 语法上指定它。

标签: c++opencvthreshold

解决方案


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;
}

尝试这个。


推荐阅读