获取OpenCV Mat中唯一的像素值列表
  OpenCV Mat有相当于np.unique()或bincount()吗?  我正在使用C ++,所以不能只是转换为numpy数组。 
不,那里没有! 不过,您可以编写自己的代码:
std::vector<float> unique(const cv::Mat& input, bool sort = false)
找到单个频道cv :: Mat的独特元素。
参数:
输入:它将被视为是1-D。
排序:对唯一值进行排序(可选)。
  这种功能的实现非常简单,但是,以下仅适用于单通道 CV_32F : 
#include <algorithm>
#include <vector>
std::vector<float> unique(const cv::Mat& input, bool sort = false)
{
    if (input.channels() > 1 || input.type() != CV_32F) 
    {
        std::cerr << "unique !!! Only works with CV_32F 1-channel Mat" << std::endl;
        return std::vector<float>();
    }
    std::vector<float> out;
    for (int y = 0; y < input.rows; ++y)
    {
        const float* row_ptr = input.ptr<float>(y);
        for (int x = 0; x < input.cols; ++x)
        {
            float value = row_ptr[x];
            if ( std::find(out.begin(), out.end(), value) == out.end() )
                out.push_back(value);
        }
    }
    if (sort)
        std::sort(out.begin(), out.end());
    return out;
}
例:
float data[][3] = {
  {  9.0,   3.0,  7.0 },
  {  3.0,   9.0,  3.0 },
  {  1.0,   3.0,  5.0 },
  { 90.0, 30.0,  70.0 },
  { 30.0, 90.0,  50.0 }
};
cv::Mat mat(3, 5, CV_32F, &data);
std::vector<float> unik = unique(mat, true);
for (unsigned int i = 0; i < unik.size(); i++)
    std::cout << unik[i] << " ";
std::cout << std::endl;
输出:
1 3 5 7 9 30 50 70 90 
您可以尝试构建一个柱状图数量等于可能像素值数量的柱状图。
链接地址: http://www.djcxy.com/p/89803.html上一篇: Obtaining list of unique pixel values in OpenCV Mat
下一篇: Get frame from video with libvlc smem and convert it to opencv Mat. (c++)
