Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

compareHist threshold

Hi at all!!! I'm doing a comparison between two different images but I have a problem to set the threshold for three color: blue, green and red. Could anyone tell me how I set these thresholdes? Below I post my program and I am attaching the two different images. The scope of my program is detect object or people doing a comparison with background image and show parts that are different in red.

include <iostream>

include <stdio.h>

include <math.h>

include <opencv2\opencv.hpp>

include "opencv\highgui.h"

include "opencv\cv.h"

using namespace cv; using namespace std; void add_piece_of_frame(const Mat&, Mat&, int, int); void add_piece_of_frame_red(const Mat&, Mat&, int, int); int const dim = 20; int main(int argc, char* argv) { int const block = 80; //read image Mat background = imread("Scene1.jpg",CV_LOAD_IMAGE_COLOR); //check read image if(! background.data )
{ cout << "Could not open or find the image" << std::endl ; return -1; } //shows reference image imshow("background di riferimento", background); waitKey(1); //vectors for background histograms Mat hB_b[block]; Mat hB_g[block]; Mat hB_r[block]; //vectors for image histograms Mat hC_b[block];
Mat hC_g[block];
Mat hC_r[block]; //
***variables for histograms************************** //number of histogram bins int histSize = 256; //range per (b,g,r) float range[] = { 0, 256 } ; const float histRange = { range }; bool uniform = true; bool accumulate = false; //********************************************** int count = 0; //counter variable for background cycle int count_c = 0; //counter variable for current cycle int nFrame = 0; //counter variable for numerated frames for(int a=0; a<320; a+=dim) { for(int b=40; b<140; b+=dim) { //block Mat SUPPORT (background, Rect(a,b,dim,dim)); //split block to obtain color elements vector<mat> bgr_planes; split( SUPPORT, bgr_planes ); //calculate histograms for each color calcHist( &bgr_planes[0], 1, 0, Mat(), hB_b[count], 1, &histSize, &histRange, uniform, accumulate );//blu calcHist( &bgr_planes[1], 1, 0, Mat(), hB_g[count], 1, &histSize, &histRange, uniform, accumulate );//green calcHist( &bgr_planes[2], 1, 0, Mat(), hB_r[count], 1, &histSize, &histRange, uniform, accumulate );//red count++; } } //VideoCapture cap("tagliato.avi"); Mat current_frame = imread("Scene1_2.jpg",CV_LOAD_IMAGE_COLOR); //while(1) //{ //Mat current_frame; //bool img = cap.read(current_frame); //if(!img) //break; //---------Mats' vector for recostruction image (save parts of original image) --------------------- Mat recostruct[block]; //matrix for modified output //Mat output = cv::Mat::zeros(240, 320, CV_8UC3); Mat output_chi_sq = current_frame.clone(); Mat output_correl = current_frame.clone(); Mat output_intersect = current_frame.clone(); //vettori per il salvataggio dei punti iniziali di ricostruzione int aa[block]; int bb[block]; for(int a=0; a<320; a+=dim) { for(int b=40; b<140; b+=dim) { Mat SUPPORT_2 (current_frame, Rect(a,b,dim,dim)); //save block to recostruction recostruct[count_c] = SUPPORT_2.clone(); //save block's points aa[count_c] = a; bb[count_c] = b; //split current block vector<mat> bgr_planes_c; split( SUPPORT_2, bgr_planes_c ); //calculate histograms for each color calcHist( &bgr_planes_c[0], 1, 0, Mat(), hC_b[count_c], 1, &histSize, &histRange, uniform, accumulate );//blu calcHist( &bgr_planes_c[1], 1, 0, Mat(), hC_g[count_c], 1, &histSize, &histRange, uniform, accumulate );//gren calcHist( &bgr_planes_c[2], 1, 0, Mat(), hC_r[count_c], 1, &histSize, &histRange, uniform, accumulate );//red

            count_c++; 
        }
    }
    //variables to save results
    //method chi square
    double f_chi_square_b[block]; 
    double f_chi_square_g[block];
    double f_chi_square_r[block];
    //method correlation
    double f_correlation_b[block];
    double f_correlation_g[block];
    double f_correlation_r[block];
    //method intersect
    double f_intersect_b[block];
    double f_intersect_g[block];
    double f_intersect_r[block];
    for (int t=0; t<block; t++)
    {
        //chi square with openCV method
        f_chi_square_b[t] = compareHist(hC_b[t], hB_b[t], CV_COMP_CHISQR); //blu comparison
        f_chi_square_g[t] = compareHist(hC_g[t], hB_g[t], CV_COMP_CHISQR); //green comparison
        f_chi_square_r[t] = compareHist(hC_r[t], hB_r[t], CV_COMP_CHISQR); //red comparison
        //correlation with openCV method
        f_correlation_b[t] = compareHist(hC_b[t], hB_b[t], CV_COMP_CORREL); //blu
        f_correlation_g[t] = compareHist(hC_g[t], hB_g[t], CV_COMP_CORREL); //green
        f_correlation_r[t] = compareHist(hC_r[t], hB_r[t], CV_COMP_CORREL); //red
        //intersection with openCV method
        f_intersect_b[t] = compareHist(hC_b[t], hB_b[t], CV_COMP_INTERSECT); //blu
        f_intersect_g[t] = compareHist(hC_g[t], hB_g[t], CV_COMP_INTERSECT); //green
        f_intersect_r[t] = compareHist(hC_r[t], hB_r[t], CV_COMP_INTERSECT); //red
        //----------------------------------------------------------------------------------------------------
        //-------------part of conditions for a possible recostruction frame----------------------------------
        //chi square
        if ((f_chi_square_b[t] < 300) || (f_chi_square_g[t] < 350) || (f_chi_square_r[t] < 300))   //300,300,300/300,350,300 (or)   440,480,430 ((and)
        //if ((f_chi_square_b[t] < 680) && (f_chi_square_g[t] < 730) && (f_chi_square_r[t] < 680))  //600,650,600 (and)
            add_piece_of_frame(recostruct[t], output_chi_sq, aa[t], bb[t]); 
        else
            add_piece_of_frame_red(recostruct[t], output_chi_sq, aa[t], bb[t]);
        //correlation
        if ((f_correlation_b[t] > 0.75) || (f_correlation_g[t] > 0.75) || (f_correlation_r[t] > 0.75))
            add_piece_of_frame(recostruct[t], output_correl, aa[t], bb[t]);
        else
            add_piece_of_frame_red(recostruct[t], output_correl, aa[t], bb[t]);
        //intersection
        if ((f_intersect_b[t] > 280) || (f_intersect_g[t] > 280) || (f_intersect_r[t] > 280))
            add_piece_of_frame(recostruct[t], output_intersect, aa[t], bb[t]);
        else
            add_piece_of_frame(recostruct[t], output_intersect, aa[t], bb[t]);
        //----------------------------------------------------------------------------------------------------
    }
    //show chi square output
    imshow("output CHI SQUARE", output_chi_sq);
    waitKey(1);
    //show correlation output
    imshow("output CORRELATION", output_correl);
    waitKey(1);
    //show intersection output
    imshow("output INTERSECTION", output_intersect);
    waitKey(1);
    //trace of frame number
    cout<<"visualizzated frame number:  " << nFrame << endl;
    nFrame++; //increment for new frame

    //reset counter for new frame
    count_c = 0;
//}//end while
waitKey(0);

}//end main //function for normal recostruction void add_piece_of_frame(const Mat &A , Mat &B, int r, int c) { Rect Roi(r, c, dim, dim); A.copyTo(B(Roi)); } //function for color recostruction void add_piece_of_frame_red(const Mat &A, Mat &B, int r, int c) { Rect Roi(r, c, dim, dim); Scalar color = Scalar(0, 0, 255); Mat mask = Mat(dim, dim, CV_8UC3, color); addWeighted(A, 0.5, mask, 0.5, 0, B(Roi), CV_8UC3); }

image description

image description

thanks at all

click to hide/show revision 2
No.2 Revision

updated 2016-01-27 14:37:15 -0600

berak gravatar image

compareHist threshold

Hi at all!!! I'm doing a comparison between two different images but I have a problem to set the threshold for three color: blue, green and red. Could anyone tell me how I set these thresholdes? Below I post my program and I am attaching the two different images. The scope of my program is detect object or people doing a comparison with background image and show parts that are different in red.

include <iostream>

include <stdio.h>

include <math.h>

include <opencv2\opencv.hpp>

include "opencv\highgui.h"

include "opencv\cv.h"

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <opencv2\opencv.hpp>
#include "opencv\highgui.h"
#include "opencv\cv.h"
using namespace cv;
using namespace std;
void add_piece_of_frame(const Mat&, Mat&, int, int);
void add_piece_of_frame_red(const Mat&, Mat&, int, int);
int const dim = 20;
int main(int argc, char* char** argv)
{
    int const block = 80;
    //read image
    Mat background = imread("Scene1.jpg",CV_LOAD_IMAGE_COLOR);
    //check read image
    if(! background.data ) 
{ cout << "Could not open or find the image" << std::endl ; return -1; } //shows reference image imshow("background di riferimento", background); waitKey(1); //vectors for background histograms Mat hB_b[block]; Mat hB_g[block]; Mat hB_r[block]; //vectors for image histograms Mat hC_b[block];
Mat hC_g[block];
Mat hC_r[block]; //
***variables //**************variables for histograms************************** histograms********************************************************************************* //number of histogram bins int histSize = 256; //range per (b,g,r) float range[] = { 0, 256 } ; const float float* histRange = { range }; bool uniform = true; bool accumulate = false; //********************************************** //********************************************************************************************************************************************** int count = 0; //counter variable for background cycle int count_c = 0; //counter variable for current cycle int nFrame = 0; //counter variable for numerated frames for(int a=0; a<320; a+=dim) { for(int b=40; b<140; b+=dim) { //block Mat SUPPORT (background, Rect(a,b,dim,dim)); //split block to obtain color elements vector<mat> vector<Mat> bgr_planes; split( SUPPORT, bgr_planes ); //calculate histograms for each color calcHist( &bgr_planes[0], 1, 0, Mat(), hB_b[count], 1, &histSize, &histRange, uniform, accumulate );//blu calcHist( &bgr_planes[1], 1, 0, Mat(), hB_g[count], 1, &histSize, &histRange, uniform, accumulate );//green calcHist( &bgr_planes[2], 1, 0, Mat(), hB_r[count], 1, &histSize, &histRange, uniform, accumulate );//red count++; } } //VideoCapture cap("tagliato.avi"); Mat current_frame = imread("Scene1_2.jpg",CV_LOAD_IMAGE_COLOR); //while(1) //{ //Mat current_frame; //bool img = cap.read(current_frame); //if(!img) //break; //---------Mats' vector for recostruction image (save parts of original image) --------------------- Mat recostruct[block]; //matrix for modified output //Mat output = cv::Mat::zeros(240, 320, CV_8UC3); Mat output_chi_sq = current_frame.clone(); Mat output_correl = current_frame.clone(); Mat output_intersect = current_frame.clone(); //vettori per il salvataggio dei punti iniziali di ricostruzione int aa[block]; int bb[block]; for(int a=0; a<320; a+=dim) { for(int b=40; b<140; b+=dim) { Mat SUPPORT_2 (current_frame, Rect(a,b,dim,dim)); //save block to recostruction recostruct[count_c] = SUPPORT_2.clone(); //save block's points aa[count_c] = a; bb[count_c] = b; //split current block vector<mat> vector<Mat> bgr_planes_c; split( SUPPORT_2, bgr_planes_c ); //calculate histograms for each color calcHist( &bgr_planes_c[0], 1, 0, Mat(), hC_b[count_c], 1, &histSize, &histRange, uniform, accumulate );//blu calcHist( &bgr_planes_c[1], 1, 0, Mat(), hC_g[count_c], 1, &histSize, &histRange, uniform, accumulate );//gren calcHist( &bgr_planes_c[2], 1, 0, Mat(), hC_r[count_c], 1, &histSize, &histRange, uniform, accumulate );//red

);//red

                count_c++; 
         }
     }
     //variables to save results
     //method chi square
     double f_chi_square_b[block]; 
     double f_chi_square_g[block];
     double f_chi_square_r[block];
     //method correlation
     double f_correlation_b[block];
     double f_correlation_g[block];
     double f_correlation_r[block];
     //method intersect
     double f_intersect_b[block];
     double f_intersect_g[block];
     double f_intersect_r[block];
     for (int t=0; t<block; t++)
     {
         //chi square with openCV method
         f_chi_square_b[t] = compareHist(hC_b[t], hB_b[t], CV_COMP_CHISQR); //blu comparison
         f_chi_square_g[t] = compareHist(hC_g[t], hB_g[t], CV_COMP_CHISQR); //green comparison
         f_chi_square_r[t] = compareHist(hC_r[t], hB_r[t], CV_COMP_CHISQR); //red comparison
         //correlation with openCV method
         f_correlation_b[t] = compareHist(hC_b[t], hB_b[t], CV_COMP_CORREL); //blu
         f_correlation_g[t] = compareHist(hC_g[t], hB_g[t], CV_COMP_CORREL); //green
         f_correlation_r[t] = compareHist(hC_r[t], hB_r[t], CV_COMP_CORREL); //red
         //intersection with openCV method
         f_intersect_b[t] = compareHist(hC_b[t], hB_b[t], CV_COMP_INTERSECT); //blu
         f_intersect_g[t] = compareHist(hC_g[t], hB_g[t], CV_COMP_INTERSECT); //green
         f_intersect_r[t] = compareHist(hC_r[t], hB_r[t], CV_COMP_INTERSECT); //red
         //----------------------------------------------------------------------------------------------------
         //-------------part of conditions for a possible recostruction frame----------------------------------
         //chi square
         if ((f_chi_square_b[t] < 300) || (f_chi_square_g[t] < 350) || (f_chi_square_r[t] < 300))   //300,300,300/300,350,300 (or)   440,480,430 ((and)
         //if ((f_chi_square_b[t] < 680) && (f_chi_square_g[t] < 730) && (f_chi_square_r[t] < 680))  //600,650,600 (and)
             add_piece_of_frame(recostruct[t], output_chi_sq, aa[t], bb[t]); 
         else
             add_piece_of_frame_red(recostruct[t], output_chi_sq, aa[t], bb[t]);
         //correlation
         if ((f_correlation_b[t] > 0.75) || (f_correlation_g[t] > 0.75) || (f_correlation_r[t] > 0.75))
             add_piece_of_frame(recostruct[t], output_correl, aa[t], bb[t]);
         else
             add_piece_of_frame_red(recostruct[t], output_correl, aa[t], bb[t]);
         //intersection
         if ((f_intersect_b[t] > 280) || (f_intersect_g[t] > 280) || (f_intersect_r[t] > 280))
             add_piece_of_frame(recostruct[t], output_intersect, aa[t], bb[t]);
         else
             add_piece_of_frame(recostruct[t], output_intersect, aa[t], bb[t]);
         //----------------------------------------------------------------------------------------------------
     }
     //show chi square output
     imshow("output CHI SQUARE", output_chi_sq);
     waitKey(1);
     //show correlation output
     imshow("output CORRELATION", output_correl);
     waitKey(1);
     //show intersection output
     imshow("output INTERSECTION", output_intersect);
     waitKey(1);
     //trace of frame number
     cout<<"visualizzated frame number:  " << nFrame << endl;
     nFrame++; //increment for new frame

     //reset counter for new frame
     count_c = 0;
 //}//end while
 waitKey(0);

}//end main //function for normal recostruction void add_piece_of_frame(const Mat &A , Mat &B, int r, int c) { Rect Roi(r, c, dim, dim); A.copyTo(B(Roi)); } //function for color recostruction void add_piece_of_frame_red(const Mat &A, Mat &B, int r, int c) { Rect Roi(r, c, dim, dim); Scalar color = Scalar(0, 0, 255); Mat mask = Mat(dim, dim, CV_8UC3, color); addWeighted(A, 0.5, mask, 0.5, 0, B(Roi), CV_8UC3); }

}

image description

image description

thanks at all