Welcome everyone to join the image recognition technology Exchange Group: 271891601, in addition, the special welcome Chengdu engaged in image recognition work of friends, my QQ number 248787278
-------------------------------------------
In the process of writing the histogram-defined code, I found that the No. 255 component of the histogram calculated by OpenCV's cvcalchist function is always 0, and the code is as follows:
#include <opencv2/opencv.hpp> #include <opencv2/legacy/compat.hpp> #include <fstream>using namespace Std; #pragma COMMENT (linker, "/subsystem:\" windows\ "/entry:\" Maincrtstartup\ "") cvhistogram* creategrayimagehist ( Iplimage **ppimage) {int nhistsize = 256; This statement indicates there are 256 gray level float frange[] = {0, 255}; The gray scale range is 0 to 255 float *pfranges[] = {Frange}; The next sentence using the Cvcreatehist function requires that the fourth parameter is a pointer to a pointer,//So to do so, as for why to use pointer pointers, I am not clear cvhistogram *pcvhistogram = cvcreatehist (1, & ; nhistsize, Cv_hist_array, pfranges); Before using the histogram to calculate the function cvcalchist,//need to first create cvcalchist the second parameter needs data type Cvcalchist (Ppimage, Pcvhistogram);//Histogram calculation function cvcalchist, Note that the first parameter requirement is a pointer pointer, specifically why this, I am not clear//as to how to take the histogram data stored in the Pcvhistogram, read the following main function to know the return pcvhistogram; } int main () {Iplimage *a = cvloadimage ("Coins.png", cv_load_image_unchanged); Cvhistogram *a_hist = creategrayimagehist (&a);//Creategrayimagehist is a function written by yourself oh int index;double watch_a_hist[256]; for (index = 0; index<256; index++){Watch_a_hist[index] = cvqueryhistvalue_1d (A_hist,index);} return 0;}
and MATLAB running results are compared as follows:
So I wrote a histogram calculation function, tested, error-correct, the code is as follows:
void Mycvcalchist (Iplimage *img,double out_hist[256]) {int i=0, j=0; Double Temp1=0;int temp2=0; const int HIST_SZ = 256;//0 to 255, altogether 256 grayscale values double HIST[HIST_SZ]; memset (hist, 0, sizeof (hist)); for (i = 0; i < img->height; i++) {for (j = 0; J < img->width; J + +) {temp1=cvget2d (img,i,j). val [0];temp2=int (TEMP1);///Type conversion hist[temp2]++;//This implements the number of times the grayscale values in the hist are stored } } memcpy (Out_hist,hist, sizeof (hist)); Someone has to ask, why not use the array name as a parameter to change the value of the real parameter group//This method generally can be, I also tested, but here is not, I estimate with//memset (hist, 0, sizeof (hist)); This statement about}int main () {Iplimage *a = cvloadimage ("Coins.png", cv_load_image_unchanged); Double A_hist[256];mycvcalchist (a,a_hist); return 0;}
Add an array of double types because the histogram data is often involved in floating-point arithmetic, so use this!
Is there a problem with OpenCV2.3 's cvcalchist function? Level 255 is always 0, simply write a histogram calculation function, with source!