void GTVideo::setForegroundMask() { for (int i=0; i<getFrameNumber(); i++) { cv::Mat foreground_mask(source.at(0).rows,source.at(0).cols,CV_8UC1); subtractBackground(foreground_mask,source.at(i)); foregroundMask.push_back(foreground_mask); } //cv::imwrite("frame_cur.jpg",source.at(55)); //cv::imwrite("foregroundmask.jpg",foregroundMask.at(55)); }
MatrixWorkspace_sptr RefReduction::processData(const std::string polarization) { m_output_message += "Processing " + polarization + '\n'; const std::string dataRun = getPropertyValue("DataRun"); IEventWorkspace_sptr evtWS = loadData(dataRun, polarization); MatrixWorkspace_sptr dataWS = boost::dynamic_pointer_cast<MatrixWorkspace>(evtWS); MatrixWorkspace_sptr dataWSTof = boost::dynamic_pointer_cast<MatrixWorkspace>(evtWS); // If we have no events, stop here if (evtWS->getNumberEvents()==0) return dataWS; // Get low-res pixel range int low_res_min = 0; int low_res_max = 0; const bool cropLowRes = getProperty("CropLowResDataAxis"); const std::vector<int> lowResRange = getProperty("LowResDataAxisPixelRange"); if (cropLowRes) { if (lowResRange.size()<2) { g_log.error() << "LowResDataAxisPixelRange parameter should be a vector of two values" << std::endl; throw std::invalid_argument("LowResDataAxisPixelRange parameter should be a vector of two values"); } low_res_min = lowResRange[0]; low_res_max = lowResRange[1]; m_output_message += " |Cropping low-res axis: [" + Poco::NumberFormatter::format(low_res_min) + ", " + Poco::NumberFormatter::format(low_res_max) + "]\n"; } // Get peak range const std::vector<int> peakRange = getProperty("SignalPeakPixelRange"); if (peakRange.size()<2) { g_log.error() << "SignalPeakPixelRange parameter should be a vector of two values" << std::endl; throw std::invalid_argument("SignalPeakPixelRange parameter should be a vector of two values"); } // Get scattering angle in degrees double theta = getProperty("Theta"); const std::string instrument = getProperty("Instrument"); const bool integrateY = instrument.compare("REF_M")==0; // Get pixel ranges in real pixels int xmin = 0; int xmax = 0; int ymin = 0; int ymax = 0; if (integrateY) { if (isEmpty(theta)) theta = calculateAngleREFM(dataWS); if (!cropLowRes) low_res_max = NY_PIXELS-1; xmin = 0; xmax = NX_PIXELS-1; ymin = low_res_min; ymax = low_res_max; } else { if (isEmpty(theta)) theta = calculateAngleREFL(dataWS); if (!cropLowRes) low_res_max = NX_PIXELS-1; ymin = 0; ymax = NY_PIXELS-1; xmin = low_res_min; xmax = low_res_max; } m_output_message += " |Scattering angle: " + Poco::NumberFormatter::format(theta,6) + " deg\n"; // Subtract background if (getProperty("SubtractSignalBackground")) { // Get background range const std::vector<int> bckRange = getProperty("SignalBackgroundPixelRange"); if (bckRange.size()<2) { g_log.error() << "SignalBackgroundPixelRange parameter should be a vector of two values" << std::endl; throw std::invalid_argument("SignalBackgroundPixelRange parameter should be a vector of two values"); } IAlgorithm_sptr convAlg = createChildAlgorithm("ConvertToMatrixWorkspace", 0.50, 0.55); convAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", dataWS); convAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", dataWS); convAlg->executeAsChildAlg(); dataWS = subtractBackground(dataWS, dataWS, peakRange[0], peakRange[1], bckRange[0], bckRange[1], low_res_min, low_res_max); m_output_message += " |Subtracted background [" + Poco::NumberFormatter::format(bckRange[0]) + ", " + Poco::NumberFormatter::format(bckRange[1]) + "]\n"; } // Process normalization run if (getProperty("PerformNormalization")) { MatrixWorkspace_sptr normWS = processNormalization(); IAlgorithm_sptr rebinAlg = createChildAlgorithm("RebinToWorkspace", 0.50, 0.55); rebinAlg->setProperty<MatrixWorkspace_sptr>("WorkspaceToRebin", normWS); rebinAlg->setProperty<MatrixWorkspace_sptr>("WorkspaceToMatch", dataWS); rebinAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", normWS); rebinAlg->executeAsChildAlg(); normWS = rebinAlg->getProperty("OutputWorkspace"); IAlgorithm_sptr divAlg = createChildAlgorithm("Divide", 0.55, 0.65); divAlg->setProperty<MatrixWorkspace_sptr>("LHSWorkspace", dataWS); divAlg->setProperty<MatrixWorkspace_sptr>("RHSWorkspace", normWS); divAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", dataWS); divAlg->executeAsChildAlg(); IAlgorithm_sptr repAlg = createChildAlgorithm("ReplaceSpecialValues", 0.55, 0.65); repAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", dataWS); repAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", dataWS); repAlg->setProperty("NaNValue", 0.0); repAlg->setProperty("NaNError", 0.0); repAlg->setProperty("InfinityValue", 0.0); repAlg->setProperty("InfinityError", 0.0); repAlg->executeAsChildAlg(); m_output_message += "Normalization completed\n"; } // // Integrate over Y // IAlgorithm_sptr refAlg = createChildAlgorithm("RefRoi", 0.90, 0.95); // refAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", dataWS); // refAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", dataWS); // refAlg->setProperty("NXPixel", NX_PIXELS); // refAlg->setProperty("NYPixel", NY_PIXELS); // refAlg->setProperty("YPixelMin", ymin); // refAlg->setProperty("YPixelMax", ymax); // refAlg->setProperty("XPixelMin", xmin); // refAlg->setProperty("XPixelMax", xmax); // refAlg->setProperty("IntegrateY", integrateY); // refAlg->setProperty("ScatteringAngle", theta); // refAlg->executeAsChildAlg(); // // // Convert back to TOF // IAlgorithm_sptr convAlgToTof = createChildAlgorithm("ConvertUnits", 0.85, 0.90); // convAlgToTof->setProperty<MatrixWorkspace_sptr>("InputWorkspace", dataWS); // convAlgToTof->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", dataWSTof); // convAlgToTof->setProperty("Target", "TOF"); // convAlgToTof->executeAsChildAlg(); // // MatrixWorkspace_sptr outputWS2 = convAlgToTof->getProperty("OutputWorkspace"); // declareProperty(new WorkspaceProperty<>("OutputWorkspace_jc_" + polarization, "TOF_"+polarization, Direction::Output)); // setProperty("OutputWorkspace_jc_" + polarization, outputWS2); //integrated over Y and keep in lambda scale IAlgorithm_sptr refAlg1 = createChildAlgorithm("RefRoi", 0.90, 0.95); refAlg1->setProperty<MatrixWorkspace_sptr>("InputWorkspace", dataWS); refAlg1->setProperty("NXPixel", NX_PIXELS); refAlg1->setProperty("NYPixel", NY_PIXELS); refAlg1->setProperty("ConvertToQ", false); refAlg1->setProperty("YPixelMin", ymin); refAlg1->setProperty("YPixelMax", ymax); refAlg1->setProperty("XPixelMin", xmin); refAlg1->setProperty("XPixelMax", xmax); refAlg1->setProperty("IntegrateY", integrateY); refAlg1->setProperty("ScatteringAngle", theta); refAlg1->executeAsChildAlg(); MatrixWorkspace_sptr outputWS2 = refAlg1->getProperty("OutputWorkspace"); declareProperty(new WorkspaceProperty<>("OutputWorkspace_jc_" + polarization, "Lambda_"+polarization, Direction::Output)); setProperty("OutputWorkspace_jc_" + polarization, outputWS2); // Conversion to Q IAlgorithm_sptr refAlg = createChildAlgorithm("RefRoi", 0.90, 0.95); refAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", dataWS); refAlg->setProperty("NXPixel", NX_PIXELS); refAlg->setProperty("NYPixel", NY_PIXELS); refAlg->setProperty("ConvertToQ", true); refAlg->setProperty("YPixelMin", ymin); refAlg->setProperty("YPixelMax", ymax); refAlg->setProperty("XPixelMin", xmin); refAlg->setProperty("XPixelMax", xmax); refAlg->setProperty("IntegrateY", integrateY); refAlg->setProperty("ScatteringAngle", theta); refAlg->executeAsChildAlg(); MatrixWorkspace_sptr output2DWS = refAlg->getProperty("OutputWorkspace"); std::vector<int> spectra; for(int i=peakRange[0]; i<peakRange[1]+1; i++) spectra.push_back(i); IAlgorithm_sptr grpAlg = createChildAlgorithm("GroupDetectors", 0.95, 0.99); grpAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", output2DWS); grpAlg->setProperty("SpectraList", spectra); grpAlg->executeAsChildAlg(); MatrixWorkspace_sptr outputWS = grpAlg->getProperty("OutputWorkspace"); const std::string prefix = getPropertyValue("OutputWorkspacePrefix"); if (polarization.compare(PolStateNone)==0) { declareProperty(new WorkspaceProperty<>("OutputWorkspace", prefix, Direction::Output)); setProperty("OutputWorkspace", outputWS); declareProperty(new WorkspaceProperty<>("OutputWorkspace2D", "2D_"+prefix, Direction::Output)); setProperty("OutputWorkspace2D", output2DWS); } else { std::string wsName = prefix+polarization; Poco::replaceInPlace(wsName, "entry", ""); declareProperty(new WorkspaceProperty<>("OutputWorkspace_"+polarization, wsName, Direction::Output)); setProperty("OutputWorkspace_"+polarization, outputWS); declareProperty(new WorkspaceProperty<>("OutputWorkspace2D_"+polarization, "2D_"+wsName, Direction::Output)); setProperty("OutputWorkspace2D_"+polarization, output2DWS); } m_output_message += "Reflectivity calculation completed\n"; return outputWS; }
MatrixWorkspace_sptr RefReduction::processNormalization() { m_output_message += "Processing normalization\n"; const std::string normRun = getPropertyValue("NormalizationRun"); IEventWorkspace_sptr evtWS = loadData(normRun, PolStateNone); MatrixWorkspace_sptr normWS = boost::dynamic_pointer_cast<MatrixWorkspace>(evtWS); const std::vector<int> peakRange = getProperty("NormPeakPixelRange"); int low_res_min = 0; int low_res_max = 0; int xmin = 0; int xmax = 0; int ymin = 0; int ymax = 0; const bool cropLowRes = getProperty("CropLowResNormAxis"); const std::vector<int> lowResRange = getProperty("LowResNormAxisPixelRange"); if (cropLowRes) { if (lowResRange.size()<2) { g_log.error() << "LowResNormAxisPixelRange parameter should be a vector of two values" << std::endl; throw std::invalid_argument("LowResNormAxisPixelRange parameter should be a vector of two values"); } low_res_min = lowResRange[0]; low_res_max = lowResRange[1]; m_output_message + " |Cropping low-res axis: [" + Poco::NumberFormatter::format(low_res_min) + ", " + Poco::NumberFormatter::format(low_res_max) + "]\n"; } const std::string instrument = getProperty("Instrument"); const bool integrateY = instrument.compare("REF_M")==0; if (integrateY) { if (!cropLowRes) low_res_max = NY_PIXELS-1; xmin = peakRange[0]; xmax = peakRange[1]; ymin = low_res_min; ymax = low_res_max; } else { if (!cropLowRes) low_res_max = NX_PIXELS-1; ymin = peakRange[0]; ymax = peakRange[1]; xmin = low_res_min; xmax = low_res_max; } if (getProperty("SubtractNormBackground")) { // Get background range const std::vector<int> bckRange = getProperty("NormBackgroundPixelRange"); if (bckRange.size()<2) { g_log.error() << "NormBackgroundPixelRange parameter should be a vector of two values" << std::endl; throw std::invalid_argument("NormBackgroundPixelRange parameter should be a vector of two values"); } IAlgorithm_sptr convAlg = createChildAlgorithm("ConvertToMatrixWorkspace", 0.50, 0.55); convAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", normWS); convAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", normWS); convAlg->executeAsChildAlg(); normWS = subtractBackground(normWS, normWS, peakRange[0], peakRange[1], bckRange[0], bckRange[1], low_res_min, low_res_max); m_output_message += " |Subtracted background [" + Poco::NumberFormatter::format(bckRange[0]) + ", " + Poco::NumberFormatter::format(bckRange[1]) + "]\n"; } IAlgorithm_sptr refAlg = createChildAlgorithm("RefRoi", 0.6, 0.65); refAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", normWS); refAlg->setProperty("NXPixel", NX_PIXELS); refAlg->setProperty("NYPixel", NY_PIXELS); refAlg->setProperty("ConvertToQ", false); refAlg->setProperty("SumPixels", true); refAlg->setProperty("NormalizeSum", true); refAlg->setProperty("YPixelMin", ymin); refAlg->setProperty("YPixelMax", ymax); refAlg->setProperty("XPixelMin", xmin); refAlg->setProperty("XPixelMax", xmax); refAlg->setProperty("IntegrateY", integrateY); refAlg->executeAsChildAlg(); MatrixWorkspace_sptr outputNormWS = refAlg->getProperty("OutputWorkspace"); return outputNormWS; }
int run_subtractBackground(int analysisIs2D, int the_case, int systModeFlag=0, int debug=-1, int iSeedMin_User=-1, int iSeedMax_User=-1) { TString confName="default"; DYTools::TRunMode_t runMode=DebugInt2RunMode(debug); //DYTools::TSystematicsStudy_t systMode=DYTools::NO_SYST; DYTools::TSystematicsStudy_t systMode=DYTools::ESCALE_DIFF_0000; switch(systModeFlag) { case 0: ; break; case 1: systMode=DYTools::UNREGRESSED_ENERGY; break; } switch(the_case) { case 0: ; break; case 1: confName="defaultAdHoc"; systMode=DYTools::APPLY_ESCALE; break; case 2: case 3: confName="defaultAdHoc"; systMode=DYTools::ESCALE_STUDY_RND; break; default: std::cout << "the_case=" << the_case << " is not ready\n"; return retCodeError; } // Setup the mass-rapidity setting if (!DYTools::setup(analysisIs2D)) { std::cout << "failed to initialize the analysis\n"; return retCodeError; } analysisIs2D=-111; // further calls to DYTools::setup do nothing int res=1; if (systMode!=DYTools::ESCALE_STUDY_RND) { res=subtractBackground(analysisIs2D,confName,runMode,systMode); } else { gBenchmark->Start("run_subtractBackground"); int iSeedMin=-1; int iSeedMax=-1; int dSeed=1; if (the_case!=3) { if ((iSeedMin_User==-1) || (iSeedMax_User==-1)) { InputFileMgr_t inpMgr; if (!inpMgr.Load(confName)) return retCodeError; iSeedMin=inpMgr.userKeyValueAsInt("SEEDMIN"); iSeedMax=inpMgr.userKeyValueAsInt("SEEDMAX"); } else { iSeedMin=iSeedMin_User; iSeedMax=iSeedMax_User; std::cout << "using user-provided iSeed values " << iSeedMin << " .. " << iSeedMax << "\n"; } } else { iSeedMin=-111; iSeedMax= 111; dSeed=iSeedMax-iSeedMin; } if (res) res=retCodeOk; for (int iSeed=iSeedMin; (res==retCodeOk) && (iSeed<=iSeedMax); iSeed+=dSeed) { std::cout << "\n\n\tstarting iSeed=" << iSeed << "\n\n"; res=subtractBackground(analysisIs2D,confName,runMode,systMode,iSeed); } ShowBenchmarkTime("run_subtractBackground"); } return res; }