Skip to content

Commit

Permalink
Modernize the code (part 1)
Browse files Browse the repository at this point in the history
This is the first batch of changes to update the code in preparation for multi-type image code changes.
  • Loading branch information
ihhub committed Mar 17, 2024
1 parent 57d3f26 commit 690e967
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 178 deletions.
35 changes: 18 additions & 17 deletions src/edge_detection.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/***************************************************************************
* penguinV: https://github.com/ihhub/penguinV *
* Copyright (C) 2017 - 2022 *
* Copyright (C) 2017 - 2024 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
Expand Down Expand Up @@ -228,11 +228,11 @@ namespace
getDerivatives( data, first, second );
getEdgePoints( negative, data, first, second, edgeParameter );
removeSimilarPoints( negative );
if ( ( forwardDirection && edgeParameter.edge == EdgeParameter::FIRST ) || ( !forwardDirection && edgeParameter.edge == EdgeParameter::LAST ) ) {
if ( ( forwardDirection && edgeParameter.edge == EdgeParameter::EdgeType::FIRST ) || ( !forwardDirection && edgeParameter.edge == EdgeParameter::EdgeType::LAST ) ) {
leaveFirstElement( positive );
leaveLastElement( negative );
}
else if ( ( forwardDirection && edgeParameter.edge == EdgeParameter::LAST ) || ( !forwardDirection && edgeParameter.edge == EdgeParameter::FIRST ) ) {
else if ( ( forwardDirection && edgeParameter.edge == EdgeParameter::EdgeType::LAST ) || ( !forwardDirection && edgeParameter.edge == EdgeParameter::EdgeType::FIRST ) ) {
leaveLastElement( positive );
leaveFirstElement( negative );
}
Expand All @@ -246,7 +246,8 @@ namespace
Image_Function::ValidateImageParameters( image, x, y, width, height );
edgeParameter.verify();

const bool horizontalEdgeDetectionBase = ( edgeParameter.direction == EdgeParameter::LEFT_TO_RIGHT || edgeParameter.direction == EdgeParameter::RIGHT_TO_LEFT );
const bool horizontalEdgeDetectionBase
= ( edgeParameter.direction == EdgeParameter::DirectionType::LEFT_TO_RIGHT || edgeParameter.direction == EdgeParameter::DirectionType::RIGHT_TO_LEFT );

if ( ( horizontalEdgeDetectionBase && ( width < 4u ) ) || ( !horizontalEdgeDetectionBase && ( height < 4u ) ) )
return;
Expand Down Expand Up @@ -279,22 +280,22 @@ namespace
std::vector<_Type> edgePositive;
std::vector<_Type> edgeNegative;
findEdgePoints( edgePositive, edgeNegative, data, firstDerivative, secondDerivative, edgeParameter,
( edgeParameter.direction == EdgeParameter::LEFT_TO_RIGHT ) );
( edgeParameter.direction == EdgeParameter::DirectionType::LEFT_TO_RIGHT ) );

const _Type yPosition = static_cast<_Type>( y + rowId + ( edgeParameter.groupFactor - 1 ) / 2.0 );

if ( edgeParameter.direction == EdgeParameter::LEFT_TO_RIGHT ) {
if ( edgeParameter.gradient == EdgeParameter::POSITIVE || edgeParameter.gradient == EdgeParameter::ANY )
if ( edgeParameter.direction == EdgeParameter::DirectionType::LEFT_TO_RIGHT ) {
if ( edgeParameter.gradient == EdgeParameter::GradientType::POSITIVE || edgeParameter.gradient == EdgeParameter::GradientType::ANY )
createPositiveXEdge( edgePositive, positiveEdgePoint, static_cast<_Type>( x ), yPosition );

if ( edgeParameter.gradient == EdgeParameter::NEGATIVE || edgeParameter.gradient == EdgeParameter::ANY )
if ( edgeParameter.gradient == EdgeParameter::GradientType::NEGATIVE || edgeParameter.gradient == EdgeParameter::GradientType::ANY )
createNegativeXEdge( edgeNegative, negativeEdgePoint, static_cast<_Type>( x + width - 1 ), yPosition );
}
else {
if ( edgeParameter.gradient == EdgeParameter::POSITIVE || edgeParameter.gradient == EdgeParameter::ANY )
if ( edgeParameter.gradient == EdgeParameter::GradientType::POSITIVE || edgeParameter.gradient == EdgeParameter::GradientType::ANY )
createNegativeXEdge( edgeNegative, positiveEdgePoint, static_cast<_Type>( x + width - 1 ), yPosition );

if ( edgeParameter.gradient == EdgeParameter::NEGATIVE || edgeParameter.gradient == EdgeParameter::ANY )
if ( edgeParameter.gradient == EdgeParameter::GradientType::NEGATIVE || edgeParameter.gradient == EdgeParameter::GradientType::ANY )
createPositiveXEdge( edgePositive, negativeEdgePoint, static_cast<_Type>( x ), yPosition );
}
}
Expand Down Expand Up @@ -324,30 +325,30 @@ namespace
std::vector<_Type> edgePositive;
std::vector<_Type> edgeNegative;
findEdgePoints( edgePositive, edgeNegative, data, firstDerivative, secondDerivative, edgeParameter,
( edgeParameter.direction == EdgeParameter::TOP_TO_BOTTOM ) );
( edgeParameter.direction == EdgeParameter::DirectionType::TOP_TO_BOTTOM ) );

const _Type xPosition = static_cast<_Type>( x + rowId + ( edgeParameter.groupFactor - 1 ) / 2.0 );

if ( edgeParameter.direction == EdgeParameter::TOP_TO_BOTTOM ) {
if ( edgeParameter.gradient == EdgeParameter::POSITIVE || edgeParameter.gradient == EdgeParameter::ANY )
if ( edgeParameter.direction == EdgeParameter::DirectionType::TOP_TO_BOTTOM ) {
if ( edgeParameter.gradient == EdgeParameter::GradientType::POSITIVE || edgeParameter.gradient == EdgeParameter::GradientType::ANY )
createPositiveYEdge( edgePositive, positiveEdgePoint, xPosition, static_cast<_Type>( y ) );

if ( edgeParameter.gradient == EdgeParameter::NEGATIVE || edgeParameter.gradient == EdgeParameter::ANY )
if ( edgeParameter.gradient == EdgeParameter::GradientType::NEGATIVE || edgeParameter.gradient == EdgeParameter::GradientType::ANY )
createNegativeYEdge( edgeNegative, negativeEdgePoint, xPosition, static_cast<_Type>( y + height - 1 ) );
}
else {
if ( edgeParameter.gradient == EdgeParameter::POSITIVE || edgeParameter.gradient == EdgeParameter::ANY )
if ( edgeParameter.gradient == EdgeParameter::GradientType::POSITIVE || edgeParameter.gradient == EdgeParameter::GradientType::ANY )
createNegativeYEdge( edgeNegative, positiveEdgePoint, xPosition, static_cast<_Type>( y + height - 1 ) );

if ( edgeParameter.gradient == EdgeParameter::NEGATIVE || edgeParameter.gradient == EdgeParameter::ANY )
if ( edgeParameter.gradient == EdgeParameter::GradientType::NEGATIVE || edgeParameter.gradient == EdgeParameter::GradientType::ANY )
createPositiveYEdge( edgePositive, negativeEdgePoint, xPosition, static_cast<_Type>( y ) );
}
}
}
}
}

EdgeParameter::EdgeParameter( directionType _direction, gradientType _gradient, edgeType _edge, uint32_t _groupFactor, uint32_t _skipFactor,
EdgeParameter::EdgeParameter( DirectionType _direction, GradientType _gradient, EdgeType _edge, uint32_t _groupFactor, uint32_t _skipFactor,
uint32_t _contrastCheckLeftSideOffset, uint32_t _contrastCheckRightSideOffset, uint8_t _minimumContrast )
: direction( _direction )
, gradient( _gradient )
Expand Down
45 changes: 28 additions & 17 deletions src/edge_detection.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/***************************************************************************
* penguinV: https://github.com/ihhub/penguinV *
* Copyright (C) 2017 - 2022 *
* Copyright (C) 2017 - 2024 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
Expand All @@ -27,46 +27,57 @@
struct EdgeParameter
{
// Direction of scanning for edges
enum directionType
enum class DirectionType : uint8_t
{
LEFT_TO_RIGHT = 0,
LEFT_TO_RIGHT,
RIGHT_TO_LEFT,
TOP_TO_BOTTOM,
BOTTOM_TO_TOP
};

// Type of edge to detect based on gradient: positive means that pixel intensity increases while for negative it decreases. Any: positive and negative
enum gradientType
enum class GradientType : uint8_t
{
POSITIVE = 4,
POSITIVE,
NEGATIVE,
ANY
};

// Type of edge to find per scanning row. First means first edge point in a row, last - last edge point. Set all to catch all edge points in a row
enum edgeType
enum class EdgeType : uint8_t
{
FIRST = 7,
FIRST,
LAST,
ALL
};

EdgeParameter( directionType _direction = LEFT_TO_RIGHT, gradientType _gradient = ANY, edgeType _edge = ALL, uint32_t _groupFactor = 1u, uint32_t _skipFactor = 1u,
uint32_t _contrastCheckLeftSideOffset = 0u, uint32_t _contrastCheckRightSideOffset = 0u, uint8_t _minimumContrast = 10 );
EdgeParameter( DirectionType _direction = DirectionType::LEFT_TO_RIGHT, GradientType _gradient = GradientType::ANY, EdgeType _edge = EdgeType::ALL,
uint32_t _groupFactor = 1u, uint32_t _skipFactor = 1u, uint32_t _contrastCheckLeftSideOffset = 0u, uint32_t _contrastCheckRightSideOffset = 0u,
uint8_t _minimumContrast = 10 );

DirectionType direction;

GradientType gradient;

EdgeType edge;

// Grouping per row or column (depending on direction) works as a median filter. Default is 1 - no grouping.
uint32_t groupFactor;

// Skip specific number of rows or columns to do not find edge points on all rows/columns. Default is 1 - no skipping.
uint32_t skipFactor;

directionType direction;
gradientType gradient;
edgeType edge;
uint32_t groupFactor; // grouping per row or column (depending on direction) works as a median filter. Default is 1 - no grouping
uint32_t skipFactor; // skip specific number of rows or columns to do not find edge points on all rows/columns. Default is 1 - no skipping
// Specify a number of pixels from each side of potential edge point to get pixel intensity needed for contrast verification
// Such offsets are useful for very smooth edge when pixel intensity increases very slowly per pixel
// Default values are 0
uint32_t contrastCheckLeftSideOffset;
uint32_t contrastCheckRightSideOffset;
uint8_t minimumContrast; // minimun contrast needed to detect edge

void verify() const; // self-verification that all parameters are correct
// Minimun contrast needed to detect edge.
uint8_t minimumContrast;

// Self-verification that all parameters are correct.
void verify() const;
};

template <typename _Type>
Expand Down Expand Up @@ -115,4 +126,4 @@ class EdgeDetectionBase
std::vector<PointBase2D<_Type>> negativeEdgePoint;
};

typedef EdgeDetectionBase<double> EdgeDetection;
using EdgeDetection = EdgeDetectionBase<double>;
Loading

0 comments on commit 690e967

Please sign in to comment.