# histogram comparison

Hi at all! I must do an histogram comparison with a formula that computes the intersection of histograms. My histograms have 256 bins. How I realize this formula correctly? I 'm using this for object tracking with colors.

enter code here


Post below one my possible solution but I think that this not execute exactly the formula

int const block = 768;
//bacground histograms
Mat hB_b[block];
Mat hB_g[block];
Mat hB_r[block];
//currents histogram
Mat hC_b[block];
Mat hC_g[block];
Mat hC_r[block];
float sum_bkg_b = 0;
float sum_bkg_g = 0;
float sum_bkg_r = 0;

float summ_bkg_b[block];
float summ_bkg_g[block];
float summ_bkg_r[block];
for(int a=0; a<320; a+=10)
{
for(int b=0; b<240; b+=10)
{
Mat SUPPORT (background, Rect(a,b,10,10));
vector<Mat> bgr_planes;
split( SUPPORT, bgr_planes );
calcHist( &bgr_planes[0], 1, 0, Mat(), hB_b[count], 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes[1], 1, 0, Mat(), hB_g[count], 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes[2], 1, 0, Mat(), hB_r[count], 1, &histSize, &histRange, uniform, accumulate );
for(int b=0; b<256; b++)
{
sum_bkg_b = sum_bkg_b + hB_b[count].at<int>(b);
sum_bkg_g = sum_bkg_g + hB_g[count].at<int>(b);
sum_bkg_r = sum_bkg_b + hB_r[count].at<int>(b);
}
summ_bkg_b[count] = sum_bkg_b;
summ_bkg_g[count] = sum_bkg_g;
summ_bkg_r[count] = sum_bkg_r;
count++;
}
}
for(int a=0; a<320; a+=10)
{
for(int b=0; b<240; b+=10)
{
Mat SUPPORT_2 (current, Rect(a,b,10,10));
ricostruzione[count_c] = SUPPORT_2.clone();
vector<Mat> bgr_planes_c;
split( SUPPORT_2, bgr_planes_c );
calcHist( &bgr_planes_c[0], 1, 0, Mat(), hC_b[count_c], 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes_c[1], 1, 0, Mat(), hC_g[count_c], 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes_c[2], 1, 0, Mat(), hC_r[count_c], 1, &histSize, &histRange, uniform, accumulate );
double f_intersect_b[block];
double f_intersect_g[block];
double f_intersect_r[block];
float min_Bb = 0;
float min_Bg = 0;
float min_Br = 0;
float min_Cb = 0;
float min_Cg = 0;
float min_Cr = 0;
float min_num_b = 0;
float min_num_g = 0;
float min_num_r = 0;
float min_sum_b = 0;
float min_sum_g = 0;
float min_sum_r = 0;
float pos_min_b = 0;
float sum_min_b = 0;
float pos_min_g = 0;
float sum_min_g = 0;
float pos_min_r = 0;
float sum_min_r = 0;
for (int t=0; t<block; t++)
{
for (int u=0; u<256; u++)
{
//intersect formula (BLU)***************************************************
if (hB_b[t].at<int>(u) <= hC_b[t].at<int>(u))
pos_min_b = hB_b[t].at<int>(u);
else
pos_min_b = hC_b[t].at<int>(u);
sum_min_b += pos_min_b;
//intersect formula (GREEN)*************************************************
if (hB_g[t].at<int>(u) <= hC_g[t].at<int>(u))
pos_min_g = hB_g[t].at<int>(u);
else
pos_min_g = hC_g[t].at<int>(u);
sum_min_g += pos_min_g;
//intersect formula (RED)***************************************************
if (hB_r[t].at<int>(u) <= hC_r[t].at<int>(u))
pos_min_r = hB_r[t].at<int>(u);
else
pos_min_r = hC_r[t].at<int>(u);
sum_mini_r += pos_min_r;
}

f_intersect_b[t] = sum_min_b / summ_bkg_b[t];
f_intersect_g[t] = sum_min_g / summ_bkg_g[t];
f_intersect_r[t] = sum_min_r / summ_bkg_r[t];
}


thanks at all for your time!

edit retag close merge delete

2

just a pro tip: never use your native language for variable names or comments, stick to (whatever broken) english.

( 2016-01-08 03:51:00 -0600 )edit

@berak as you suggested I have modified the code as required. This program executes correctly the formula ? thanks you

( 2016-01-08 04:16:24 -0600 )edit

please check the type() of your histograms, iirc they're usually float, not int, and you have to take the correct type for at()

( 2016-01-08 04:32:36 -0600 )edit

@berak ok if I change int to float the comparison is correct? what do you intend for historams type?

( 2016-01-08 04:39:19 -0600 )edit

@LorenaGdL Good morning! Could you help me on this argument?

( 2016-01-08 05:13:48 -0600 )edit