Ask Your Question
1

How can we apply threshold to single component of color space model like RGB and LAB?

asked 2018-09-16 14:29:11 -0600

abdur-rehman gravatar image

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

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-09-17 12:07:02 -0600

sjhalayka gravatar image

updated 2018-09-17 12:10:59 -0600

This 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 extractChannel 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;
}
edit flag offensive delete link more

Comments

1

+1 for going the extra mile, and make it a very useful answer !

berak gravatar imageberak ( 2018-09-17 12:18:04 -0600 )edit

Thanks man! Have a good day!

sjhalayka gravatar imagesjhalayka ( 2018-09-17 13:34:57 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-09-16 14:29:11 -0600

Seen: 779 times

Last updated: Sep 17 '18