简体   繁体   English

获取向量中出现最多的值 <cv::Vec3b> 用C ++

[英]Getting the most occurrence value in a vector<cv::Vec3b> with C++

I am now currently storing my RGB values of the image with a vector <cv::Vec3b> . 我现在正在使用vector <cv::Vec3b>存储图像的RGB值。

To avoid confusion, let me try clarify my question more. 为避免混淆,让我尝试进一步阐明我的问题。 For instance I have a vector of size 10. And the values stored are as shown: 例如,我有一个大小为10的向量。存储的值如下所示:

 Vector address        RGB Values(R,G,B)
    [0][0][0]=         255,255,255
    [1][1][1]=         40,42,40
    [2][2][2]=         40,42,40
    [3][3][3]=         40,42,40
    [4][4][4]=         40,2,60
    [5][5][5]=         9,9,0
    [6][6][6]=         40,2,60
    [7][7][7]=         40,42,40
    [8][8][8]=         255,255,40
    [9][9][9]=         255,255,40

as observed, The most occurring values is (40,42,40). 如图所示,最常出现的值为(40,42,40)。 How do I get these values? 如何获得这些价值? I wish to find the most occurring RGB values of the vector. 我希望找到向量中出现频率最高的RGB值。 Does anybody have any suggestions, code sample on how I can do it? 有人对我的建议有任何建议和代码示例吗? Thanks. 谢谢。

Lets say your 3D array is array[][][] and at some location (x,y) you got R=200, G=100, B=10 then you will increment the value of array[200][100][10] by 1. Lets at some another location also you again get R=200, G=100, B=10 then again you will increment array[200][100][10] by 1 so, now the total value at array[200][100][10] is 2. You will do the same for all pixel location. 假设您的3D数组是array[][][]并且在某个位置(x,y)上您得到R = 200,G = 100,B = 10,那么您将增加array[200][100][10]乘1。在另一个位置,您再次得到R = 200,G = 100,B = 10,然后再次将array[200][100][10]乘以1,因此, array[200][100][10]的总值array[200][100][10]为2。您将对所有像素位置执行相同的操作。

Then, at the end you will find the maximum value in your 3D array and lets say that maximum value is 1000 at array[210][15][10] . 然后,最后在3D数组中找到最大值,可以说在array[210][15][10]处最大值为1000。 This represents that the combination of RG and B which occurs maximum number of times is "R=210, G=15, B=10" 这表示出现最大次数的RG和B的组合为“ R = 210,G = 15,B = 10”

So, in short we can say that value at [ ][ ][ ] tells you , how many times the combination [R][G][B] has occurred. 因此,简而言之,我们可以说[] [] []的值告诉您[R] [G] [B]组合发生了多少次。

I am not very much acquainted with IP. 我对IP不太了解。 If its just a matter of getting most numbers of occurring RGB objects then it can be done using sort() and equal_range() algorithms present in algorithm.h header file. 如果仅是获取最多数量的RGB对象,那么可以使用algorithm.h头文件中存在的sort()和equal_range()算法来完成。 In the below snippet I have taken example of vector instead of RGB type. 在下面的代码段中,我以vector而不是RGB类型为例。 This code snippet can be used for your purpose with some changes to find the count of highest occurring object: 此代码段可用于您的目的,进行一些更改以查找出现次数最高的对象的计数:

    void main() 
{
    vector<int> vecInt;
    vecInt.push_back(0);
    vecInt.push_back(1);
    vecInt.push_back(2);
    vecInt.push_back(3);
    vecInt.push_back(1);
    vecInt.push_back(1);
    vecInt.push_back(1);
    vecInt.push_back(9);
    vecInt.push_back(1);
    vecInt.push_back(0);
    vecInt.push_back(0);

    sort(vecInt.begin(), vecInt.end());
    vector<int>::iterator it;
    it = vecInt.begin();    

    pair<vector<int>::iterator, vector<int>::iterator> pairIter;

    int count = 0;
    for(; it != vecInt.end(); ++it)
    {
            if(it != vecInt.end() )
            {
                pairIter = equal_range(it, vecInt.end(), *it);
            }

        while(pairIter.first != pairIter.second)
        {
        ++count;
        ++(pairIter.first);
        }


    }

}

equal_count(), works on sorted containers and returns a pair of iterators (a range in the sorted container), which contains the value being searched. equal_count(),在已排序的容器上工作,并返回一对迭代器(已排序容器中的范围),其中包含要搜索的值。

pairIter = equal_range(it, vecInt.end(), *it);

Now, we can get the number of items within that pair of iterator using a loop like: 现在,我们可以使用如下循环来获取该对迭代器中的项目数:

            while(pairIter.first != pairIter.second)
        {
        ++count;
        ++(pairIter.first);
        }

With some changes in this code, I think will solve your purpose. 通过对该代码进行一些更改,我认为可以解决您的目的。

I think that you can define a three dimensional array and you can do it as mentioned here: http://www.cplusplus.com/forum/articles/7459/ 我认为您可以定义三维数组,并且可以按照此处的说明进行操作: http : //www.cplusplus.com/forum/articles/7459/

then extract the values of pixel ( Veb3b pixelValue; )at each location of your image and then based upon the R,G and B values ( rValue = pixelValue[0]; , gValue = pixelValue[1]; , bValue = pixelValue[2]; ) increment the corresponding bin of your 3D array 然后在图像的每个位置提取像素值( Veb3b pixelValue; ),然后基于R,G和B值( rValue = pixelValue[0];gValue = pixelValue[1];bValue = pixelValue[2]; )增加3D数组的相应bin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM