Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply misra c++ quality rules #1397

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions modules/core/include/visp3/core/vpCannyEdgeDetection.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
* Then, compute the x-axis and y-axis gradients of the image.
* \param[in] I : The image we want to compute the gradients.
*/
void performFilteringAndGradientComputation(const vpImage<unsigned char> &I);
void computeFilteringAndGradient(const vpImage<unsigned char> &I);

/**
* \brief Step 3: Edge thining.
Expand Down Expand Up @@ -214,9 +214,9 @@
*/
friend inline void from_json(const nlohmann::json &j, vpCannyEdgeDetection &detector)
{
std::string filteringAndGradientName = vpImageFilter::vpCannyFilteringAndGradientTypeToString(detector.m_filteringAndGradientType);
std::string filteringAndGradientName = vpImageFilter::vpCannyFiltAndGradTypeToStr(detector.m_filteringAndGradientType);

Check warning on line 217 in modules/core/include/visp3/core/vpCannyEdgeDetection.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpCannyEdgeDetection.h#L217

Added line #L217 was not covered by tests
filteringAndGradientName = j.value("filteringAndGradientType", filteringAndGradientName);
detector.m_filteringAndGradientType = vpImageFilter::vpCannyFilteringAndGradientTypeFromString(filteringAndGradientName);
detector.m_filteringAndGradientType = vpImageFilter::vpCannyFiltAndGradTypeFromStr(filteringAndGradientName);

Check warning on line 219 in modules/core/include/visp3/core/vpCannyEdgeDetection.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpCannyEdgeDetection.h#L219

Added line #L219 was not covered by tests
detector.m_gaussianKernelSize = j.value("gaussianSize", detector.m_gaussianKernelSize);
detector.m_gaussianStdev = j.value("gaussianStdev", detector.m_gaussianStdev);
detector.m_lowerThreshold = j.value("lowerThreshold", detector.m_lowerThreshold);
Expand All @@ -234,7 +234,7 @@
*/
friend inline void to_json(nlohmann::json &j, const vpCannyEdgeDetection &detector)
{
std::string filteringAndGradientName = vpImageFilter::vpCannyFilteringAndGradientTypeToString(detector.m_filteringAndGradientType);
std::string filteringAndGradientName = vpImageFilter::vpCannyFiltAndGradTypeToStr(detector.m_filteringAndGradientType);
j = nlohmann::json {
{"filteringAndGradientType", filteringAndGradientName},
{"gaussianSize", detector.m_gaussianKernelSize},
Expand Down
8 changes: 4 additions & 4 deletions modules/core/include/visp3/core/vpImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@
CANNY_COUNT_FILTERING = 2 //! Number of supported backends
} vpCannyFilteringAndGradientType;

static std::string vpCannyFilteringAndGradientTypeList(const std::string &pref = "<", const std::string &sep = " , ",
static std::string vpGetCannyFiltAndGradTypes(const std::string &pref = "<", const std::string &sep = " , ",
const std::string &suf = ">");

static std::string vpCannyFilteringAndGradientTypeToString(const vpCannyFilteringAndGradientType &type);
static std::string vpCannyFiltAndGradTypeToStr(const vpCannyFilteringAndGradientType &type);

static vpCannyFilteringAndGradientType vpCannyFilteringAndGradientTypeFromString(const std::string &name);
static vpCannyFilteringAndGradientType vpCannyFiltAndGradTypeFromStr(const std::string &name);

static void canny(const vpImage<unsigned char> &I, vpImage<unsigned char> &Ic, const unsigned int &gaussianFilterSize,
const float &thresholdCanny, const unsigned int &apertureSobel);
Expand Down Expand Up @@ -305,7 +305,7 @@
}
else {
std::string errMsg = "[vpImageFilter::computePartialDerivatives] Filtering + gradient method \"";
errMsg += vpCannyFilteringAndGradientTypeToString(filteringType);
errMsg += vpCannyFiltAndGradTypeToStr(filteringType);

Check warning on line 308 in modules/core/include/visp3/core/vpImageFilter.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImageFilter.h#L308

Added line #L308 was not covered by tests
errMsg += "\" is not implemented yet\n";
throw(vpException(vpException::notImplementedError, errMsg));
}
Expand Down
133 changes: 79 additions & 54 deletions modules/core/src/image/vpCannyEdgeDetection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
}
else {
std::string errMsg = "[vpCannyEdgeDetection::initGradientFilters] Error: gradient filtering method \"";
errMsg += vpImageFilter::vpCannyFilteringAndGradientTypeToString(m_filteringAndGradientType);
errMsg += vpImageFilter::vpCannyFiltAndGradTypeToStr(m_filteringAndGradientType);

Check warning on line 174 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L174

Added line #L174 was not covered by tests
errMsg += "\" has not been implemented yet\n";
throw vpException(vpException::notImplementedError, errMsg);
}
Expand Down Expand Up @@ -209,7 +209,7 @@

// // Step 1 and 2: filter the image and compute the gradient, if not given by the user
if (!m_areGradientAvailable) {
performFilteringAndGradientComputation(I);
computeFilteringAndGradient(I);

Check warning on line 212 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L212

Added line #L212 was not covered by tests
}
m_areGradientAvailable = false; // Reset for next call

Expand Down Expand Up @@ -238,7 +238,7 @@
}

void
vpCannyEdgeDetection::performFilteringAndGradientComputation(const vpImage<unsigned char> &I)
vpCannyEdgeDetection::computeFilteringAndGradient(const vpImage<unsigned char> &I)

Check warning on line 241 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L241

Added line #L241 was not covered by tests
{
if ((m_filteringAndGradientType == vpImageFilter::CANNY_GBLUR_SOBEL_FILTERING)
|| (m_filteringAndGradientType == vpImageFilter::CANNY_GBLUR_SCHARR_FILTERING)) {
Expand All @@ -254,7 +254,7 @@
}
else {
std::string errmsg("Currently, the filtering operation \"");
errmsg += vpImageFilter::vpCannyFilteringAndGradientTypeToString(m_filteringAndGradientType);
errmsg += vpImageFilter::vpCannyFiltAndGradTypeToStr(m_filteringAndGradientType);

Check warning on line 257 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L257

Added line #L257 was not covered by tests
errmsg += "\" is not handled.";
throw(vpException(vpException::notImplementedError, errmsg));
}
Expand All @@ -273,7 +273,7 @@
* \param[out] dColGradBeta : The offset along the column attached to the beta weight.
*/
void
getInterpolationWeightsAndOffsets(const float &gradientOrientation,
getInterpolWeightsAndOffsets(const float &gradientOrientation,

Check warning on line 276 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L276

Added line #L276 was not covered by tests
float &alpha, float &beta,
int &dRowGradAlpha, int &dRowGradBeta,
int &dColGradAlpha, int &dColGradBeta
Expand Down Expand Up @@ -380,43 +380,56 @@
int nbRows = m_dIx.getRows();
int nbCols = m_dIx.getCols();

bool ignore_current_pixel = false;
bool grad_lower_threshold = false;
for (int row = 0; row < nbRows; ++row) {
for (int col = 0; col < nbCols; ++col) {
// reset the checks
ignore_current_pixel = false;
grad_lower_threshold = false;

if (mp_mask != nullptr) {
if (!(*mp_mask)[row][col]) {
// The mask tells us to ignore the current pixel
continue;
ignore_current_pixel = true;
// continue
}
}
// continue if the mask does not tell us to ignore the current pixel
if (ignore_current_pixel == false) {

// Computing the gradient orientation and magnitude
float grad = getManhattanGradient(m_dIx, m_dIy, row, col);

if (grad < lowerThreshold) {
// The gradient is lower than minimum threshold => ignoring the point
continue;
}
// Computing the gradient orientation and magnitude
float grad = getManhattanGradient(m_dIx, m_dIy, row, col);

Check warning on line 402 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L402

Added line #L402 was not covered by tests

// Getting the offset along the horizontal and vertical axes
// depending on the gradient orientation
int dRowAlphaPlus = 0, dRowBetaPlus = 0;
int dColAphaPlus = 0, dColBetaPlus = 0;
float gradientOrientation = getGradientOrientation(m_dIx, m_dIy, row, col);
float alpha = 0.f, beta = 0.f;
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);
float gradBetaPlus = getManhattanGradient(m_dIx, m_dIy, row + dRowBetaPlus, col + dColBetaPlus);
float gradAlphaMinus = getManhattanGradient(m_dIx, m_dIy, row + dRowAlphaMinus, col + dColAphaMinus);
float gradBetaMinus = getManhattanGradient(m_dIx, m_dIy, row + dRowBetaMinus, col + dColBetaMinus);
float gradPlus = (alpha * gradAlphaPlus) + (beta * gradBetaPlus);
float gradMinus = (alpha * gradAlphaMinus) + (beta * gradBetaMinus);

if ((grad >= gradPlus) && (grad >= gradMinus)) {
// Keeping the edge point that has the highest gradient
std::pair<unsigned int, unsigned int> bestPixel(row, col);
m_edgeCandidateAndGradient[bestPixel] = grad;
if (grad < lowerThreshold) {
// The gradient is lower than minimum threshold => ignoring the point
grad_lower_threshold = true;
// continue
}
if (grad_lower_threshold == false) {
//
// Getting the offset along the horizontal and vertical axes
// depending on the gradient orientation
int dRowAlphaPlus = 0, dRowBetaPlus = 0;
int dColAphaPlus = 0, dColBetaPlus = 0;
float gradientOrientation = getGradientOrientation(m_dIx, m_dIy, row, col);
float alpha = 0.f, beta = 0.f;
getInterpolWeightsAndOffsets(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);
float gradBetaPlus = getManhattanGradient(m_dIx, m_dIy, row + dRowBetaPlus, col + dColBetaPlus);
float gradAlphaMinus = getManhattanGradient(m_dIx, m_dIy, row + dRowAlphaMinus, col + dColAphaMinus);
float gradBetaMinus = getManhattanGradient(m_dIx, m_dIy, row + dRowBetaMinus, col + dColBetaMinus);
float gradPlus = (alpha * gradAlphaPlus) + (beta * gradBetaPlus);
float gradMinus = (alpha * gradAlphaMinus) + (beta * gradBetaMinus);

Check warning on line 425 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L413-L425

Added lines #L413 - L425 were not covered by tests

if ((grad >= gradPlus) && (grad >= gradMinus)) {
// Keeping the edge point that has the highest gradient
std::pair<unsigned int, unsigned int> bestPixel(row, col);
m_edgeCandidateAndGradient[bestPixel] = grad;

Check warning on line 430 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L430

Added line #L430 was not covered by tests
}
}
}
}
}
Expand All @@ -426,7 +439,8 @@
vpCannyEdgeDetection::performHysteresisThresholding(const float &lowerThreshold, const float &upperThreshold)
{
std::map<std::pair<unsigned int, unsigned int>, float>::iterator it;
for (it = m_edgeCandidateAndGradient.begin(); it != m_edgeCandidateAndGradient.end(); ++it) {
std::map<std::pair<unsigned int, unsigned int>, float>::iterator m_edgeCandidateAndGradient_end = m_edgeCandidateAndGradient.end();
for (it = m_edgeCandidateAndGradient.begin(); it != m_edgeCandidateAndGradient_end; ++it) {
if (it->second >= upperThreshold) {
m_edgePointsCandidates[it->first] = STRONG_EDGE;
}
Expand All @@ -440,7 +454,8 @@
vpCannyEdgeDetection::performEdgeTracking()
{
std::map<std::pair<unsigned int, unsigned int>, EdgeType>::iterator it;
for (it = m_edgePointsCandidates.begin(); it != m_edgePointsCandidates.end(); ++it) {
std::map<std::pair<unsigned int, unsigned int>, EdgeType>::iterator m_edgePointsCandidates_end = m_edgePointsCandidates.end();
for (it = m_edgePointsCandidates.begin(); it != m_edgePointsCandidates_end; ++it) {
if (it->second == STRONG_EDGE) {
m_edgeMap[it->first.first][it->first.second] = 255;
}
Expand All @@ -459,37 +474,47 @@
int nbRows = m_dIx.getRows();
int nbCols = m_dIx.getCols();
m_edgePointsCandidates[coordinates] = ON_CHECK;
bool test_row = false;
bool test_col = false;
bool test_drdc = false;
bool edge_in_image_limit = false;
for (int dr = -1; (dr <= 1) && (!hasFoundStrongEdge); ++dr) {
for (int dc = -1; (dc <= 1) && (!hasFoundStrongEdge); ++dc) {
// reset the check for the edge on image limit
edge_in_image_limit = false;

int idRow = dr + static_cast<int>(coordinates.first);
idRow = std::max<int>(idRow, 0); // Avoid getting negative pixel ID
int idCol = dc + static_cast<int>(coordinates.second);
idCol = std::max<int>(idCol, 0); // Avoid getting negative pixel ID

// Checking if we are still looking for an edge in the limit of the image
if (((idRow < 0) || (idRow >= nbRows))
|| ((idCol < 0) || (idCol >= nbCols))
|| ((dr == 0) && (dc == 0))
) {
continue;
test_row = (idRow < 0) || (idRow >= nbRows);
test_col = (idCol < 0) || (idCol >= nbCols);
test_drdc = (dr == 0) && (dc == 0);

Check warning on line 494 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L492-L494

Added lines #L492 - L494 were not covered by tests
if (test_row || test_col || test_drdc) {
edge_in_image_limit = true;
// the continue is replaced by the test
}

try {
std::pair<unsigned int, unsigned int> key_candidate(idRow, idCol);
// Checking if the 8-neighbor point is in the list of edge candidates
EdgeType type_candidate = m_edgePointsCandidates.at(key_candidate);
if (type_candidate == STRONG_EDGE) {
// The 8-neighbor point is a strong edge => the weak edge becomes a strong edge
hasFoundStrongEdge = true;
if (edge_in_image_limit == false) {

try {
std::pair<unsigned int, unsigned int> key_candidate(idRow, idCol);
// Checking if the 8-neighbor point is in the list of edge candidates
EdgeType type_candidate = m_edgePointsCandidates.at(key_candidate);

Check warning on line 504 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L504

Added line #L504 was not covered by tests
if (type_candidate == STRONG_EDGE) {
// The 8-neighbor point is a strong edge => the weak edge becomes a strong edge
hasFoundStrongEdge = true;
}
else if (type_candidate == WEAK_EDGE) {

Check warning on line 509 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L509

Added line #L509 was not covered by tests
// Checking if the WEAK_EDGE neighbor has a STRONG_EDGE neighbor
hasFoundStrongEdge = recursiveSearchForStrongEdge(key_candidate);

Check warning on line 511 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L511

Added line #L511 was not covered by tests
}
}
else if (type_candidate == WEAK_EDGE) {
// Checking if the WEAK_EDGE neighbor has a STRONG_EDGE neighbor
hasFoundStrongEdge = recursiveSearchForStrongEdge(key_candidate);
catch (...) {

Check warning on line 514 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L514

Added line #L514 was not covered by tests
// continue - nothing to do
}
}
catch (...) {
continue;
}
}
}
if (hasFoundStrongEdge) {
Expand Down
15 changes: 7 additions & 8 deletions modules/core/src/image/vpGaussianFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ class vpGaussianFilter::Impl
: m_funcPtrGray(nullptr), m_funcPtrRGBa(nullptr), m_deinterleave(deinterleave)
{
const float epsilon = 0.001f;
{
const size_t channels = 1;
m_funcPtrGray = SimdGaussianBlurInit(width, height, channels, &sigma, &epsilon);
}
{
const size_t channels = 4;
m_funcPtrRGBa = SimdGaussianBlurInit(width, height, channels, &sigma, &epsilon);
}

const size_t channels_1 = 1;
m_funcPtrGray = SimdGaussianBlurInit(width, height, channels_1, &sigma, &epsilon);

const size_t channels_4 = 4;
m_funcPtrRGBa = SimdGaussianBlurInit(width, height, channels_4, &sigma, &epsilon);

if (m_deinterleave) {
m_red.resize(height, width);
m_green.resize(height, width);
Expand Down
Loading
Loading