void exportHistograms(){ TreeReader reader; reader.openSeries(257,"D"); reader.openSeries(258,"D"); reader.openSeries(260,"D"); reader.openSeries(261,"D"); reader.openSeries(262,"D"); TH1F hist1("hist1","hist1",100,0,30000); TH1F hist2("hist2","hist2",100,0,30000); TH1F hist3("hist3","hist3",100,0,30000); for(Long64_t i=0;i<reader.tree->GetEntries();i++){ reader.tree->GetEntry(i); if(fabs(reader.recon.t_gamma[2]-600)<10){ hist1.Fill(reader.recon.E_gamma[2]); } if(fabs(reader.recon.t_gamma[6]-600)<10){ hist2.Fill(reader.recon.E_gamma[6]); } if(fabs(reader.recon.t_gamma[13]-600)<10){ hist3.Fill(reader.recon.E_gamma[13]); } } ofstream output(Form("%s/output.csv",RDKANAL)); output<<"bin center,det02,det06,det16"<<endl; for(Long64_t i=1;i<=100;i++){ output<<hist1.GetBinCenter(i)<<","<<hist1.GetBinContent(i)<<","; output<<hist2.GetBinContent(i)<<","<<hist3.GetBinContent(i)<<endl; } output.close(); }
TEST(Histogram, General) { Histogram hist1(-1.0, 1.0, 10); EXPECT_EQ(10, hist1.numBins()); EXPECT_EQ(-1.0, hist1.minValue()); EXPECT_EQ(1.0, hist1.maxValue()); EXPECT_EQ(7, hist1.valueToBin(hist1.binToValue(7))); EXPECT_EQ(8, hist1.valueToBin(R(0.79))); EXPECT_EQ(9, hist1.valueToBin(R(0.81))); EXPECT_EQ(9, hist1.valueToBin(R(0.99))); EXPECT_EQ(9, hist1.valueToBin(R(1.0))); for (size_t i = 0; i < 10; ++i) EXPECT_EQ(0, hist1[i]); hist1.insert(R(1e-6)); EXPECT_EQ(1, hist1[5]); EXPECT_EQ(1, hist1.numEntries()); hist1.insert(R( 0.1)); hist1.insert(R(-0.1)); EXPECT_EQ(3, hist1.numEntries()); EXPECT_EQ(5, hist1.mode()); EXPECT_DOUBLE_EQ(0.1 / 3.0, hist1.mean()); }
int KMeansClustering::GetClusterID( std::vector<float> cur ) const { int curC = -1; float curD = FLT_MAX; for(int j = 0; j < clusterN; j++){ float d = 0; if( dist_type == KM_L2Norm ){ d = SCI::L2Norm( cur, cluster[j] ); } if( dist_type == KM_Pearson ){ d = 1.0f - SCI::PearsonCorrelation( cur, cluster[j] ); } if( dist_type == KM_Histogram ){ SCI::SimpleHistogram hist0( 5, histogram_min, histogram_max ); SCI::SimpleHistogram hist1( 5, histogram_min, histogram_max ); hist0.AddValues(cur); hist1.AddValues(cluster[j]); d = 0; for(int i = 0; i < 5; i++){ d += (float)abs( hist0.GetBin(i)-hist1.GetBin(i) ); } return d; } if( d < curD ){ curD = d; curC = j; } } return curC; }
void CompareChiDialog::startMining() { QString logsDir = medianFilter->isChecked() ? "/CleanLogs" : "/Logs"; QDir logs_dir = QApplication::applicationDirPath(); logs_dir = logs_dir.path() + logsDir; logs_dir = logs_dir.path() + "/" + this->userNameEdit1->text(); if (this->userNameEdit1->text().isEmpty() || this->userNameEdit2->text().isEmpty()) { QMessageBox::critical(this, tr("Error"), tr("Usernames cannot be empty!")); return; } if (!logs_dir.exists()) { QMessageBox::critical(this, tr("Error"), tr("Can't find logs for user \"") + this->userNameEdit1->text() + tr("\" in the directory ") + logs_dir.path()); return; } logs_dir = QApplication::applicationDirPath(); logs_dir = logs_dir.path() + logsDir; logs_dir = logs_dir.path() + "/" + this->userNameEdit2->text(); if (!logs_dir.exists()) { QMessageBox::critical(this, tr("Error"), tr("Can't find logs for user \"") + this->userNameEdit2->text() + tr("\" in the directory ") + logs_dir.path()); return; } if (theresoldCut->isChecked()){ Histogram hist1 (this->steps->value(), this->maxValue->value(), this->userNameEdit1->text(), medianFilter->isChecked()); int end1 = hist1.findMinGoodEnd(); Histogram hist2 (this->steps->value(), this->maxValue->value(), this->userNameEdit2->text(), medianFilter->isChecked()); int end2 = hist2.findMinGoodEnd(); this->maxValue->setValue(end1 < end2 ? end2 : end1); } long long int info = Mining::useChiSquareApprox(this->userNameEdit1->text(), this->userNameEdit2->text(), this->steps->value(), this->maxValue->value(), medianFilter->isChecked()); QMessageBox::information(this, "Mining complete", "The probability that it is one person is " + QString::number(info) + "%"); }
template<class Storage1, class Storage2> static int foaOperations(Storage1, Storage2) { const int SIZE = 720; typedef Storage1::value_type Val; typedef Storage1::idx_type Idx; CVHistogram<Storage1> hist1( SIZE ); CVHistogram<Storage2> hist2( SIZE ); int Errors = 0; for( Idx i = 0; i < SIZE; i++ ) { hist1[i] = (Val)(i % 10); hist2[i] = (Val)(9 - hist1[i]); } if( calc_histogram_intersection( hist1, hist2 ) != SIZE * 2 ) { Errors++; trsWrite( ATS_CON | ATS_LST, "Error Intersection function\n" ); } double res; if( (res = fabs(calc_histogram_chi_square( hist1, hist2 ) - SIZE * 330 / 90)) > 0.0001 ) { Errors++; trsWrite( ATS_CON | ATS_LST, "Error ChiSqr function\n" ); } for( i = 0; i < SIZE; i++ ) { hist1[i] = (Val)(i % 9); hist2[i] = (Val)(8 - hist1[i]); } if( fabs(calc_histogram_correlation( hist1, hist2 ) + 1 ) >= 0.0001 ) { Errors++; trsWrite( ATS_CON | ATS_LST, "Error Correl function\n" ); } return Errors == 0 ? TRS_OK : trsResult( TRS_FAIL, "Fixed %d errors", Errors ); }
float KMeansClustering::GetDistance( int id ) const { std::vector<float> cur = pdata->GetElement( id ); std::vector<float> clu = cluster[ clusterID[id] ]; if( dist_type == KM_L2Norm ){ return SCI::L2Norm( cur, clu ); } if( dist_type == KM_Pearson ){ return 1.0f - SCI::PearsonCorrelation( cur, clu ); } if( dist_type == KM_Histogram ){ SCI::SimpleHistogram hist0( 5, histogram_min, histogram_max ); SCI::SimpleHistogram hist1( 5, histogram_min, histogram_max ); hist0.AddValues(cur); hist1.AddValues(clu); float diff = 0; for(int i = 0; i < 5; i++){ diff += (float)abs( hist0.GetBin(i)-hist1.GetBin(i) ); } return diff; } return 0; }
int test_calculate_histogram() { // Blog: http://blog.csdn.net/fengbingchun/article/details/76417598 const int length{ 10 * 1024 * 1024 }; // 100MB std::unique_ptr<unsigned char[]> data(new unsigned char[length]); generator_random_number<unsigned char>(data.get(), length, 0, 255); const int hist_size{ 256 }; std::unique_ptr<unsigned int[]> hist1(new unsigned int[hist_size]), hist2(new unsigned int[hist_size]); std::for_each(hist1.get(), hist1.get() + hist_size, [](unsigned int& n) {n = 0; }); std::for_each(hist2.get(), hist2.get() + hist_size, [](unsigned int& n) {n = 0; }); float elapsed_time1{ 0.f }, elapsed_time2{ 0.f }; // milliseconds unsigned int value1{ 0 }, value2{ 0 }; int ret = calculate_histogram_cpu(data.get(), length, hist1.get(), value1, &elapsed_time1); if (ret != 0) PRINT_ERROR_INFO(calculate_histogram_cpu); ret = calculate_histogram_gpu(data.get(), length, hist2.get(), value2, &elapsed_time2); if (ret != 0) PRINT_ERROR_INFO(calculate_histogram_gpu); if (value1 != value2) { fprintf(stderr, "their values are different: val1: %d, val2: %d\n", value1, value2); return -1; } for (int i = 0; i < hist_size; ++i) { if (hist1[i] != hist2[i]) { fprintf(stderr, "their values are different at: %d, val1: %d, val2: %d\n", i, hist1[i], hist2[i]); return -1; } } fprintf(stderr, "test calculate histogram: cpu run time: %f ms, gpu run time: %f ms\n", elapsed_time1, elapsed_time2); return 0; }
void whichone(char **arg,int len) { int i; int bg = 0; int pipe = 0; int redir = 0; if(arg[0] == NULL) { fprintf(stderr,"Invalid Command\n"); } for(i=0; i<len; i++) if((strcmp(arg[i],"<") == 0) || (strcmp(arg[i],">") == 0)) redir = 1; for(i=0; i<len; i++) if(strcmp(arg[i],"|") == 0) pipe = 1; if(arg[len-1][strlen(arg[len-1])-1] == '&') { bg = 1; if(arg[len-1][0] == '&') arg[len-1] = NULL; else arg[len-1][strlen(arg[len-1])-1] = '\0'; } if(pipe && !redir) { pipe_func(arg,len); } else if(!pipe && redir) { redir_func(arg,len); } else if(pipe && redir) { redir_pipe_func(arg,len); } else if(strcmp(arg[0],"cd") == 0) { cd(arg,len); } else if(strcmp(arg[0],"pid") == 0) { pidx(arg,len); } else if(arg[0][0]=='h' && arg[0][1]=='i' && arg[0][2]=='s' && arg[0][3]=='t') { hist(arg,len); } else if(arg[0][0]=='!' && arg[0][1]=='h' && arg[0][2]=='i' && arg[0][3]=='s' && arg[0][4]=='t') { hist1(arg,len); } else { other(arg,len,bg); } }
// MOD3<-function(Ng.out,Ng.in,STin,STout,max.con,Cg=0) MATRICEi *mod3(MATRICEi *ris, int Ng_out, int Ng_in, const VETTOREd *STin, const VETTOREd *STout, int max_con, double Cg) { int N, j, n_reg, L, Ls, ns; double S, tmpd; int aus; #ifdef MDEBUG GString *tmp = NULL; #endif _Intestazione("\n***mod3***\n"); #ifdef DET fprintf(fp_det, "input:\n"); fprintf(fp_det, "\tNg_out = %d\n", Ng_out); fprintf(fp_det, "\tNg_in = %d\n", Ng_in); fprintf(fp_det, "\tSTin = "); _StampaRawVett_d(STin); fprintf(fp_det, "\tSTout = "); _StampaRawVett_d(STout); fprintf(fp_det, "\tmax_con = %d\n", max_con); fprintf(fp_det, "\tCg = %.16g\n", Cg); #endif /* #ifdef FDEBUG fprintf(fp_fdbg, "Ng_out = %d\n", Ng_out); fprintf(fp_fdbg, "Ng_in = %d\n", Ng_in); _StampaVett_d(STin); _StampaVett_d(STout); fprintf(fp_fdbg, "max_con = %d\nCg = %.5e\n", max_con, Cg); fprintf(fp_fdbg, "\n"); fprintf(fp_fdbg, "*****************************************\n\n"); #endif*/ CREAv_d(g_scalare_d, 1); CREAv_i(g_scalare_i, 1); // ris<-matrix(0,Ng.out+Ng.in,Ng.out+Ng.in) CREAm_i(ris, Ng_out + Ng_in, Ng_out + Ng_in); InitMatr_i(ris, 0); // N<-Ng.in+Ng.out N = Ng_in + Ng_out; // S<-sum(STout[2:(Ng.in+1)],na.rm=TRUE) g_tmp1_d = segmento_v_d(g_tmp1_d, STout, 2, Ng_in + 1); S = somma_v_d(g_tmp1_d, true); // if (S>0) g_p<-STout[2:(Ng.in+1)]/S if (S > 0.0) { g_p = segmento_v_d(g_p, STout, 2, Ng_in + 1); dividi1_vs_d(g_p, S); } // else g_p<-rep(1/Ng.in,Ng.in) else { #ifdef MDEBUG if (Ng_in == 0) { CREAstr(tmp, ""); g_string_printf(tmp, "ATTENZIONE (mod3.c, linea 53): divisione per zero!\n"); warning(tmp->str); fprintf(fp_fdbg, tmp->str); CANCELLAstr(tmp); } #endif CREAv_d(g_p, Ng_in); InitVett_d(g_p, (double) 1.0 / Ng_in); } // M.out<-sampleB(seq(1,Ng.in,1),Ng.out,prob=g_p,replace=TRUE) g_tmp1_i = seq_i(g_tmp1_i, 1, Ng_in, 1); g_M_out = sampleB_p(g_M_out, g_tmp1_i, Ng_out, 1, g_p); g_tmpSTin = copia_v_d(g_tmpSTin, STin, 1, LENGTHv_d(STin)); g_tmpSTout = copia_v_d(g_tmpSTout, STout, 1, LENGTHv_d(STout)); // while (sum(M.out)<(N-1)) # rm.na == FALSE while (somma_v_i(g_M_out, false) < N - 1) { // {if (S>0) if (S > 0.0) { // {STout<-(STout[1:(Ng.in+1)]/S)*Ng.in segmento1_v_d(g_tmpSTout, 1, Ng_in + 1); #ifdef MDEBUG if (Ng_in == 0) { CREAstr(tmp, ""); g_string_printf(tmp, "ATTENZIONE (mod3.c, linea 74): divisione per zero!\n"); warning(tmp->str); fprintf(fp_fdbg, tmp->str); CANCELLAstr(tmp); } #endif dividi1_vs_d(g_tmpSTout, (double) S / Ng_in); // aus<-max(M.out)+1 aus = max_v_i(g_M_out) + 1; // Freq.out<-hist(M.out,breaks=seq(0,Ng.in+1,1),right=FALSE,plot=FALSE)$counts/N g_tmp2_i = seq_i(g_tmp2_i, 0, Ng_in + 1, 1); g_tmp1_i = hist1(g_tmp1_i, g_M_out, g_tmp2_i, 0, 1, 0); g_Freq_out = dividi_vs_i(g_Freq_out, g_tmp1_i, (double) N); // g_Sc<-Score(S=M.out,ST=STout,Freq=Freq.out,n=1,toll=rep(Inf,(Ng.out+1))) CREAv_d(g_tmp3_d, Ng_out + 1); InitVett_d(g_tmp3_d, R_PosInf); g_Sc = score1(g_Sc, g_M_out, g_tmpSTout, g_Freq_out, 1, g_tmp3_d); // g_indok<-which(g_Sc!=-Inf) g_indok = which_v_indxne_d(g_indok, g_Sc, R_NegInf); // g_indInf <- setdiff(seq(1, Ng.out, 1), g_indok) g_tmp3_i = seq_i(g_tmp3_i, 1, Ng_out, 1); g_indInf = setdiff_i(g_indInf, g_tmp3_i, g_indok); // g_Sc[g_indInf]<-min(c(0,g_Sc[g_indok]))-1 g_tmp1_d = assegna_v_indx_d(g_tmp1_d, g_Sc, g_indok); tmpd = min_v_d(g_tmp1_d); if (tmpd < 0.0) tmpd = 0.0; assegna1_v_indx_d(g_Sc, g_indInf, tmpd - 1); // g_p<-g_Sc/sum(g_Sc) g_p = dividi_vs_d(g_p, g_Sc, somma_v_d(g_Sc, false)); // } } // else g_p<-rep(1/Ng.in,Ng.in) else { #ifdef MDEBUG if (Ng_in == 0) { CREAstr(tmp, ""); g_string_printf(tmp, "ATTENZIONE (mod3.c, linea 111): divisione per zero!\n"); warning(tmp->str); fprintf(fp_fdbg, tmp->str); CANCELLAstr(tmp); } #endif CREAv_d(g_p, Ng_in); InitVett_d(g_p, (double) 1.0 / Ng_in); } // g_ind.M<-sampleB(seq(1,Ng.out,1),1,prob=g_p) g_tmp2_i = seq_i(g_tmp2_i, 1, Ng_out, 1); g_ind_M = sampleB_p(g_ind_M, g_tmp2_i, 1, 0, g_p); // M.out[g_ind.M]<-M.out[g_ind.M]+1 ASSEGNAv_i(g_M_out, ACCEDIv_i(g_ind_M, 1), ACCEDIv_i(g_M_out, ACCEDIv_i(g_ind_M, 1)) + 1); } // for (j in (1:Ng.out)) for (j = 1; j <= Ng_out; j++) { // {n.reg<-M.out[j] n_reg = ACCEDIv_i(g_M_out, j); // if (j==1) if (j == 1) { // {ris[1:n.reg,(j+Ng.in)]<-1 g_tmp1_i = seq_i(g_tmp1_i, 1, n_reg, 1); ASSEGNAv_i(g_scalare_i, 1, j + Ng_in); assegna1_m_vv_i(ris, g_tmp1_i, g_scalare_i, 1); // g_indS<-1:n.reg g_indS = seq_i(g_indS, 1, n_reg, 1); // g_indBS<-setdiff(1:Ng.in,g_indS) g_tmp2_i = seq_i(g_tmp2_i, 1, Ng_in, 1); g_indBS = setdiff_i(g_indBS, g_tmp2_i, g_indS); // } } // else else { // {L<-length(g_indBS) L = LENGTHv_i(g_indBS); // Ls<-min(L,n.reg-1) Ls = min_s_i(L, n_reg - 1); // ns<-n.reg-Ls ns = n_reg - Ls; // S<-sum(STin[1:(Ng.out+1)],na.rm=TRUE) g_tmp1_d = segmento_v_d(g_tmp1_d, g_tmpSTin, 1, Ng_out + 1); S = somma_v_d(g_tmp1_d, true); // g_Sin<-apply(ris,1,sum) g_Sin = somma_righe_i(g_Sin, ris); // if (S>0) if (S > 0.0) { // {STin<-(STin[1:(Ng.out+1)]/S)*Ng.out segmento1_v_d(g_tmpSTin, 1, Ng_out + 1); #ifdef MDEBUG if (Ng_out == 0) { CREAstr(tmp, ""); g_string_printf(tmp, "ATTENZIONE (mod3.c, linea 163): divisione per zero!\n"); warning(tmp->str); fprintf(fp_fdbg, tmp->str); CANCELLAstr(tmp); } #endif dividi1_vs_d(g_tmpSTin, (double) S / Ng_out); // aus<-max(g_Sin)+1 aus = max_v_i(g_Sin) + 1; // Freq.in<-hist(g_Sin,breaks=seq(0,aus,1),right=FALSE,plot=FALSE)$counts/N g_tmp2_i = seq_i(g_tmp2_i, 0, aus, 1); g_tmp1_i = hist1(g_tmp1_i, g_Sin, g_tmp2_i, 0, 1, 0); g_Freq_in = dividi_vs_i(g_Freq_in, g_tmp1_i, (double) N); // g_Sc<-Score(S=g_Sin[g_indS],ST=STin,Freq=Freq.in,n=1,toll=rep(Inf,(Ng.out+1))) g_tmp2_i = assegna_v_indx_i(g_tmp2_i, g_Sin, g_indS); CREAv_d(g_tmp3_d, Ng_out + 1); InitVett_d(g_tmp3_d, R_PosInf); g_Sc = score1(g_Sc, g_tmp2_i, g_tmpSTin, g_Freq_in, 1, g_tmp3_d); // g_indok<-which(g_Sc!=-Inf) g_indok = which_v_indxne_d(g_indok, g_Sc, R_NegInf) ; // g_indInf <- setdiff(seq(1, length(g_indS), 1), g_indok) g_tmp1_i = seq_i(g_tmp1_i, 1, LENGTHv_i(g_indS), 1); g_indInf = setdiff_i(g_indInf, g_tmp1_i, g_indok); // g_Sc[g_indInf]<-min(c(0,g_Sc[g_indok]))-1 g_tmp1_d = assegna_v_indx_d(g_tmp1_d, g_Sc, g_indok); tmpd = min_v_d(g_tmp1_d); if (tmpd < 0.0) tmpd = 0.0; assegna1_v_indx_d(g_Sc, g_indInf, tmpd - 1); // g_p.sc<-g_Sc/sum(g_Sc) g_p_sc = dividi_vs_d(g_p_sc, g_Sc, somma_v_d(g_Sc, false)); // } } // else g_p.sc<-rep(1/length(g_indS),length(g_indS)) else { #ifdef MDEBUG if (LENGTHv_i(g_indS) == 0) { CREAstr(tmp, ""); g_string_printf(tmp, "ATTENZIONE (mod3.c, linea 201): divisione per zero!\n"); warning(tmp->str); fprintf(fp_fdbg, tmp->str); CANCELLAstr(tmp); } #endif CREAv_d(g_p_sc, LENGTHv_i(g_indS)); InitVett_d(g_p_sc, (double) 1.0 / LENGTHv_i(g_indS)); } // // g_ind<-which(g_Sin[g_indS]==max.con) g_ind = which_v_indxeq_i(g_ind, g_tmp1_i, max_con); // g_p.sc[g_ind]<-0 assegna1_vs_indx_d(g_p_sc, g_ind, 0.0); // g_p.sc<-g_p.sc/sum(g_p.sc) g_p = dividi_vs_d(g_p, g_p_sc, somma_v_d(g_p_sc, false)); // g_ind1<-sampleB(g_indS,ns,prob=g_p.sc) g_ind1 = sampleB_p(g_ind1, g_indS, ns, 0, g_p_sc); // if (Ls>0) if (Ls > 0) { // {g_ind<-g_indBS[1:Ls] g_ind = segmento_v_i(g_ind, g_indBS, 1, Ls); // g_indS<-c(g_indS,g_ind) g_indS = accoda1_vv_i(g_indS, g_ind); // g_indBS<-setdiff(1:Ng.in,g_indS) g_tmp1_i = seq_i(g_tmp1_i, 1, Ng_in, 1); g_indBS = setdiff_i(g_indBS, g_tmp1_i, g_indS); // g_ind1<-c(g_ind1,g_ind) g_ind1 = accoda1_vv_i(g_ind1, g_ind); // } } // m[g_ind1,(j+Ng.in)]<-1 ASSEGNAv_i(g_scalare_i, 1, j + Ng_in); assegna1_m_vv_i(ris, g_ind1, g_scalare_i, 1); // } } // } } // if ( (Cg>0)&((Ng.in+Ng.out)>2) ) ris<-triangola(M=ris,Cg=Cg,max.con) if (Cg > 0.0 && Ng_in + Ng_out > 2) ris = triangola(ris, Cg, max_con); // return(ris) //~ CANCELLAv_i(g_M_out); //~ CANCELLAv_i(g_indok); //~ CANCELLAv_i(g_tmp3_i); //~ CANCELLAv_d(g_tmp1_d); //~ CANCELLAv_d(g_Freq_out); //~ CANCELLAv_d(g_tmp3_d); //~ CANCELLAv_d(g_p); //~ CANCELLAv_d(g_Sc); //~ CANCELLAv_i(g_tmp2_i); //~ CANCELLAv_i(g_ind_M); //~ CANCELLAv_i(g_ind1); //~ CANCELLAv_i(g_indS); //~ CANCELLAv_i(g_indBS); //~ CANCELLAv_i(g_Sin); //~ CANCELLAv_i(g_ind); //~ CANCELLAv_i(g_indInf); //~ CANCELLAv_i(g_tmp1_i); //~ CANCELLAv_i(g_scalare_i); //~ CANCELLAv_d(g_scalare_d); //~ CANCELLAv_d(g_p_sc); #ifdef FDEBUG fprintf(fp_fdbg, "\n\n"); fprintf(fp_fdbg, "*****************************************\n"); fprintf(fp_fdbg, "*****************************************\n\n"); #endif StrBilanciam(); #ifdef DET fprintf(fp_det, "mod3 output:\n"); fprintf(fp_det, "\tm = "); _StampaRawMatr_i(ris); #endif return ris; // } }
bool brightRGB::getMedian(const image& img,dvector& dest) const{ // image empty? if (img.empty()) { setStatusString("image empty"); dest.resize(0); return false; } const rgbPixel transColor = getParameters().transColor; dest.resize(3); ivector hist0(256,0); ivector hist1(256,0); ivector hist2(256,0); image::const_iterator it = img.begin(); if(getParameters().transparent) { while(it != img.end()) { if(*it != transColor) { ++hist0.at((*it).getRed()); ++hist1.at((*it).getGreen()); ++hist2.at((*it).getBlue()); } it++; } const int counterHalf = hist0.sumOfElements()/2; // check for complete image transparent if (counterHalf==0) { setStatusString("only transparent pixels"); dest.resize(0); return false; } int i,s; i=-1,s=0; while(++i<256 && s<counterHalf) { s += hist0.at(i); } dest.at(0) = i-1; i=-1,s=0; while(++i<256 && s<counterHalf) { s += hist1.at(i); } dest.at(1) = i-1; i=-1,s=0; while(++i<256 && s<counterHalf) { s += hist2.at(i); } dest.at(2) = i-1; } else { // no transparent color while(it != img.end()) { ++hist0.at((*it).getRed()); ++hist1.at((*it).getGreen()); ++hist2.at((*it).getBlue()); it++; } const int counterHalf = img.columns()*img.rows()/2; int i,s; i=-1,s=0; while(++i<256 && s<counterHalf) { s += hist0.at(i); } dest.at(0) = i-1; i=-1,s=0; while(++i<256 && s<counterHalf) { s += hist1.at(i); } dest.at(1) = i-1; i=-1,s=0; while(++i<256 && s<counterHalf) { s += hist2.at(i); } dest.at(2) = i-1; } // normalize to 0..1 dest.divide(255); return true; };
int main( int argc, char* argv[]) { if( argc != 3) { std::cerr << "Usage: "<<argv[0]<<" [input.nc] [output.nc]\n"; return -1; } std::cout << argv[1]<< " -> "<<argv[2]<<std::endl; //---------------- const unsigned Nhist = 50; const unsigned nhist = 1; const unsigned Ninput =100; const double Nsigma =4.; std::vector<double> input1(Ninput,0.); std::vector<double> input2(Ninput,0.); thrust::random::minstd_rand generator; thrust::random::normal_distribution<double> d1; thrust::random::normal_distribution<double> d2; std::vector<double> rand1(Ninput,0.); std::vector<double> rand2(Ninput,0.); for (unsigned i=0;i<rand1.size();i++) { rand1[i] = d1(generator); } for (unsigned i=0;i<rand2.size();i++) { rand2[i] = d2(generator); } for (unsigned i=0;i<input1.size();i++) { double t = (double)(i/(input1.size()-1)); double omega1 =2.*M_PI* 20.; input1[i] = (rand1[i]*0.1*cos( omega1*t)+1.); } for (unsigned i=0;i<input2.size();i++) { double t = (double)(i/(input2.size()-1)); //double omega1 = 2.*M_PI*20.; double omega2= 2.*M_PI*30.; //double phase = 0.5*M_PI; // input2[i] =input1[i]; //perfectly correlated // input2[i] = (-rand1[i]*0.1*cos(omega1*t)+1.);//perfectly anticorrelated // input2[i] = (rand2[i]*0.001*cos(omega2*t)+3.);//perfectly uncorrelated input2[i] = (rand2[i]*0.001*cos(omega2*t)+3.);//uncorrelated } //normalize grid and compute sigma NormalizeToFluc(input1); NormalizeToFluc(input2); dg::Grid1d g1d1(-Nsigma,Nsigma, nhist, Nhist,dg::DIR); dg::Grid1d g1d2(-Nsigma,Nsigma, nhist, Nhist,dg::DIR); dg::Grid2d g2d( -Nsigma,Nsigma,-Nsigma,Nsigma, nhist, Nhist,Nhist,dg::DIR,dg::DIR); dg::Histogram<dg::HVec> hist1(g1d1,input1); dg::Histogram<dg::HVec> hist2(g1d2,input2); dg::Histogram2D<dg::HVec> hist12(g2d,input1,input2); dg::HVec PA1 = dg::evaluate(hist1,g1d1); dg::HVec A1 = dg::evaluate(dg::cooX1d,g1d1); dg::HVec PA2= dg::evaluate(hist2,g1d2); dg::HVec A2 = dg::evaluate(dg::cooX1d,g1d2); dg::HVec PA1A2= dg::evaluate(hist12,g2d); //-----------------NC output start int dataIDs1[2],dataIDs2[2],dataIDs12[1]; int dim_ids1[1],dim_ids2[1],dim_ids12[2]; int ncid; file::NC_Error_Handle err; err = nc_create(argv[2],NC_NETCDF4|NC_CLOBBER, &ncid); //plot 1 err = file::define_dimension( ncid,"A1_", &dim_ids1[0], g1d1); err = nc_def_var( ncid, "P(A1)", NC_DOUBLE, 1, &dim_ids1[0], &dataIDs1[0]); err = nc_def_var( ncid, "A1", NC_DOUBLE, 1, &dim_ids1[0], &dataIDs1[1]); err = nc_enddef( ncid); err = nc_put_var_double( ncid, dataIDs1[0], PA1.data() ); err = nc_put_var_double( ncid, dataIDs1[1], A1.data() ); err = nc_redef(ncid); //plot 2 err = file::define_dimension( ncid,"A2_", &dim_ids2[0], g1d2); err = nc_def_var( ncid, "P(A2)", NC_DOUBLE, 1, &dim_ids2[0], &dataIDs2[0]); err = nc_def_var( ncid, "A2", NC_DOUBLE, 1, &dim_ids2[0], &dataIDs2[1]); err = nc_enddef( ncid); err = nc_put_var_double( ncid, dataIDs2[0], PA2.data() ); err = nc_put_var_double( ncid, dataIDs2[1], A2.data() ); err = nc_redef(ncid); //plot12 // dim_ids12[0]=dim_ids1[0]; // dim_ids12[1]=dim_ids2[0]; dim_ids12[0]=dataIDs1[0]; dim_ids12[1]=dataIDs2[0]; err = file::define_dimensions( ncid, &dim_ids12[0], g2d); err = nc_def_var( ncid, "P(A1,A2)", NC_DOUBLE, 2, &dim_ids12[0], &dataIDs12[0]); err = nc_enddef( ncid); err = nc_put_var_double( ncid, dataIDs12[0], PA1A2.data() ); err = nc_redef(ncid); nc_close( ncid); return 0; }