Skip to content

Commit 5a28ad8

Browse files
authored
Merge pull request #330 from lasp/feature/1135-brightness-increase-threshold
Feature/1135 brightness increase threshold
2 parents 099e23d + 9b7bcd8 commit 5a28ad8

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

src/fswAlgorithms/imageProcessing/centerOfBrightness/_UnitTest/test_centerOfBrightness.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def centerOfBrightnessTest(show_plots, image, blur, saveTest, validImage, saveIm
9494
if windowCenter.all() != 0 and windowWidth != 0 and windowHeight != 0:
9595
moduleConfig.setWindowCenter(windowCenter)
9696
moduleConfig.setWindowSize(windowWidth, windowHeight)
97+
brightnessIncreaseThreshold = 0.0
98+
moduleConfig.setRelativeBrightnessIncreaseThreshold(brightnessIncreaseThreshold)
9799
unitTestSim.AddModelToTask(unitTaskName, moduleConfig)
98100

99101
numberOfPointsBrightnessAverage = 3
@@ -131,7 +133,7 @@ def centerOfBrightnessTest(show_plots, image, blur, saveTest, validImage, saveIm
131133

132134
# run simulation for 5 time steps (excluding initial time step at 0 ns), scale brightness each time step
133135
# necessary to test rolling brightness average
134-
scaler = np.array([1.0, 0.9, 0.8, 0.7, 0.6])
136+
scaler = np.array([0.5, 0.6, 0.8, 0.3, 0.9])
135137
brightness_ref = np.zeros([len(scaler)])
136138
brightnessAverage_ref = np.zeros([len(scaler)])
137139
for i in range(0, len(scaler)):
@@ -150,8 +152,8 @@ def centerOfBrightnessTest(show_plots, image, blur, saveTest, validImage, saveIm
150152
lower_idx = max(0, i-(numberOfPointsBrightnessAverage-1))
151153
brightnessAverage_ref[i] = np.mean(brightness_ref[lower_idx:i+1])
152154

153-
center = dataLog.centerOfBrightness[-1,:]
154-
pixelNum = dataLog.pixelsFound[-1]
155+
center = dataLog.centerOfBrightness[0, :]
156+
pixelNum = dataLog.pixelsFound[0]
155157
brightnessAverage = dataLog.rollingAverageBrightness
156158

157159
output_image = Image.new("RGB", input_image.size)
@@ -203,7 +205,7 @@ def centerOfBrightnessTest(show_plots, image, blur, saveTest, validImage, saveIm
203205

204206
np.testing.assert_allclose(brightnessAverage,
205207
brightnessAverage_ref,
206-
rtol=tolerance,
208+
rtol=0.001,
207209
atol=0,
208210
err_msg='Variable: brightnessAverage',
209211
verbose=True)

src/fswAlgorithms/imageProcessing/centerOfBrightness/centerOfBrightness.cpp

+38-7
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,29 @@ void CenterOfBrightness::UpdateState(uint64_t CurrentSimNanos)
9292
if (!locations.empty()){
9393
std::pair<Eigen::Vector2d, double> cobData;
9494
cobData = this->computeWeightedCenterOfBrightness(locations);
95+
96+
double averageBrightnessOld = 0.0;
97+
if (this->brightnessHistory.rows() > 0){
98+
averageBrightnessOld = this->brightnessHistory.mean();
99+
}
95100
this->updateBrightnessHistory(cobData.second);
101+
double averageBrightnessNew = this->brightnessHistory.mean();
102+
double brightnessIncrease = 0.0;
103+
if (averageBrightnessOld > 0.0){
104+
brightnessIncrease = (averageBrightnessNew - averageBrightnessOld)
105+
/ averageBrightnessOld;
106+
}
96107

97-
cobBuffer.valid = true;
98-
cobBuffer.timeTag = this->sensorTimeTag;
99-
cobBuffer.cameraID = imageBuffer.cameraID;
100-
cobBuffer.centerOfBrightness[0] = cobData.first[0];
101-
cobBuffer.centerOfBrightness[1] = cobData.first[1];
102-
cobBuffer.pixelsFound = static_cast<int32_t> (locations.size());
103-
cobBuffer.rollingAverageBrightness = this->brightnessHistory.mean();
108+
/*! If brightness increase is less than brightness increase threshold, do not validate image */
109+
if (brightnessIncrease >= this->relativeBrightnessIncreaseThreshold){
110+
cobBuffer.valid = true;
111+
cobBuffer.timeTag = this->sensorTimeTag;
112+
cobBuffer.cameraID = imageBuffer.cameraID;
113+
cobBuffer.centerOfBrightness[0] = cobData.first[0];
114+
cobBuffer.centerOfBrightness[1] = cobData.first[1];
115+
cobBuffer.pixelsFound = static_cast<int32_t> (locations.size());
116+
}
117+
cobBuffer.rollingAverageBrightness = averageBrightnessNew;
104118
}
105119

106120
this->opnavCOBOutMsg.write(&cobBuffer, this->moduleID, CurrentSimNanos);
@@ -273,3 +287,20 @@ Eigen::VectorXi CenterOfBrightness::getWindowSize() const
273287
Eigen::VectorXi center = {this->windowWidth, this->windowHeight};
274288
return center;
275289
}
290+
291+
/*! Set threshold for the increase in brightness for images not to be invalidated
292+
@param double increaseThreshold
293+
@return void
294+
*/
295+
void CenterOfBrightness::setRelativeBrightnessIncreaseThreshold(double increaseThreshold)
296+
{
297+
this->relativeBrightnessIncreaseThreshold = increaseThreshold;
298+
}
299+
300+
/*! Get threshold for the increase in brightness for images not to be invalidated
301+
@return double increaseThreshold
302+
*/
303+
double CenterOfBrightness::getRelativeBrightnessIncreaseThreshold() const
304+
{
305+
return this->relativeBrightnessIncreaseThreshold;
306+
}

src/fswAlgorithms/imageProcessing/centerOfBrightness/centerOfBrightness.h

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class CenterOfBrightness: public SysModel {
4545
Eigen::VectorXi getWindowCenter() const;
4646
void setWindowSize(int32_t width, int32_t height);
4747
Eigen::VectorXi getWindowSize() const;
48+
void setRelativeBrightnessIncreaseThreshold(double increaseThreshold);
49+
double getRelativeBrightnessIncreaseThreshold() const;
4850

4951
private:
5052
std::vector<cv::Vec2i> extractBrightPixels(cv::Mat image);
@@ -75,6 +77,7 @@ class CenterOfBrightness: public SysModel {
7577
Eigen::Vector2i windowPointBottomRight{}; //!< [px] bottom right point of window
7678
bool validWindow = false; //!< [px] true if window is set, false if center, height, or width equal 0
7779
Eigen::VectorXd brightnessHistory{}; //!< [-] brightness history to be used for rolling average
80+
double relativeBrightnessIncreaseThreshold{}; //!< [-] minimum relative brightness increase (if less, invalidated)
7881
/* OpenCV specific arguments needed for finding all non-zero pixels*/
7982
cv::Mat imageGray; //!< [cv mat] Gray scale image for weighting
8083
};

src/fswAlgorithms/imageProcessing/centerOfBrightness/centerOfBrightness.rst

+7-1
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ The normalized total brightness is equal to
6363
I_\mathrm{tot, normalized} = \frac{I_\mathrm{tot}}{255}
6464
6565
and the rolling average is computed over the last :math:`N` time steps, as specified by numberOfPointsBrightnessAverage.
66+
If the relative increase of the rolling brightness average from one time step to the next is below the threshold
67+
brightnessIncreaseThreshold, the image is tagged as invalid.
6668

67-
If the incomping image is not valid, or there were no pixels above the threshold, the image is tagged as invalid.
69+
If the incoming image is not valid, or there were no pixels above the threshold, the image is tagged as invalid.
6870
Downstream algorithms can therefore know when to skip a measurement.
6971

7072
User Guide
@@ -93,6 +95,10 @@ This section is to outline the steps needed to setup a Center of Brightness in P
9395

9496
cobAlgorithm.numberOfPointsBrightnessAverage = 5
9597

98+
#. Specify the minimum relative brightness increase of the rolling average of total brightness (optional)::
99+
100+
moduleConfig.setRelativeBrightnessIncreaseThreshold(0.1)
101+
96102
#. Subscribe to the image message output by the camera model or visualization interface::
97103

98104
cobAlgorithm.imageInMsg.subscribeTo(imgInMsg)

0 commit comments

Comments
 (0)