Skip to content

Commit

Permalink
CodedTargetSimul: intervals min/max for params
Browse files Browse the repository at this point in the history
  • Loading branch information
jmmuller committed Oct 23, 2024
1 parent f4bc76e commit 459625d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 48 deletions.
1 change: 1 addition & 0 deletions MMVII/include/MMVII_nums.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ double RandUnif_C_NotNull(double aEps); ///< Uniform distribution in -1 1, b
double RandUnif_NotNull(double aEps); ///< Uniform distribution in 0 1, but abs > aEps
double RandInInterval(double a,double b); ///< Uniform distribution in [a,b]
double RandInInterval(const cPt2dr &interval); ///< Uniform distribution in [interval.x,interval.y]
double RandInInterval_C(const cPt2dr &interval); ///< Uniform distribution in [-interval.y,-interval.x]U[interval.x,interval.y]

/** Class for mapping object R->R */
class cFctrRR
Expand Down
102 changes: 55 additions & 47 deletions MMVII/src/CodedTarget/cSimulTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,6 @@ class cAppliSimulCodeTarget : public cMMVII_Appli
/// Put the target in the image
void IncrustTarget(cGeomSimDCT & aGSD);

/// return a random radius in the specified interval
double RandomRadius() const;
/// return a random radius ratio in the specified interval
double RandomRatio() const;


cPhotogrammetricProject mPhProj;

// =========== Mandatory args ============
Expand All @@ -147,10 +141,10 @@ class cAppliSimulCodeTarget : public cMMVII_Appli

// --
double mDownScale; ///< initial downscale of target
double mAttenContrast; ///< amplitude of (random) gray attenuatio,
double mAttenMul; ///< Multiplicative attenuation
double mPropSysLin; ///< amplitude of (random) linear bias
double mAmplWhiteNoise; ///< amplitud of random white noise
cPt2dr mAttenContrast; ///< min/max amplitude of (random) gray attenuatio,
cPt2dr mAttenMul; ///< min/max Multiplicative attenuation
cPt2dr mPropSysLin; ///< min/max amplitude of (random) linear bias
cPt2dr mAmplWhiteNoise; ///< min/max amplitude of random white noise

// =========== Internal param ============
tIm mImIn; ///< Input global image
Expand All @@ -172,10 +166,10 @@ cAppliSimulCodeTarget::cAppliSimulCodeTarget(const std::vector<std::string> & aV
mSzKernel (2.0),
mPatternNames (".*"),
mDownScale (3.0),
mAttenContrast (0.2),
mAttenMul (0.4),
mPropSysLin (0.2),
mAmplWhiteNoise (0.1),
mAttenContrast (0.,0.2),
mAttenMul (0.0,0.4),
mPropSysLin (0.,0.2),
mAmplWhiteNoise (0.,0.1),
mImIn (cPt2di(1,1)),
mSpec (nullptr),
mSuplPref ("")
Expand Down Expand Up @@ -209,11 +203,6 @@ cCollecSpecArg2007 & cAppliSimulCodeTarget::ArgOpt(cCollecSpecArg2007 & anArgOpt
;
}

double cAppliSimulCodeTarget::RandomRadius() const { return RandInInterval(mRS.mRadiusMinMax.x(),mRS.mRadiusMinMax.y());}

double cAppliSimulCodeTarget::RandomRatio() const { return RandInInterval(mRS.mRatioMinMax.x(),mRS.mRatioMinMax.y());}


void cAppliSimulCodeTarget::AddPosTarget(const cOneEncoding & anEncod)
{
cBox2dr aBoxGenerate = mImIn.DIm().ToR().Dilate(-mRS.BorderGlob());
Expand All @@ -222,8 +211,8 @@ void cAppliSimulCodeTarget::AddPosTarget(const cOneEncoding & anEncod)
{
cPt2dr aC = aBoxGenerate.GeneratePointInside(); // generat a random point inside the box
// Compute two random radii in the given interval
double aRbig = RandomRadius() ;
double aRsmall = aRbig*RandomRatio() ;
double aRbig = RandInInterval(mRS.mRadiusMinMax);
double aRsmall = aRbig*RandInInterval(mRS.mRatioMinMax);

// check if there is already a selected target overlaping
cGeomSimDCT aGSD(anEncod,aC,aRsmall,aRbig);
Expand Down Expand Up @@ -254,9 +243,9 @@ void cAppliSimulCodeTarget::IncrustTarget(cGeomSimDCT & aGSD)

cPt2dr aDirModif = FromPolar(1.0,M_PI*RandUnif_C());
double aDiag = Norm2(aC0);
double aAttenContrast = mAttenContrast * RandUnif_0_1();
double aAttenMul = (1-mAttenMul) + mAttenMul * RandUnif_0_1();
double aAttenLin = mPropSysLin * RandUnif_0_1();
double aAttenContrast = RandInInterval(mAttenContrast);
double aAttenMul = (1-mAttenMul.y()) + RandInInterval(mAttenMul);
double aAttenLin = RandInInterval(mPropSysLin);
for (const auto & aPix : aDImT)
{
double aVal = aDImT.GetV(aPix);
Expand Down Expand Up @@ -288,29 +277,30 @@ void cAppliSimulCodeTarget::IncrustTarget(cGeomSimDCT & aGSD)
for (const auto & aPix : cRect2(aBoxIm)) // ressample image, parse image coordinates
{
if ( aDImIn.Inside(aPix))
{
{
// compute a weighted coordinate in target coordinates,
cRessampleWeigth aRW = cRessampleWeigth::GaussBiCub(ToR(aPix),aMapIm2T,mSzKernel);
const std::vector<cPt2di> & aVPts = aRW.mVPts;
if (!aVPts.empty())
{
const std::vector<cPt2di> & aVPts = aRW.mVPts;
if (!aVPts.empty())
{
double aSomW = 0.0; // sum of weight
double aSomVW = 0.0; // weighted sum of vals
for (int aK=0; aK<int(aVPts.size()) ; aK++)
{
for (int aK=0; aK<int(aVPts.size()) ; aK++)
{
if (aDImT.Inside(aVPts[aK]))
{
double aW = aRW.mVWeight[aK];
aSomW += aW;
aSomVW += aW * aDImT.GetV(aVPts[aK]);
double aW = aRW.mVWeight[aK];
aSomW += aW;
aSomVW += aW * aDImT.GetV(aVPts[aK]);
}
}
// in cRessampleWeigth => Sum(W) is standartized and equals 1
aSomVW = aSomVW * (1- mAmplWhiteNoise) + mAmplWhiteNoise * 128 * RandUnif_C() * aSomW;
double aVal = aSomVW + (1-aSomW)*aDImIn.GetV(aPix);
aDImIn.SetV(aPix,aVal);
}
}
}
// in cRessampleWeigth => Sum(W) is standartized and equals 1

aSomVW = aSomVW * (1- mAmplWhiteNoise.y()) + RandInInterval_C(mAmplWhiteNoise) * 128 * aSomW;
double aVal = aSomVW + (1-aSomW)*aDImIn.GetV(aPix);
aDImIn.SetV(aPix,aVal);
}
}
}
// aGSD.mCornEl1 = aMapT2Im.Value(mPCT.mCornEl1/mDownScale);
// aGSD.mCornEl2 = aMapT2Im.Value(mPCT.mCornEl2/mDownScale);
Expand All @@ -323,6 +313,19 @@ void cAppliSimulCodeTarget::IncrustTarget(cGeomSimDCT & aGSD)
const std::string ThePrefixSimulTarget = "SimulTarget_";
const std::string ThePostfixGTSimulTarget = "_GroundTruth.xml";

// make sure that interval.x<interval.y, check if all in [0,1]
bool orderAndAssertInterval01(cPt2dr & interval, const std::string & aIntervalName)
{
if (interval.y()<interval.x())
std::swap(interval.x(), interval.y());
if ((interval.x()<0.)||(interval.y()>1.))
{
MMVII_USER_WARNING(aIntervalName+" must be in [0;1].");
return false;
}
return true;
}

int cAppliSimulCodeTarget::Exe()
{
mPhProj.FinishInit();
Expand All @@ -339,13 +342,18 @@ int cAppliSimulCodeTarget::Exe()
return EXIT_SUCCESS;
}

// check that ratios are in (0;1]
if ((mRS.mRatioMinMax.x()<=0)||(mRS.mRatioMinMax.x()>1.)||
(mRS.mRatioMinMax.y()<=0)||(mRS.mRatioMinMax.y()>1))
{
MMVII_USER_WARNING("Ratios must be in (0;1].");
mRS.mRatioMinMax = {0.3,1.0};
}
// check that all intervals are in (0;1]
if (!orderAndAssertInterval01(mRS.mRatioMinMax, "Ratio"))
return EXIT_FAILURE;
if (!orderAndAssertInterval01(mAmplWhiteNoise, "NoiseAmpl"))
return EXIT_FAILURE;
if (!orderAndAssertInterval01(mPropSysLin, "PropLinBias"))
return EXIT_FAILURE;
if (!orderAndAssertInterval01(mAttenContrast, "ContrastAtten"))
return EXIT_FAILURE;
if (!orderAndAssertInterval01(mAttenMul, "MulAtten"))
return EXIT_FAILURE;


// We want that random is different for each image, but deterministic for one given image
cRandGenerator::TheOne()->setSeed(HashValue(mNameIm,true));
Expand Down
13 changes: 12 additions & 1 deletion MMVII/src/UtiMaths/uti_rand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,20 @@ double RandInInterval(double a,double b)

double RandInInterval(const cPt2dr &interval)
{
return RandInInterval(interval.x(), interval.y());
return RandInInterval(interval.y(), interval.x());
}

double RandInInterval_C(const cPt2dr &interval)
{
auto a = fabs(interval.x());
auto b = fabs(interval.y());
if (a<b)
std::swap(a,b);
double rand_C_1 = (RandUnif_0_1()-0.5) * 2.0;
return (b+ (a-b) * fabs(rand_C_1)) * (rand_C_1>0?1.:-1.) ;
}


double RandUnif_C_NotNull(double aEps)
{
double aRes = RandUnif_C();
Expand Down

0 comments on commit 459625d

Please sign in to comment.