c++ - Trying to compute my own Histogram without opencv calcHist() -
what i'm trying writing function calculates histogram of greyscale image forwarded number of bins (anzbin) histograms range divided in. i'm running through image pixels compairing value different bins , in case value fits, increasing value of 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); (int y = 0; y<size.height; y++) { const uchar *src_pic_point = (uchar *)(src_pic->imagedata + y*src_pic->widthstep); (int x = 0; x<size.width; x++) { (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... wrong here? please help
there few issues can see
- your binsize calculation wrong
- your binning algorithm 1 sided, , should 2 sided
- you aren't incrementing proper bin when find match
1. binsize calculation
bin size = range / number of bins
2. 2 sided binning
if (src_pic_point[x] <= z*binsize)
you need 2 sided range of values, not 1 sided inequality. imagine have 4 bins , values 0 255. bins should have following ranges
bin low high 0 0 63.75 1 63.75 127.5 2 127.5 191.25 3 191.25 255
for example: value of 57
should go in bin 0. code says value goes in bins! because <= z*binsize
need something lower , upper bound.
3. incrementing appropriate bin
you using z
loop on each bin, when find match should increment bin z
, don't use actual pixel value except when determining bin belongs to
this buffer overrun imagine again have 4 bins, , current pixel has value of 57. code says increment bin 57. have 4 bins (0-3)
histogram[src_pic_point[x]]++;
you want increment bin pixel value falls
histogram[z]++;
code
in mind here revised code (it untested, 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 works //so leave //goes through rows (int y = 0; y<size.height; y++) { //grabs entire row of imagedata const uchar *src_pic_point = (uchar *)(src_pic->imagedata + y*src_pic->widthstep); //goes through each column (int x = 0; x<size.width; x++) { //for each bin (int z = 0; z < anzbin; z++) { //check both upper , lower limits if (src_pic_point[x] >= z*binsize && src_pic_point[x] < (z+1)*binsize) { //increment index contains point histogram[z]++; } } } } return histogram; }
Comments
Post a Comment