Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You could use mixChannels too

#include <initializer_list>

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

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