0

trying to apply the Otsu threshold to single component "L" of LAB color space. But I can not figure out, how to specify it in OpenCV syntactically.

vimansa
  • 1
  • 3
  • yes you can, split the image to get each cannel, do your thresholding and then merge again – api55 Sep 18 '18 at 05:33
  • You can use the `split()` function to split your image into its three channels, after you change the colorspace to L\*a\*b\*. – alkasm Sep 18 '18 at 07:08

1 Answers1

0

C++ code splits the Lab image into the separate channels.

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

This C++ code uses extract channel to get just the first channel (channel 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;
}

Try this.