int ManualLabeling::catheterFloodFillSegmentation(const cv::Mat& inputImage, cv::Mat &outputImg, cv::Point2i seedPt, cv::Rect regionIn) { cv::Mat inputImageHSV; outputImg = cv::Mat::zeros(inputImage.size(), CV_8UC1); cvtColor(inputImage, inputImageHSV, CV_BGR2HSV); int newMaskVal = 255; cv::Scalar newVal = cv::Scalar(120, 120, 120); int connectivity = 8; int flags = connectivity + (newMaskVal << 8 ) + cv::FLOODFILL_FIXED_RANGE + cv::FLOODFILL_MASK_ONLY; int lo = 10; int up = 10; cv::Mat mask2; mask2 = cv::Mat::zeros(inputImage.rows + 2, inputImage.cols + 2, CV_8UC1); floodFill(inputImage, mask2, seedPt, newVal, 0, cv::Scalar(lo, lo, lo), cv::Scalar(up, up, up), flags); cv::Mat mask = mask2(cv::Range( 1, mask2.rows - 1 ), cv::Range( 1, mask2.cols - 1 ) ); cv::Mat elementOpen = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3, 3)); cv::Mat elementClose = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2, 2)); cv::Mat openMask,closeMask; morphologyEx(mask, openMask, cv::MORPH_OPEN, elementOpen); morphologyEx(openMask, closeMask, cv::MORPH_CLOSE, elementClose); outputImg = closeMask.clone(); return 1; }
/* Match CIDR strings, e.g. 127.0.0.1 to 127.0.0.0/8 or 3ffe:1:5:6::8 to 3ffe:1::0/32 * * This will also attempt to match any leading usernames or nicknames on the mask, using * match(), when match_with_username is true. */ bool irc::sockets::MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username) { std::string address_copy; std::string cidr_copy; /* The caller is trying to match ident@<mask>/bits. * Chop off the ident@ portion, use match() on it * seperately. */ if (match_with_username) { /* Use strchr not strrchr, because its going to be nearer to the left */ std::string::size_type username_mask_pos = cidr_mask.rfind('@'); std::string::size_type username_addr_pos = address.rfind('@'); /* Both strings have an @ symbol in them */ if (username_mask_pos != std::string::npos && username_addr_pos != std::string::npos) { /* Try and match() the strings before the @ * symbols, and recursively call MatchCIDR without * username matching enabled to match the host part. */ return (InspIRCd::Match(address.substr(0, username_addr_pos), cidr_mask.substr(0, username_mask_pos), ascii_case_insensitive_map) && MatchCIDR(address.substr(username_addr_pos + 1), cidr_mask.substr(username_mask_pos + 1), false)); } else { address_copy = address.substr(username_addr_pos + 1); cidr_copy = cidr_mask.substr(username_mask_pos + 1); } } else { address_copy.assign(address); cidr_copy.assign(cidr_mask); } const std::string::size_type per_pos = cidr_copy.rfind('/'); if ((per_pos == std::string::npos) || (per_pos == cidr_copy.length()-1) || (cidr_copy.find_first_not_of("0123456789", per_pos+1) != std::string::npos) || (cidr_copy.find_first_not_of("0123456789abcdefABCDEF.:") < per_pos)) { // The CIDR mask is invalid return false; } irc::sockets::sockaddrs addr; irc::sockets::aptosa(address_copy, 0, addr); irc::sockets::cidr_mask mask(cidr_copy); irc::sockets::cidr_mask mask2(addr, mask.length); return mask == mask2; }
//for approximate Flajolet & Martin counting void create_hashed_bitmask(size_t id) { for (size_t i = 0; i < DUPULICATION_OF_BITMASKS; ++i) { size_t hash_val = hash_value(); std::vector<bool> mask1(hash_val + 2, 0); mask1[hash_val] = 1; bitmask1.push_back(mask1); std::vector<bool> mask2(hash_val + 2, 0); mask2[hash_val] = 1; bitmask2.push_back(mask2); } }
bool dgDynamicBody::IsInEquilibrium () const { if (m_equilibrium) { dgVector forceError (m_accel - m_prevExternalForce); dgVector torqueError (m_alpha - m_prevExternalTorque); dgVector mask0 ((forceError.DotProduct4(forceError) < m_equilibriumError2) & (torqueError.DotProduct4(torqueError) < m_equilibriumError2)); if (mask0.GetSignMask()) { dgVector invMassMag2 (m_invMass[3] * m_invMass[3]); dgVector mask1 ((invMassMag2.CompProduct4 (m_netForce.DotProduct4(m_netForce)) < m_equilibriumError2) & (invMassMag2.CompProduct4 (m_netTorque.DotProduct4(m_netTorque)) < m_equilibriumError2)); if (mask1.GetSignMask()) { dgVector mask2 ((m_veloc.DotProduct4(m_veloc) < m_equilibriumError2) & (m_omega.DotProduct4(m_omega) < m_equilibriumError2)); return mask2.GetSignMask() ? true : false; } } } return false; }
// Analysis_Modes::Setup() Analysis::RetType Analysis_Modes::Setup(ArgList& analyzeArgs, AnalysisSetup& setup, int debugIn) { debug_ = debugIn; // Analysis type if (analyzeArgs.hasKey("fluct")) type_ = FLUCT; else if (analyzeArgs.hasKey("displ")) type_ = DISPLACE; else if (analyzeArgs.hasKey("corr")) type_ = CORR; else if (analyzeArgs.Contains("trajout")) type_ = TRAJ; else if (analyzeArgs.hasKey("eigenval")) type_ = EIGENVAL; else if (analyzeArgs.hasKey("rmsip")) type_ = RMSIP; else { mprinterr("Error: No analysis type specified.\n"); return Analysis::ERR; } // Get modes name std::string modesfile = analyzeArgs.GetStringKey("name"); if (modesfile.empty()) { // Check for deprecated args CheckDeprecated(analyzeArgs, modesfile, "file"); CheckDeprecated(analyzeArgs, modesfile, "stack"); if (modesfile.empty()) { mprinterr("Error: No 'name <modes data set name>' argument given.\n"); return Analysis::ERR; } } // Get second modes name for RMSIP std::string modesfile2 = analyzeArgs.GetStringKey("name2"); if (type_ == RMSIP) { if (modesfile2.empty()) { mprinterr("Error: 'rmsip' requires second modes data 'name2 <modes>'\n"); return Analysis::ERR; } } else modesfile2.clear(); // Get topology for TRAJ/CORR Topology* analyzeParm = setup.DSL().GetTopology( analyzeArgs ); if (type_ == TRAJ ) { // Get trajectory format args for projected traj beg_ = analyzeArgs.getKeyInt("beg",1) - 1; // Args start at 1 std::string tOutName = analyzeArgs.GetStringKey("trajout"); if (tOutName.empty()) { mprinterr("Error: Require output trajectory filename, 'trajout <name>'\n"); return Analysis::ERR; } TrajectoryFile::TrajFormatType tOutFmt = TrajectoryFile::UNKNOWN_TRAJ; if ( analyzeArgs.Contains("trajoutfmt") ) tOutFmt = TrajectoryFile::GetFormatFromString( analyzeArgs.GetStringKey("trajoutfmt") ); if (analyzeParm == 0) { mprinterr("Error: Could not get topology for output trajectory.\n"); return Analysis::ERR; } AtomMask tOutMask( analyzeArgs.GetStringKey("trajoutmask") ); if ( analyzeParm->SetupIntegerMask( tOutMask ) || tOutMask.None() ) { mprinterr("Error: Could not setup output trajectory mask.\n"); return Analysis::ERR; } tOutMask.MaskInfo(); // Strip topology to match mask. if (tOutParm_ != 0) delete tOutParm_; tOutParm_ = analyzeParm->modifyStateByMask( tOutMask ); if (tOutParm_ == 0) { mprinterr("Error: Could not create topology to match mask.\n"); return Analysis::ERR; } // Setup output traj if (trajout_.InitTrajWrite( tOutName, ArgList(), tOutFmt ) != 0) { mprinterr("Error: Could not init output trajectory.\n"); return Analysis::ERR; } // Get min and max for PC pcmin_ = analyzeArgs.getKeyDouble("pcmin", -10.0); pcmax_ = analyzeArgs.getKeyDouble("pcmax", 10.0); if (pcmax_ < pcmin_ || pcmax_ - pcmin_ < Constants::SMALL) { mprinterr("Error: pcmin must be less than pcmax\n"); return Analysis::ERR; } tMode_ = analyzeArgs.getKeyInt("tmode", 1); } else { // Args for everything else beg_ = analyzeArgs.getKeyInt("beg",7) - 1; // Args start at 1 bose_ = analyzeArgs.hasKey("bose"); calcAll_ = analyzeArgs.hasKey("calcall"); } end_ = analyzeArgs.getKeyInt("end", 50); factor_ = analyzeArgs.getKeyDouble("factor",1.0); std::string setname = analyzeArgs.GetStringKey("setname"); // Check if modes name exists on the stack modinfo_ = (DataSet_Modes*)setup.DSL().FindSetOfType( modesfile, DataSet::MODES ); if (modinfo_ == 0) { mprinterr("Error: '%s' not found: %s\n", modesfile.c_str(), DataSet_Modes::DeprecateFileMsg); return Analysis::ERR; } if (!modesfile2.empty()) { modinfo2_ = (DataSet_Modes*)setup.DSL().FindSetOfType( modesfile2, DataSet::MODES ); if (modinfo2_ == 0) { mprinterr("Error: Set %s not found.\n", modesfile2.c_str()); return Analysis::ERR; } } // Check modes type for specified analysis if (type_ == FLUCT || type_ == DISPLACE || type_ == CORR || type_ == TRAJ) { if (modinfo_->Meta().ScalarType() != MetaData::COVAR && modinfo_->Meta().ScalarType() != MetaData::MWCOVAR) { mprinterr("Error: Modes must be of type COVAR or MWCOVAR for %s.\n", analysisTypeString[type_]); return Analysis::ERR; } } // Get output filename for types that use DataSets std::string outfilename = analyzeArgs.GetStringKey("out"); // TODO all datafile? DataFile* dataout = 0; if (type_ == FLUCT || type_ == DISPLACE || type_ == EIGENVAL || type_ == RMSIP) dataout = setup.DFL().AddDataFile( outfilename, analyzeArgs ); else if (type_ == CORR) { // CORR-specific setup outfile_ = setup.DFL().AddCpptrajFile( outfilename, "Modes analysis", DataFileList::TEXT, true ); if (outfile_ == 0) return Analysis::ERR; // Get list of atom pairs if (analyzeParm == 0) { mprinterr("Error: 'corr' requires topology.\n"); return Analysis::ERR; } std::string maskexp = analyzeArgs.GetStringKey("mask1"); if (maskexp.empty()) { while (analyzeArgs.hasKey("maskp")) { // Next two arguments should be one-atom masks std::string a1mask = analyzeArgs.GetMaskNext(); std::string a2mask = analyzeArgs.GetMaskNext(); if (a1mask.empty() || a2mask.empty()) { mprinterr("Error: For 'corr' two 1-atom masks are expected.\n"); return Analysis::ERR; } // Check that each mask is just 1 atom AtomMask m1( a1mask ); AtomMask m2( a2mask ); analyzeParm->SetupIntegerMask( m1 ); analyzeParm->SetupIntegerMask( m2 ); if ( m1.Nselected()==1 && m2.Nselected()==1 ) // Store atom pair atompairStack_.push_back( std::pair<int,int>( m1[0], m2[0] ) ); else { mprinterr("Error: For 'corr', masks should specify only one atom.\n" "\tM1[%s]=%i atoms, M2[%s]=%i atoms.\n", m1.MaskString(), m1.Nselected(), m2.MaskString(), m2.Nselected()); return Analysis::ERR; } } } else { AtomMask mask1( maskexp ); maskexp = analyzeArgs.GetStringKey("mask2"); if (maskexp.empty()) { mprinterr("Error: 'mask2' must be specified if 'mask1' is.\n"); return Analysis::ERR; } AtomMask mask2( maskexp ); if ( analyzeParm->SetupIntegerMask( mask1 ) ) return Analysis::ERR; if ( analyzeParm->SetupIntegerMask( mask2 ) ) return Analysis::ERR; mask1.MaskInfo(); mask2.MaskInfo(); if (mask1.None() || mask2.None()) { mprinterr("Error: One or both masks are empty.\n"); return Analysis::ERR; } if (mask1.Nselected() != mask2.Nselected()) { mprinterr("Error: # atoms in mask 1 not equal to # atoms in mask 2.\n"); return Analysis::ERR; } for (int idx = 0; idx != mask1.Nselected(); idx++) atompairStack_.push_back( std::pair<int,int>( mask1[idx], mask2[idx] ) ); } if ( atompairStack_.empty() ) { mprinterr("Error: No atom pairs found (use 'maskp' or 'mask1'/'mask2' keywords.)\n"); return Analysis::ERR; } } // Set up data sets Dimension Xdim; if (type_ == FLUCT) { if (setname.empty()) setname = setup.DSL().GenerateDefaultName("FLUCT"); MetaData md(setname, "rmsX"); OutSets_.resize( 4, 0 ); OutSets_[RMSX] = setup.DSL().AddSet( DataSet::DOUBLE, md ); md.SetAspect("rmsY"); OutSets_[RMSY] = setup.DSL().AddSet( DataSet::DOUBLE, md ); md.SetAspect("rmsZ"); OutSets_[RMSZ] = setup.DSL().AddSet( DataSet::DOUBLE, md ); md.SetAspect("rms"); OutSets_[RMS] = setup.DSL().AddSet( DataSet::DOUBLE, md ); Xdim = Dimension(1, 1, "Atom_no."); } else if (type_ == DISPLACE) { if (setname.empty()) setname = setup.DSL().GenerateDefaultName("DISPL"); MetaData md(setname, "displX"); OutSets_.resize( 3, 0 ); OutSets_[RMSX] = setup.DSL().AddSet( DataSet::DOUBLE, md ); md.SetAspect("displY"); OutSets_[RMSY] = setup.DSL().AddSet( DataSet::DOUBLE, md ); md.SetAspect("displZ"); OutSets_[RMSZ] = setup.DSL().AddSet( DataSet::DOUBLE, md ); Xdim = Dimension(1, 1, "Atom_no."); } else if (type_ == EIGENVAL) { if (setname.empty()) setname = setup.DSL().GenerateDefaultName("XEVAL"); MetaData md(setname, "Frac"); OutSets_.resize( 3, 0 ); OutSets_[0] = setup.DSL().AddSet( DataSet::DOUBLE, md ); md.SetAspect("Cumulative"); OutSets_[1] = setup.DSL().AddSet( DataSet::DOUBLE, md ); md.SetAspect("Eigenval"); OutSets_[2] = setup.DSL().AddSet( DataSet::DOUBLE, md ); Xdim = Dimension( 1, 1, "Mode" ); } else if (type_ == RMSIP) { if (setname.empty()) setname = setup.DSL().GenerateDefaultName("RMSIP"); OutSets_.push_back( setup.DSL().AddSet( DataSet::DOUBLE, setname ) ); if (dataout != 0) dataout->ProcessArgs("noxcol"); OutSets_[0]->SetupFormat() = TextFormat(TextFormat::GDOUBLE); OutSets_[0]->SetLegend( modinfo_->Meta().Legend() + "_X_" + modinfo2_->Meta().Legend() ); } for (std::vector<DataSet*>::const_iterator set = OutSets_.begin(); set != OutSets_.end(); ++set) { if (*set == 0) return Analysis::ERR; if (dataout != 0) dataout->AddDataSet( *set ); (*set)->SetDim(Dimension::X, Xdim); } // Status mprintf(" ANALYZE MODES: Calculating %s using modes from %s", analysisTypeString[type_], modinfo_->legend()); if ( type_ != TRAJ ) { if (type_ != EIGENVAL) mprintf(", modes %i to %i", beg_+1, end_); if (outfile_ != 0) mprintf("\n\tResults are written to %s\n", outfile_->Filename().full()); else if (dataout != 0) mprintf("\n\tResults are written to '%s'\n", dataout->DataFilename().full()); if (type_ != EIGENVAL && type_ != RMSIP) { if (bose_) mprintf("\tBose statistics used.\n"); else mprintf("\tBoltzmann statistics used.\n"); if (calcAll_) mprintf("\tEigenvectors associated with zero or negative eigenvalues will be used.\n"); else mprintf("\tEigenvectors associated with zero or negative eigenvalues will be skipped.\n"); } if (type_ == DISPLACE) mprintf("\tFactor for displacement: %f\n", factor_); if (type_ == CORR) { mprintf("\tUsing the following atom pairs:"); for (modestack_it apair = atompairStack_.begin(); apair != atompairStack_.end(); ++apair) mprintf(" (%i,%i)", apair->first+1, apair->second+1 ); mprintf("\n"); } if (type_ == RMSIP) mprintf("\tRMSIP calculated to modes in %s\n", modinfo2_->legend()); } else { mprintf("\n\tCreating trajectory for mode %i\n" "\tWriting to trajectory %s\n" "\tPC range: %f to %f\n" "\tScaling factor: %f\n", tMode_, trajout_.Traj().Filename().full(), pcmin_, pcmax_, factor_); } return Analysis::OK; }