如何在opencv中访问Mat的第n个通道?

时间:2017-07-27 11:47:34

标签: c++ opencv channel

我知道如何使用cv::Mat访问三个频道Vec3b。但现在我有一个n频道cv::Matn不是常数(使用cv::Vec<uchar, n>)。我现在如何访问cv::Mat频道?

2 个答案:

答案 0 :(得分:2)

我们说 <script src="jquery-1.11.2.js"></script> <script> $(document).ready(function () { $('#btnGetAccountList').click(function () { $.ajax({ //url will be yourclassname.svc/yourmethodname url: 'namespaceofGL_AccMainType_GetAllmethod.svc/GL_AccMainType_GetAll', method: 'Get', contentType: "application/json;charset=utf-8", success: function (data) { //your account detail list will be here. }, error: function (err) { alert(err); } }); }); }); </script> ,我们希望访问像素n = 10的{​​{1}}频道。这是一个简单的例子:

4th

希望这会对你有所帮助。请注意,您可以省略(i, j)行,我认为这样更容易。

答案 1 :(得分:0)

为了能够处理任意数量的通道,您可以使用cv::Mat::ptr和一些指针算术。

例如,仅支持CV_8U数据类型的简单方法如下:

#include <opencv2/opencv.hpp>
#include <cstdint>
#include <iostream>

inline uint8_t get_value(cv::Mat const& img, int32_t row, int32_t col, int32_t channel)
{
    CV_DbgAssert(channel < img.channels());
    uint8_t const* pixel_ptr(img.ptr(row, col));
    uint8_t const* value_ptr(pixel_ptr + channel);
    return *value_ptr;
}

void test(uint32_t channel_count)
{
    cv::Mat img(128, 128, CV_8UC(channel_count));
    cv::randu(img, 0, 256);

    for (int32_t i(0); i < img.channels(); ++i) {
        std::cout << i << ":" << get_value(img, 32, 32, i) << "\n";
    }
}

int main()
{
    for (uint32_t i(1); i < 10; ++i) {
        test(i);
    }

    return 0;
}