Ask Your Question

Automatic Threshold using Hue channel

asked 2013-10-14 11:03:44 -0600

Dhineshram gravatar image

updated 2013-10-24 16:46:53 -0600

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

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-10-24 18:40:04 -0600

stereomatching gravatar image

updated 2013-10-24 19:10:47 -0600

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

    return 0;
edit flag offensive delete link more


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

Dhineshram gravatar imageDhineshram ( 2013-11-14 09:24:18 -0600 )edit

How i can remove noise from out put ?

shahzaib_iqbal gravatar imageshahzaib_iqbal ( 2015-10-06 04:27:26 -0600 )edit

answered 2013-10-24 17:08:27 -0600

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

edit flag offensive delete link more


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

Dhineshram gravatar imageDhineshram ( 2013-11-14 09:19:00 -0600 )edit

Question Tools


Asked: 2013-10-14 11:03:44 -0600

Seen: 6,337 times

Last updated: Oct 24 '13