Ask Your Question
1

Automatic Threshold using Hue channel

asked Oct 14 '13

Dhineshram gravatar image

updated Oct 24 '13

Hi to all, I am doing a project where I have to do Automatic threshold for Hue channel. Threshold should be calculated automatically from histrogram of H channel . At present I am succeed with splitting HSV channel and only taking Hue channel in to the account. Now I dont know how to move further . I searched a lot and found Otsu threshold method do the same work but using greyscale . I am doing programming in C++

thanks in advance

Preview: (hide)

2 answers

Sort by » oldest newest most voted
3

answered Oct 25 '13

stereomatching gravatar image

updated Oct 25 '13

You could use mixChannels too

#include <initializer_list>
#include <iterator>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

inline void mix_channels(cv::Mat const &src, cv::Mat &dst, std::initializer_list<int> from_to)
{
    cv::mixChannels(&src, 1, &dst, 1, std::begin(from_to), from_to.size() / 2);
}

int main()
{
    cv::Mat src = cv::imread("/Users/Qt/program/blogsCodes/pic/marker00.png");
    cv::Mat hsv;
    cv::cvtColor(src, hsv, CV_BGR2HSV);
    cv::Mat hue(src.size(), CV_8U);
    //the third arguments are two number a pair, (0, 0) means copy the data of channels 0(hsv) to channels 0(hue)
    mix_channels(hsv, hue, {0, 0});
    cv::Mat otsuMat;
    cv::threshold(hue, otsuMat, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY);

    cv::imshow("", otsuMat);
    cv::waitKey();

    return 0;
}
Preview: (hide)

Comments

Thanks for the code . Exactly what I wanted to do . Now I am working to remove noice from the output .

Dhineshram gravatar imageDhineshram (Nov 14 '13)edit

How i can remove noise from out put ?

shahzaib_iqbal gravatar imageshahzaib_iqbal (Oct 6 '15)edit
1

answered Oct 24 '13

berak gravatar image

you've been probably doing something like this:

cvtColor( bgr,hsv, CV_RGB2HSV )

now, you want to separate the H channel ( it's the same 1 channel image as a greyscale, actually )

Mat chan[3];
split( hsv, chan );
Mat H = hsv[0];

and threshold that:

Mat binary;
threshold(H, binary, 1 /*ignored for otsu*/, 255, CV_THRESH_OTSU);

note, that there's also adaptiveThreshold, and CLAHE

Preview: (hide)

Comments

Exactly what I wanted to do . Thanks for the Answer .. I get lot of noise in threshold image .

Dhineshram gravatar imageDhineshram (Nov 14 '13)edit

Question Tools

Stats

Asked: Oct 14 '13

Seen: 6,408 times

Last updated: Oct 24 '13