Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

@LorenaGdL

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 main(int argc, char** argv) { //lettura immagine Mat background = imread("C:\Users\Fabrizio\Desktop\frame\VIDEO_PER_TESI\Scene1.jpg",CV_LOAD_IMAGE_COLOR); //controllo di avvenuta lettura corretta if(! background.data )
{ cout << "Could not open or find the image" << std::endl ; return -1; } //mostra immagine riferimento imshow("background di riferimento", background); waitKey(1); //vettori per istogrami sfondo Mat hB_b[768]; Mat hB_g[768]; Mat hB_r[768]; //vettori per istogrami immagine Mat hC_b[768];
Mat hC_g[768];
Mat hC_r[768];

/**************variabili necessarie per l'istogrmma da generare*********************************************************************************/
//numero di bins
int histSize = 256;
//range per (b,g,r)
float range[] = { 0, 256 } ;
const float* histRange = { range };
bool uniform = true;
bool accumulate = false;
//**********************************************************************************************************************************************
Mat b_hist_b, g_hist_b, r_hist_b;
Mat b_hist_c, g_hist_c, r_hist_c;
Mat back_hist;
Mat current_hist;

double somma_bkg_b = 0;
double somma_bkg_g = 0;
double somma_bkg_r = 0;

double sommatoria_bkg_b[768];
double sommatoria_bkg_g[768];
double sommatoria_bkg_r[768];

int conteggio = 0;
int conteggio_c = 0;
int cont_hist = 0;

/***********************************************************************************************************************************************/
//*******************inizio raccolta dati di riferimento*****************************************************************************************

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[conteggio], 1, &histSize, &histRange, uniform, accumulate );
        calcHist( &bgr_planes[1], 1, 0, Mat(), hB_g[conteggio], 1, &histSize, &histRange, uniform, accumulate );
        calcHist( &bgr_planes[2], 1, 0, Mat(), hB_r[conteggio], 1, &histSize, &histRange, uniform, accumulate );


        //hB_b[conteggio] = b_hist_b.clone();
        //hB_g[conteggio] = g_hist_b.clone();
        //hB_r[conteggio] = r_hist_b.clone();

        for(int b=0; b<256; b++)
        {
            somma_bkg_b = somma_bkg_b + hB_b[conteggio].at<int>(b);
            somma_bkg_g = somma_bkg_g + hB_g[conteggio].at<int>(b);
            somma_bkg_r = somma_bkg_b + hB_r[conteggio].at<int>(b);
        }
        sommatoria_bkg_b[conteggio] = somma_bkg_b; 
        sommatoria_bkg_g[conteggio] = somma_bkg_g;
        sommatoria_bkg_r[conteggio] = somma_bkg_r;

        conteggio++;
    }
}

//----------------------fine della prima parte (salvataggio dati)--------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------

CvCapture* capture = cvCreateFileCapture("C:\\Users\\Fabrizio\\Desktop\\frame\\VIDEO_PER_TESI\\Scene1.avi");
IplImage* frame;

cout <<"***************************************************************************************** "<< std::endl ;
cout <<"************************inizio parte corrente******************************************** "<< std::endl ;
cout <<"***************************************************************************************** "<< std::endl ;

int A=0;

while(true)
{
    frame = cvQueryFrame(capture); 
    if(!frame) break;      //check for valid frame
    Mat current(frame);
    //---------vettore che serve per ricostruire l'immagine (salvataggio di parti di immagine originali)--------------------------------------
    Mat ricostruzione[768];
    //matrice che serve per generare output modificato
    Mat output = cv::Mat::zeros(240, 320, CV_8UC3);

    //vettori per il salvataggio dei punti iniziali di ricostruzione
    int aa[768];
    int bb[768];

    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[conteggio_c] = SUPPORT_2.clone();

            aa[conteggio_c] = a;
            bb[conteggio_c] = b;

            vector<Mat> bgr_planes_c;
            split( SUPPORT_2, bgr_planes_c );

            calcHist( &bgr_planes_c[0], 1, 0, Mat(), hC_b[conteggio_c], 1, &histSize, &histRange, uniform, accumulate );
            calcHist( &bgr_planes_c[1], 1, 0, Mat(), hC_g[conteggio_c], 1, &histSize, &histRange, uniform, accumulate );
            calcHist( &bgr_planes_c[2], 1, 0, Mat(), hC_r[conteggio_c], 1, &histSize, &histRange, uniform, accumulate );

            conteggio_c++; 
        }
    }

    float f_chi_squared_b[768]; 
    float f_chi_squared_g[768];
    float f_chi_squared_r[768];

    bool f_chi_sq[768];

    Mat mat_Bb, mat_Bg, mat_Br;
    Mat mat_Cb, mat_Cg, mat_Cr;

    float tmp_num_b=0;
    float num_b=0;
    float den_b=0;
    float sum_chi_sq_b = 0;

    float tmp_num_g=0;
    float num_g=0;
    float den_g=0;
    float sum_chi_sq_g = 0;

    float tmp_num_r=0;
    float num_r=0;
    float den_r=0;
    float sum_chi_sq_r = 0;

    for (int t=0; t<768; t++)
    {
        for (int u=0; u<256; u++)
        {
            //seconda formula (BLU)***************************************************
            tmp_num_b = hC_b[t].at<float>(u) - hB_b[t].at<float>(u);
            num_b = /*pow(tmp_num_b,2)*/ tmp_num_b * tmp_num_b;
            den_b = hC_b[t].at<float>(u) + hB_b[t].at<float>(u); 
            if(den_b==0)
                sum_chi_sq_b += 0;
            else
                sum_chi_sq_b += (num_b / den_b);
            //seconda formula (GREEN)*************************************************
            tmp_num_g = hC_g[t].at<float>(u) - hB_b[t].at<float>(u);
            num_g = tmp_num_g * tmp_num_g;
            den_g = hC_g[t].at<float>(u) + hB_b[t].at<float>(u); 
            if(den_g==0)
                sum_chi_sq_g += 0;
            else
                sum_chi_sq_g += (num_g / den_g);
            //seconda formula (RED)***************************************************
            tmp_num_r = hC_r[t].at<float>(u) - hB_r[t].at<float>(u);
            num_r = tmp_num_r * tmp_num_r;
            den_r = hC_r[t].at<float>(u) + hB_r[t].at<float>(u); 
            if(den_r==0)
                sum_chi_sq_r += 0;
            else
                sum_chi_sq_r += (num_r / den_r);                               
        }

        f_chi_squared_b[t] = 2 * sum_chi_sq_b;
        f_chi_squared_g[t] = 2 * sum_chi_sq_g;
        f_chi_squared_r[t] = 2 * sum_chi_sq_r;

        sum_chi_sq_b = 0;
        sum_chi_sq_g = 0;
        sum_chi_sq_r = 0;

//**********************************************************************************************************
//---------REGOLAZIONE SOGLIE------------------------------------------------------------------------------

        if ((f_chi_squared_b[t] <= 250) && (f_chi_squared_g[t] <= 400) && (f_chi_squared_r[t] <= 250))
            f_chi_sq[t] = true;
        else
            f_chi_sq[t] = false;
//----------------------------------------------------------------------------------------------
        if(f_chi_sq[t] == false)
            add_piece_of_frame_red(ricostruzione[t], output, aa[t], bb[t]);
        else
            add_piece_of_frame(ricostruzione[t] , output, aa[t] , bb[t]);
    }
    imshow("current frame", current);
    waitKey(1);

    imshow("output", output);
    waitKey(1);
    cout<<"frame visualizzato n:     "<<A<<endl;
    A++;
}//close while

}

void add_piece_of_frame(const Mat &A , Mat &B, int r, int c) { Rect Roi(r, c, 10, 10); A.copyTo(B(Roi)); }

void add_piece_of_frame_red(const Mat &A, Mat &B, int r, int c) { Rect Roi(r, c, 10, 10); Scalar color = Scalar(0, 0, 255); Mat mask = Mat(10, 10, CV_8UC3, color); addWeighted(A, 0.5, mask, 0.5, 0, B(Roi), CV_8UC3); }

my output is visible but the program does not work for video. How I fix this? thanks you