简体   繁体   English

尝试不使用opencv calcHist()来计算自己的直方图

[英]Trying to compute my own Histogram without opencv calcHist()

What I'm trying to do is writing a function that calculates a Histogram of a greyscale image with a forwarded Number of Bins (anzBin) which the histograms range is divided in. Then I'm running through the Image Pixels compairing their value to the different Bins and in case a value fits, increasing the value of the Bin by 1 我想做的是编写一个函数,该函数计算带有转发的Bins(anzBin)数的直方图范围被划分的灰度图像的直方图。然后,我遍历Image Pixels将其值补偿为不同的Bin,如果值合适,则将Bin的值增加1

   vector<int> calcuHisto(const IplImage *src_pic, int anzBin) 
   {
   CvSize size = cvGetSize(src_pic);
   int binSize = (size.width / 256)*anzBin;
   vector<int> histogram(anzBin,0);

    for (int y = 0; y<size.height; y++) 
    {
         const uchar *src_pic_point =
        (uchar *)(src_pic->imageData + y*src_pic->widthStep);
       for (int x = 0; x<size.width; x++) 
       {
        for (int z = 0; z < anzBin; z++)
        {
            if (src_pic_point[x] <= z*binSize)
            {
                histogram[src_pic_point[x]]++;
            }

        }

    }
}
return histogram;
}

But unfortunately it's not working... What is wrong here? 但不幸的是,它不起作用...这里出了什么问题? Please help 请帮忙

There are a few issues I can see 我可以看到一些问题

  1. Your binSize calculation is wrong 您的binSize计算错误
  2. Your binning algorithm is one sided, and should be two sided 您的分箱算法是单面的,应该是双面的
  3. You aren't incrementing the proper bin when you find a match 找到匹配项时,您没有增加适当的垃圾箱

1. binsize calculation 1. binsize计算

bin size = your range / number of bins

2. two sided binning 2.双面装箱

if (src_pic_point[x] <= z*binSize)

you need a two sided range of values, not a one sided inequality. 您需要一个双向的值范围,而不是单边的不等式。 Imagine you have 4 bins and values from 0 to 255. Your bins should have the following ranges 假设您有4个bin,其值从0到255。您的bin应具有以下范围

bin     low     high
0       0       63.75
1       63.75   127.5
2       127.5   191.25
3       191.25  255

For example: a value of 57 should go in bin 0. Your code says the value goes in all the bins! 例如:值57应该进入bin0。您的代码说该值进入所有bin! Because its always <= z*binsize You need something something with a lower and upper bound. 因为它总是<= z*binsize您需要一些具有上下限的东西。

3. Incrementing the appropriate bin 3.增加适当的垃圾箱

You are using z to loop over each bin, so when you find a match you should increment bin z , you don't use the actual pixel value except when determining which bin it belongs to 您正在使用z遍历每个bin,因此,当找到匹配项时,应该增加bin z ,除非确定其属于哪个bin,否则不要使用实际的像素值

this would likely be buffer overrun imagine again you have 4 bins, and the current pixel has a value of 57. This code says increment bin 57. But you only have 4 bins (0-3) 这可能会导致缓冲区溢出。再次假设您有4个bin,并且当前像素的值为57。此代码显示增量bin57。但是您只有4个bin(0-3)

histogram[src_pic_point[x]]++;

you want to increment only the bin the pixel value falls into 您只想增加像素值所属的bin

histogram[z]++;

CODE
With that in mind here is revised code (it is untested, but should work) 考虑到这一点,这里是修改后的代码(未经测试,但应该可以工作)

vector<int> calcuHisto(const IplImage *src_pic, int anzBin) 
{
    CvSize size = cvGetSize(src_pic);
    double binSize = 256.0 / anzBin;        //new definition
    vector<int> histogram(anzBin,0);        //i don't know if this works so I
                                            //so I will leave it

    //goes through all rows
    for (int y = 0; y<size.height; y++) 
    {
        //grabs an entire row of the imageData
        const uchar *src_pic_point = (uchar *)(src_pic->imageData + y*src_pic->widthStep);

        //goes through each column
        for (int x = 0; x<size.width; x++) 
        {
            //for each bin
            for (int z = 0; z < anzBin; z++)
            {
                //check both upper and lower limits
                if (src_pic_point[x] >= z*binSize && src_pic_point[x] < (z+1)*binSize)
                {
                    //increment the index that contains the point
                    histogram[z]++;
                }
            }
        }
    }
    return histogram;
}

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

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