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

Automatic Gamma correct #1346

Merged
merged 3 commits into from
Mar 11, 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
7 changes: 7 additions & 0 deletions doc/tutorial/imgproc/tutorial-imgproc-brightness.dox
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ The result image is the following:

\image html img-tutorial-brighness-gamma-correction-3.5.png "Left: underexposed image - Right: image corrected with gamma=3.5"

ViSP proposes the implementation of several automatic computation of the gamma factor.
Most of these methods are designed for gray-shade images, so ViSP proposes different way
of handling the colors.

You can test the different methods using the `--gamma-method` option of the tutorial program
and the different way of handling the colors using the `--gamma-color-handling` option.

\section imgproc_brightness_histogram_equalization Histogram equalization

<a href="https://en.wikipedia.org/wiki/Histogram_equalization">Histogram equalization</a> is an image processing method that will adjust the contrast of an image by stretching or shrinking the intensity distribution in order to have a linear cumulative histogram distribution.
Expand Down
181 changes: 178 additions & 3 deletions modules/core/include/visp3/core/vpImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,13 @@
// Return the maximum value within the bitmap
Type getMaxValue(bool onlyFiniteVal = true) const;
// Return the mean value of the bitmap
Type getMeanValue() const;
double getMeanValue() const;
double getMeanValue(const vpImage<bool> *p_mask) const;
double getMeanValue(const vpImage<bool> *p_mask, unsigned int &nbValidPoints) const;
double getStdev() const;
double getStdev(const vpImage<bool> *p_mask) const;
double getStdev(const double &mean) const;
double getStdev(const double &mean, const unsigned int &nbValidPoints, const vpImage<bool> *p_mask) const;
// Return the minumum value within the bitmap
Type getMinValue(bool onlyFiniteVal = true) const;
// Look for the minumum and the maximum value within the bitmap
Expand Down Expand Up @@ -231,6 +237,7 @@

// Get image pixels sum
double getSum() const;
double getSum(const vpImage<bool> *p_mask, unsigned int &nbValidPoints) const;

/*!
Get the image width.
Expand Down Expand Up @@ -939,14 +946,103 @@
/*!
\brief Return the mean value of the bitmap
*/
template <class Type> Type vpImage<Type>::getMeanValue() const
template <class Type> double vpImage<Type>::getMeanValue() const
{
if ((height == 0) || (width == 0))
if ((height == 0) || (width == 0)) {
return 0.0;
}

return getSum() / (height * width);
}

/*!
\brief Return the mean value of the bitmap

\param[in] p_mask A boolean mask that indicates which points must be considered, if set.
*/
template <class Type> double vpImage<Type>::getMeanValue(const vpImage<bool> *p_mask) const
{
unsigned int nbValidPoints = 0;
return getMeanValue(p_mask, nbValidPoints);

Check warning on line 966 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L965-L966

Added lines #L965 - L966 were not covered by tests
}

/*!
\brief Return the mean value of the bitmap

\param[in] p_mask A boolean mask that indicates which points must be considered, if set.
\param[out] nbValidPoints Number of points that are valid according to the boolean mask.
*/
template <class Type> double vpImage<Type>::getMeanValue(const vpImage<bool> *p_mask, unsigned int &nbValidPoints) const

Check warning on line 975 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L975

Added line #L975 was not covered by tests
{
nbValidPoints = 0;
if ((height == 0) || (width == 0)) {

Check warning on line 978 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L977-L978

Added lines #L977 - L978 were not covered by tests
return 0.0;
}

double sum = getSum(p_mask, nbValidPoints);
return sum / nbValidPoints;

Check warning on line 983 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L982-L983

Added lines #L982 - L983 were not covered by tests
}

/*!
* \brief Return the standard deviation of the bitmap
*/
template <class Type> double vpImage<Type>::getStdev() const
{
double mean = getMeanValue();
return getStdev(mean);
}

/*!
* \brief Return the standard deviation of the bitmap
*
* \param[in] p_mask A boolean mask that indicates which points must be considered, if set.
*/
template <class Type> double vpImage<Type>::getStdev(const vpImage<bool> *p_mask) const

Check warning on line 1000 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1000

Added line #L1000 was not covered by tests
{
unsigned int nbValidPoints = 0;
double mean = getMeanValue(p_mask, nbValidPoints);
return getStdev(mean, nbValidPoints, p_mask);

Check warning on line 1004 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1002-L1004

Added lines #L1002 - L1004 were not covered by tests
}

/*!
* \brief Return the standard deviation of the bitmap
*
* \param[in] mean The mean of the image.
*/
template <class Type> double vpImage<Type>::getStdev(const double &mean) const

Check warning on line 1012 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1012

Added line #L1012 was not covered by tests
{
const unsigned int size = width * height;

Check warning on line 1014 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1014

Added line #L1014 was not covered by tests
double sum = 0.;
for (unsigned int i = 0; i < size; ++i) {
sum += (bitmap[i] - mean) * (bitmap[i] - mean);

Check warning on line 1017 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1016-L1017

Added lines #L1016 - L1017 were not covered by tests
}
sum /= static_cast<double>(size);
return std::sqrt(sum);

Check warning on line 1020 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1019-L1020

Added lines #L1019 - L1020 were not covered by tests
}

/*!
* \brief Return the standard deviation of the bitmap
*
* \param[in] mean The mean of the image.
* \param[in] nbValidPoints Number of points that are valid according to the boolean mask.
* \param[in] p_mask A boolean mask that indicates which points must be considered, if set.
*/
template <class Type> double vpImage<Type>::getStdev(const double &mean, const unsigned int &nbValidPoints, const vpImage<bool> *p_mask) const

Check warning on line 1030 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1030

Added line #L1030 was not covered by tests
{
if (p_mask == nullptr) {
return getStdev(mean);

Check warning on line 1033 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1032-L1033

Added lines #L1032 - L1033 were not covered by tests
}
const unsigned int size = width * height;

Check warning on line 1035 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1035

Added line #L1035 was not covered by tests
double sum = 0.;
for (unsigned int i = 0; i < size; ++i) {
if (p_mask->bitmap[i]) {
sum += (bitmap[i] - mean) * (bitmap[i] - mean);

Check warning on line 1039 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1037-L1039

Added lines #L1037 - L1039 were not covered by tests
}
}
sum /= static_cast<double>(nbValidPoints);
return std::sqrt(sum);

Check warning on line 1043 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1042-L1043

Added lines #L1042 - L1043 were not covered by tests
}

/*!
* \brief Return the minimum value within the bitmap
* \param onlyFiniteVal : This parameter is ignored for non double or non float bitmap.
Expand Down Expand Up @@ -1869,6 +1965,34 @@
return res;
}

/**
* Compute the sum of image intensities.
* For vpRGBa image type, compute the sum (R+G+B) of image intensities.
*
* \param[in] p_mask Boolean mask that indicates the valid points by a true flag.
* \param[out] nbValidPoints The number of valid points according to the \b p_mask.
*/
template <class Type> inline double vpImage<Type>::getSum(const vpImage<bool> *p_mask, unsigned int &nbValidPoints) const

Check warning on line 1975 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1975

Added line #L1975 was not covered by tests
{
if ((height == 0) || (width == 0))

Check warning on line 1977 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1977

Added line #L1977 was not covered by tests
return 0.0;
if (p_mask == nullptr) {
nbValidPoints = height * width;

Check warning on line 1980 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1979-L1980

Added lines #L1979 - L1980 were not covered by tests
return getSum();
}

double res = 0.0;
nbValidPoints = 0;
unsigned int size = height * width;
for (unsigned int i = 0; i < size; ++i) {
if (p_mask->bitmap[i]) {
res += static_cast<double>(bitmap[i]);
++nbValidPoints;

Check warning on line 1990 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L1985-L1990

Added lines #L1985 - L1990 were not covered by tests
}
}
return res;
}

/**
* \relates vpImage
*/
Expand All @@ -1884,6 +2008,32 @@
return res;
}

/**
* \relates vpImage
*/
template <> inline double vpImage<vpRGBa>::getSum(const vpImage<bool> *p_mask, unsigned int &nbValidPoints) const

Check warning on line 2014 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L2014

Added line #L2014 was not covered by tests
{
if ((height == 0) || (width == 0)) {

Check warning on line 2016 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L2016

Added line #L2016 was not covered by tests
return 0.0;
}

if (p_mask == nullptr) {
nbValidPoints = height * width;
return getSum();

Check warning on line 2022 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L2020-L2022

Added lines #L2020 - L2022 were not covered by tests
}

double res = 0.0;
nbValidPoints = 0;
unsigned int size = height * width;
for (unsigned int i = 0; i < size; ++i) {
if (p_mask->bitmap[i]) {
res += static_cast<double>(bitmap[i].R) + static_cast<double>(bitmap[i].G) + static_cast<double>(bitmap[i].B);
++nbValidPoints;

Check warning on line 2031 in modules/core/include/visp3/core/vpImage.h

View check run for this annotation

Codecov / codecov/patch

modules/core/include/visp3/core/vpImage.h#L2026-L2031

Added lines #L2026 - L2031 were not covered by tests
}
}
return res;
}

/**
* \relates vpImage
*/
Expand All @@ -1899,6 +2049,31 @@
return res;
}

/**
* \relates vpImage
*/
template <> inline double vpImage<vpRGBf>::getSum(const vpImage<bool> *p_mask, unsigned int &nbValidPoints) const
{
if ((height == 0) || (width == 0)) {
return 0.0;
}
if (p_mask == nullptr) {
nbValidPoints = height * width;
return getSum();
}

double res = 0.0;
nbValidPoints = 0;
unsigned int size = height * width;
for (unsigned int i = 0; i < size; ++i) {
if (p_mask->bitmap[i]) {
res += static_cast<double>(bitmap[i].R) + static_cast<double>(bitmap[i].G) + static_cast<double>(bitmap[i].B);
++nbValidPoints;
}
}
return res;
}

/*!
Operation C = *this - B.

Expand Down
Loading
Loading