Skip to content

Commit 1a6f5e4

Browse files
committed
Img_processing: Refactor Distance and Velocity classes
- Removed singleton from Distance class - Enhanced Velocity to support null values - Suppressed speed calculations and alerts when insufficient distance data is available
1 parent 1c54d31 commit 1a6f5e4

File tree

9 files changed

+65
-60
lines changed

9 files changed

+65
-60
lines changed

img_processing/include/distance.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,13 @@
99
class Distance {
1010
public:
1111
void findDistance(std::vector<ObjectInformation> &objectInformation);
12-
static Distance &getInstance(const cv::Mat &image = cv::Mat());
12+
void setFocalLength(const cv::Mat &image);
1313
void setFocalLength(double focalLength);
1414
void drawDistance(std::shared_ptr<cv::Mat> image,
1515
std::vector<ObjectInformation> &objects);
1616

1717
private:
18-
static Distance *instance;
1918
double focalLength;
20-
21-
Distance(const cv::Mat &image);
22-
Distance(const Distance &) = delete;
23-
Distance &operator=(const Distance &) = delete;
2419
void findFocalLength(const cv::Mat &image);
2520
void addDistance(float distance, ObjectInformation &obj);
2621
};

img_processing/include/manager.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,22 @@ class Manager {
2929
Detector detector;
3030
Velocity velocity;
3131
DynamicTracker dynamicTracker;
32+
Distance distance;
3233
Alerter alerter;
3334
int iterationCnt;
3435
uint32_t destID;
3536
uint32_t processID;
37+
bool isTravel;
3638

3739
// Moves the current image to the prevFrame
3840
// and clears the memory of the currentFrame;
3941
void prepareForTheNext();
4042
int drawOutput();
41-
bool isDetect(bool isTravel);
42-
bool isResetTracker(bool isTravel);
43-
bool isTrack(bool isTravel);
43+
bool isDetect();
44+
bool isResetTracker();
45+
bool isTrack();
46+
bool isCalcVelocity();
47+
4448
void sendAlerts(std::vector<std::vector<uint8_t>> &alerts);
4549
void runOnVideo(std::string videoPath);
4650
int readIdFromJson(const char *target);

img_processing/include/object_information_struct.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <opencv2/opencv.hpp>
55
#include "object_type_enum.h"
6+
#include <optional>
67

78
#define MAX_PREV_DISTANCES_SIZE 10
89
#define MAX_PREV_VELOCITIES_SIZE 2
@@ -15,7 +16,7 @@ struct ObjectInformation {
1516
std::deque<float> prevDistances;
1617
float distance;
1718
std::deque<float> prevVelocities;
18-
float velocity;
19+
std::optional<float> velocity = std::nullopt;
1920
};
2021

2122
#endif // __OBJECT_INFORMATION_STRUCT_H__

img_processing/src/alerter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ vector<vector<uint8_t>> Alerter::sendAlerts(
3131
if (isSendAlert(objectInformation)) {
3232
vector<uint8_t> alertBuffer = makeAlertBuffer(
3333
objectInformation.type, objectInformation.distance,
34-
objectInformation.velocity);
34+
objectInformation.velocity.value());
3535
alerts.push_back(alertBuffer);
3636
}
3737
}

img_processing/src/distance.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,10 @@
1010
using namespace std;
1111
using namespace cv;
1212

13-
Distance *Distance::instance = nullptr;
14-
15-
Distance::Distance(const cv::Mat &image)
13+
void Distance::setFocalLength(const Mat &image)
1614
{
1715
findFocalLength(image);
1816
}
19-
20-
Distance &Distance::getInstance(const cv::Mat &image)
21-
{
22-
if (!instance) {
23-
if (image.empty()) {
24-
LogManager::logErrorMessage(ErrorType::IMAGE_ERROR,
25-
"Could not load image");
26-
throw std::runtime_error(
27-
"Could not load image. Distance instance creation failed.");
28-
}
29-
else
30-
instance = new Distance(image);
31-
}
32-
return *instance;
33-
}
34-
3517
void Distance::setFocalLength(double focalLength)
3618
{
3719
this->focalLength = focalLength;

img_processing/src/manager.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void Manager::init()
3131
LogManager::logErrorMessage(ErrorType::IMAGE_ERROR, "image not found");
3232
return;
3333
}
34-
Distance &distance = Distance::getInstance(calibrationImage);
34+
distance.setFocalLength(calibrationImage);
3535
iterationCnt = 1;
3636
bool isCuda = false;
3737
detector.init(isCuda);
@@ -67,7 +67,6 @@ void Manager::mainDemo()
6767
return;
6868
}
6969
// intialize focal length
70-
Distance &distance = Distance::getInstance();
7170
distance.setFocalLength(focalLength);
7271
runOnVideo(videoPath);
7372
}
@@ -104,21 +103,28 @@ void Manager::runOnVideo(string videoPath)
104103
}
105104
}
106105

107-
bool Manager::isDetect(bool isTravel)
106+
bool Manager::isDetect()
108107
{
109108
if (!isTravel || iterationCnt == 1)
110109
return true;
111110
return false;
112111
}
113112

114-
bool Manager::isResetTracker(bool isTravel)
113+
bool Manager::isResetTracker()
115114
{
116115
if (isTravel && iterationCnt == 1)
117116
return true;
118117
return false;
119118
}
120119

121-
bool Manager::isTrack(bool isTravel)
120+
bool Manager::isTrack()
121+
{
122+
if (isTravel && iterationCnt > 1)
123+
return true;
124+
return false;
125+
}
126+
127+
bool Manager::isCalcVelocity()
122128
{
123129
if (isTravel && iterationCnt > 1)
124130
return true;
@@ -127,31 +133,36 @@ bool Manager::isTrack(bool isTravel)
127133

128134
int Manager::processing(const Mat &newFrame, bool isTravel)
129135
{
130-
Distance &distance = Distance::getInstance();
136+
this->isTravel = isTravel;
131137
currentFrame = make_shared<Mat>(newFrame);
132-
if (isDetect(isTravel)) {
138+
if (isDetect()) {
133139
// send the frame to detect
134140
detector.detect(this->currentFrame, isTravel);
135141
this->currentOutput = detector.getOutput();
136142
}
137143

138-
if (isResetTracker(isTravel)) {
144+
if (isResetTracker()) {
139145
// prepare the tracker
140146
dynamicTracker.startTracking(this->currentFrame, this->currentOutput);
141147
}
142148

143-
if (isTrack(isTravel)) {
149+
if (isTrack()) {
144150
// send the frame to track
145151
dynamicTracker.tracking(this->currentFrame, this->currentOutput);
146152
}
147153

148154
// add distance to detection objects
149155
distance.findDistance(this->currentOutput);
150-
velocity.returnVelocities(this->currentOutput);
156+
if (isCalcVelocity()) {
157+
velocity.returnVelocities(this->currentOutput);
158+
}
151159

152160
// send allerts to main control
153-
vector<vector<uint8_t>> alerts = alerter.sendAlerts(this->currentOutput);
154-
sendAlerts(alerts);
161+
if (isCalcVelocity()) {
162+
vector<vector<uint8_t>> alerts =
163+
alerter.sendAlerts(this->currentOutput);
164+
sendAlerts(alerts);
165+
}
155166

156167
// update of the iterationCnt
157168
if (isTravel) {
@@ -166,10 +177,10 @@ int Manager::processing(const Mat &newFrame, bool isTravel)
166177

167178
int Manager::drawOutput()
168179
{
169-
Distance &distance = Distance::getInstance();
170180
dynamicTracker.drawTracking(currentFrame, currentOutput);
171181
distance.drawDistance(currentFrame, currentOutput);
172-
velocity.drawVelocity(currentFrame, currentOutput);
182+
if (isCalcVelocity())
183+
velocity.drawVelocity(currentFrame, currentOutput);
173184

174185
// Legend
175186
int legendX = 10, legendY = 10;

img_processing/src/velocity.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,28 +83,41 @@ void Velocity::updateVelocity(float newVelocity, ObjectInformation &obj)
8383
}
8484
}
8585

86-
void Velocity::drawVelocity(shared_ptr<Mat> image,
87-
vector<ObjectInformation> &objects)
86+
void Velocity::drawVelocity(std::shared_ptr<Mat> image,
87+
std::vector<ObjectInformation> &objects)
8888
{
8989
// Font properties
9090
int fontFace = FONT_HERSHEY_SIMPLEX;
9191
double fontScale = 0.6;
9292
int thickness = 2;
9393
int baseline = 0;
94+
9495
// Calculate text sizes
9596
Size velocityTextSize =
9697
getTextSize("velocity", fontFace, fontScale, thickness, &baseline);
98+
9799
for (auto &obj : objects) {
98-
std::stringstream ssVelocity;
99-
ssVelocity << std::fixed << std::setprecision(2) << obj.velocity;
100+
// Check if velocity has a value
101+
if (obj.velocity.has_value()) {
102+
std::stringstream ssVelocity;
103+
ssVelocity << std::fixed << std::setprecision(2)
104+
<< obj.velocity.value();
100105

101-
Point velocityTextOrg(obj.position.x + 5, obj.position.y - 7);
106+
Point velocityTextOrg(obj.position.x + 5, obj.position.y - 7);
102107

103-
// Draw outline for velocity text
104-
putText(*image, ssVelocity.str(), velocityTextOrg, fontFace, fontScale,
105-
Scalar(0, 0, 0), thickness + 3);
106-
// Write the velocity text
107-
putText(*image, ssVelocity.str(), velocityTextOrg, fontFace, fontScale,
108-
Scalar(255, 255, 0), thickness);
108+
// Draw outline for velocity text
109+
putText(*image, ssVelocity.str(), velocityTextOrg, fontFace,
110+
fontScale, Scalar(0, 0, 0), thickness + 3);
111+
// Write the velocity text
112+
putText(*image, ssVelocity.str(), velocityTextOrg, fontFace,
113+
fontScale, Scalar(255, 255, 0), thickness);
114+
}
115+
else {
116+
// Optional: Handle the case where velocity is not set
117+
// For example, you can draw a placeholder or skip drawing.
118+
Point velocityTextOrg(obj.position.x + 5, obj.position.y - 7);
119+
putText(*image, "N/A", velocityTextOrg, fontFace, fontScale,
120+
Scalar(255, 0, 0), thickness); // Draw "N/A" in red
121+
}
109122
}
110123
}

img_processing/tests/test_distance.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ TEST(DistanceTest, DistanceWithCalibration)
2121
throw runtime_error("Could not open or find the image");
2222
}
2323

24-
Distance &distance = Distance::getInstance(calibrationImage);
25-
24+
Distance distance;
25+
distance.setFocalLength(calibrationImage);
2626
// Load a real image from file
2727
string imagePath2 = "../tests/images/parking_car.JPG";
2828
Mat carImage;

img_processing/tests/test_velocity.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ TEST(TVelocity, calculate_TVelocity)
1919
if (calibrationImage.empty()) {
2020
throw runtime_error("Could not open or find the image");
2121
}
22-
23-
Distance &distance = Distance::getInstance(calibrationImage);
24-
22+
Distance distance;
23+
distance.setFocalLength(calibrationImage);
2524
Detector detector;
2625
DynamicTracker tracker;
2726
Velocity velocity;

0 commit comments

Comments
 (0)