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(); }
void MainWindow::on_actionShape_histogram_distance_triggered() { QPixmap pixmap = pixmapItem->pixmap().copy(); QImage image = pixmap.toImage(); int width = image.width(); int height = image.height(); if (width == 0 || height == 0) { ui->statusBar->showMessage( tr("Error. Image bad size"), 3000 ); return; } double threshold = 0.8; picturesR.clear(); QString dirName = QFileDialog::getExistingDirectory(this, tr("Choose directory") ); QDir dir(dirName); QDirIterator it(dir.absolutePath(), QDir::Files); std::vector<double> origHist = hist2(pixmap, 20, 20); for ( ; it.hasNext(); it.next() ) { QString curFileName = it.filePath(); QPixmap curPixmap(curFileName); if ( curPixmap.isNull() ) continue; std::vector<double> curHist = hist2(curPixmap, 20, 20); double dist = histDistance2(origHist, curHist); qDebug() << dist; if (dist < threshold) picturesR.append(curPixmap); } QString mes = tr("Images: ") + QString::number(picturesR.size()); ui->statusBar->showMessage(mes, 3000); if (picturesR.size() > 0) { picturesRind = 0; pixmapItem_2->setPixmap(picturesR[0]); scene_2->setSceneRect(QRectF(picturesR[0].rect())); } }
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 ); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { const int *dimsf, *dimsg; float s[3]; if (nrhs>4 || nrhs<3 || nlhs>1) mexErrMsgTxt("Incorrect usage."); if (!mxIsNumeric(prhs[0]) || !mxIsUint8(prhs[0]) || mxIsComplex(prhs[0])) mexErrMsgTxt("Wrong sort of data (1)."); if (mxGetNumberOfDimensions(prhs[0]) != 3) mexErrMsgTxt("Wrong number of dims (1).");; dimsg = mxGetDimensions(prhs[0]); if (!mxIsNumeric(prhs[1]) || !mxIsUint8(prhs[1]) || mxIsComplex(prhs[1])) mexErrMsgTxt("Wrong sort of data (2)."); if (mxGetNumberOfDimensions(prhs[0]) != 3) mexErrMsgTxt("Wrong number of dims (1).");; dimsf = mxGetDimensions(prhs[1]); if (!mxIsNumeric(prhs[2]) || !mxIsDouble(prhs[2]) || mxIsComplex(prhs[2])) mexErrMsgTxt("Wrong sort of matrix."); if (mxGetM(prhs[2]) != 4 || mxGetN(prhs[2]) != 4) mexErrMsgTxt("Matrix must be 4x4."); if (nrhs == 4) { if (!mxIsNumeric(prhs[3]) || !mxIsDouble(prhs[3]) || mxIsComplex(prhs[3]) || mxGetM(prhs[3])*mxGetN(prhs[3]) != 3) mexErrMsgTxt("Invalid skips."); s[0] = mxGetPr(prhs[3])[0]; s[1] = mxGetPr(prhs[3])[1]; s[2] = mxGetPr(prhs[3])[2]; } else { s[0] = s[1] = s[2] = 1.0; } plhs[0] = mxCreateDoubleMatrix(256,256,mxREAL); hist2(mxGetPr(prhs[2]), (unsigned char *)mxGetPr(prhs[0]), (unsigned char *)mxGetPr(prhs[1]), dimsg, dimsf, mxGetPr(plhs[0]), s); }
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; }
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; };
double pewarpcost(float *x, float *bX, float *bY, float *dbY, float *bZ, mwSignedIndex *bdims, uint8_T *target, float *source, uint8_T *mask, mwSignedIndex *ddims, float *skrn, int skrnl, float xscale, float regstrength, float *H, uint8_T *warped, float *def) { int nvox = ddims[0] * ddims[1] * ddims[2]; /* float *def = mxMalloc(sizeof(float) * nvox); */ float *jac = mxMalloc(sizeof(float) * nvox); /* uint8_T *warped = mxMalloc(sizeof(uint8_T) * nvox); */ /* float *H = mxMalloc(sizeof(float) * 256 * 256); */ float *Htmp = mxMalloc(sizeof(float) * 256 * 256); double *s1 = mxMalloc(sizeof(double) * 256); double *s2 = mxMalloc(sizeof(double) * 256); double s, sL, s1L, s2L; double cost; /* mexPrintf(">>> Start -> %lu (CLOCKS_PER_SEC: %lu)\n", clock(), CLOCKS_PER_SEC); */ getDef(def, ddims, bdims[0], bX, bdims[1], bY, bdims[2], bZ, x, xscale); /* The actual Jacobian is 1 + jac, but it is easier to add 1 later. */ getDef(jac, ddims, bdims[0], bX, bdims[1], dbY, bdims[2], bZ, x, xscale); /* mexPrintf("getDef done -> %lu\n", clock()); */ /* For some reason, execution is single-threaded without the num_threads clause. * TODO: Find out why and change. */ #pragma omp parallel num_threads(4) shared(s, sL, s1L, s2L) { peResampleAndApplyJacobian(warped, source, def, /* jac, */ mask, ddims); /* if (omp_get_thread_num() == 1) mexPrintf("peResampleAndApplyJacobian done -> %lu\n", clock()); */ hist2(H, target, warped, mask, nvox); /* if (omp_get_thread_num() == 1) mexPrintf("hist2 done -> %lu\n", clock()); */ smoothRows(Htmp, H, 256, 256, skrn, skrnl); smoothCols(H, Htmp, 256, 256, skrn, skrnl); /* if (omp_get_thread_num() == 1) mexPrintf("smooth... done -> %lu\n", clock()); */ #pragma omp single { s = histSumAll(H, 65536); } #pragma omp sections { #pragma omp section { sL = histLogAll(H, s, 65536); } #pragma omp section { s1L = histLogVert(H, s1, s, 256, 256); } #pragma omp section { s2L = histLogHorz(H, s2, s, 256, 256); } } /* if (omp_get_thread_num() == 1) mexPrintf("hist... done -> %lu\n", clock()); */ } cost = - (s1L + s2L) / sL + regstrength * regularisation(jac, nvox); /* mexPrintf("regularisation done -> %lu\n", clock()); mexPrintf("<<<\n"); */ /* mexPrintf("cost: %f sL: %f s1L: %f s2L: %f #voxels: %d\n", cost, sL, s1L, s2L, nvox); */ /* mxFree(def); */ mxFree(jac); /* mxFree(warped); */ /* mxFree(H); */ mxFree(Htmp); mxFree(s1); mxFree(s2); return cost; }
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; }