Skip to content

Commit

Permalink
[CORPS] Working with radians instead of degrees [CLEAN] Changed posit…
Browse files Browse the repository at this point in the history
…iveGradient for gradientOrientation that is more understandable
  • Loading branch information
rlagneau committed Dec 1, 2023
1 parent 43f9441 commit e459437
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions modules/core/src/image/vpCannyEdgeDetection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ vpCannyEdgeDetection::performFilteringAndGradientComputation(const vpImage<unsig
/**
* \brief Get the interpolation weights and offsets.
*
* \param[in] positiveTheta : The positive value of the angle of the edge, expressed in degrees.
* \param[in] gradientOrientation : The positive value of the angle of the edge, expressed in radians.
* Its value is between 0 and M_PIf radians.
* \param[out] alpha : The weight of the first point used for the interpolation.
* \param[out] beta : The weight of the second point used for the interpolation.
* \param[out] dRowGradAlpha : The offset along the row attached to the alpha weight.
Expand All @@ -244,45 +245,45 @@ vpCannyEdgeDetection::performFilteringAndGradientComputation(const vpImage<unsig
* \param[out] dColGradBeta : The offset along the column attached to the beta weight.
*/
void
getInterpolationWeightsAndOffsets(const float &positiveTheta,
getInterpolationWeightsAndOffsets(const float &gradientOrientation,
float &alpha, float &beta,
int &dRowGradAlpha, int &dRowGradBeta,
int &dColGradAlpha, int &dColGradBeta
)
{
float thetaMin = 0.f;
if (positiveTheta < 45.f) {
if (gradientOrientation < M_PI_4f) {
// Angles between 0 and 45 deg rely on the horizontal and diagonal points
dColGradAlpha = 1;
dColGradBeta = 1;
dRowGradAlpha = 0;
dRowGradBeta = -1;
}
else if (positiveTheta >= 45.f && positiveTheta < 90.f) {
else if (gradientOrientation >= M_PI_4f && gradientOrientation < M_PI_2f) {
// Angles between 45 and 90 deg rely on the diagonal and vertical points
thetaMin = 45.f;
thetaMin = M_PI_4f;
dColGradAlpha = 1;
dColGradBeta = 0;
dRowGradAlpha = -1;
dRowGradBeta = -1;
}
else if (positiveTheta >= 90.f && positiveTheta < 135.f) {
else if (gradientOrientation >= M_PI_2f && gradientOrientation < (3.f * M_PI_4f)) {
// Angles between 90 and 135 deg rely on the vertical and diagonal points
thetaMin = 90.f;
thetaMin = M_PI_2f;
dColGradAlpha = 0;
dColGradBeta = -1;
dRowGradAlpha = -1;
dRowGradBeta = -1;
}
else if (positiveTheta >= 135.f && positiveTheta < 180.f) {
else if (gradientOrientation >= (3.f * M_PI_4f) && gradientOrientation < M_PIf) {
// Angles between 135 and 180 deg rely on the vertical and diagonal points
thetaMin = 135.f;
thetaMin = 3.f * M_PI_4f;
dColGradAlpha = -1;
dColGradBeta = -1;
dRowGradAlpha = -1;
dRowGradBeta = 0;
}
beta = (positiveTheta - thetaMin) / 45.f;
beta = (gradientOrientation - thetaMin) / M_PI_4f;
alpha = 1.f - beta;
}

Expand Down Expand Up @@ -314,35 +315,35 @@ getManhattanGradient(const vpImage<float> &dIx, const vpImage<float> &dIy, const
}

/**
* @brief Get the gradient orientation, expressed in degrees, between 0 and +180 degrees.
* If the gradient orientation is negative, we add 180 degrees (i.e. M_PI radiants) in
* @brief Get the gradient orientation, expressed in radians, between 0 and M_PIf radians.
* If the gradient orientation is negative, we add M_PI radians in
* order to keep the same orientation but in the positive direction.
*
* @param dIx : Gradient along the horizontal axis.
* @param dIy : Gradient along the vertical axis.
* @param row : Index along the vertical axis.
* @param col : Index along the horizontal axis.
* @return float The positive value of the gradient orientation, expressed in degrees.
* @return float The positive value of the gradient orientation, expressed in radians.
*/
float
getPositiveTheta(const vpImage<float> &dIx, const vpImage<float> &dIy, const int &row, const int &col)
getGradientOrientation(const vpImage<float> &dIx, const vpImage<float> &dIy, const int &row, const int &col)
{
float positiveTheta = 0.f;
float gradientOrientation = 0.f;
float dx = dIx[row][col];
float dy = dIy[row][col];

if (std::abs(dx) < std::numeric_limits<float>::epsilon()) {
positiveTheta = 90.f;
gradientOrientation = M_PI_2f;
}
else {
// -dy because the y-axis of the image is oriented towards the bottom of the screen
// while we later work with a y-axis oriented towards the top when getting the theta quadrant.
positiveTheta = static_cast<float>(vpMath::deg(std::atan2(-dy , dx)));
if(positiveTheta < 0.f) {
positiveTheta += 180.f; // + M_PI in order to be between 0 and M_PI
gradientOrientation = static_cast<float>(std::atan2(-dy , dx));
if(gradientOrientation < 0.f) {
gradientOrientation += M_PIf; // + M_PI in order to be between 0 and M_PIf
}
}
return positiveTheta;
return gradientOrientation;
}

void
Expand All @@ -365,9 +366,9 @@ vpCannyEdgeDetection::performEdgeThinning(const float &lowerThreshold)
// depending on the gradient orientation
int dRowAlphaPlus = 0, dRowBetaPlus = 0;
int dColAphaPlus = 0, dColBetaPlus = 0;
float positiveTheta = getPositiveTheta(m_dIx, m_dIy, row, col);
float gradientOrientation = getGradientOrientation(m_dIx, m_dIy, row, col);
float alpha = 0.f, beta = 0.f;
getInterpolationWeightsAndOffsets(positiveTheta, alpha, beta, dRowAlphaPlus, dRowBetaPlus, dColAphaPlus, dColBetaPlus);
getInterpolationWeightsAndOffsets(gradientOrientation, alpha, beta, dRowAlphaPlus, dRowBetaPlus, dColAphaPlus, dColBetaPlus);
int dRowAlphaMinus = -dRowAlphaPlus, dRowBetaMinus = -dRowBetaPlus;
int dColAphaMinus = -dColAphaPlus, dColBetaMinus = -dColBetaPlus;
float gradAlphaPlus = getManhattanGradient(m_dIx, m_dIy, row + dRowAlphaPlus, col + dColAphaPlus);
Expand Down

0 comments on commit e459437

Please sign in to comment.