Skip to content

Commit

Permalink
Add entropy measure and some computation time stats.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-trinh committed Oct 27, 2024
1 parent 0533dbf commit d1f9401
Showing 1 changed file with 64 additions and 11 deletions.
75 changes: 64 additions & 11 deletions tutorial/imgproc/brightness/tutorial-compare-auto-gamma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,42 @@ void computeCanny(const vpImage<unsigned char> &I, vpCannyEdgeDetection &cannyDe

I_canny_visp = cannyDetector.detect(I);
}

double computeImageEntropy(const vpImage<unsigned char> &I)
{
// https://github.com/dengyueyun666/Image-Contrast-Enhancement/blob/cd2b1eb5bf6396e2fc3b94cd27f73933d5467147/src/Ying_2017_CAIP.cpp#L186-L207
std::vector<int> hist(256, 0);
for (unsigned int i = 0; i < I.getHeight(); i++) {
for (unsigned int j = 0; j < I.getWidth(); j++) {
int bin = I[i][j];
hist[bin]++;
}
}

double N = I.getSize();
double cost = 0;
for (size_t i = 0; i < hist.size(); i++) {
if (hist[i] == 0) {
continue;
}
double p = hist[i] / N;
cost += -p * std::log2(p);
}

return cost;
}
} // namespace

int main(int argc, const char **argv)
{
std::string input = "Sample_low_brightness.png";
std::string output = "Results";
int gaussianKernelSize = 3;
float gaussianStdev = 1.;
float gaussianStdev = 1.0f;
int apertureSize = 3;
bool half = false;
vpImageFilter::vpCannyFilteringAndGradientType filteringType = vpImageFilter::CANNY_GBLUR_SOBEL_FILTERING;
VISP_NAMESPACE_NAME::vpGammaColorHandling gamma_colorspace = VISP_NAMESPACE_NAME::GAMMA_HSV;
bool jpeg = false;

for (int i = 1; i < argc; i++) {
if (std::string(argv[i]) == "--input" && i + 1 < argc) {
Expand Down Expand Up @@ -115,9 +138,6 @@ int main(int argc, const char **argv)
else if (std::string(argv[i]) == "--gamma-rgb") {
gamma_colorspace = VISP_NAMESPACE_NAME::GAMMA_RGB;
}
else if (std::string(argv[i]) == "--jpeg") {
jpeg = true;
}
else if (std::string(argv[i]) == "--output" && i + 1 < argc) {
++i;
output = std::string(argv[i]);
Expand All @@ -131,7 +151,6 @@ int main(int argc, const char **argv)
" [--aperture-size <e.g. 3>]"
" [--canny-filtering-type <0=CANNY_GBLUR_SOBEL_FILTERING, 1=CANNY_GBLUR_SCHARR_FILTERING>]"
" [--gamma-rgb (RGB colorspace, else HSV]"
" [--jpeg (save in jpeg, otherwise png]"
" [--output <folder path> (to save results)]"
<< std::endl;
return EXIT_SUCCESS;
Expand All @@ -145,7 +164,6 @@ int main(int argc, const char **argv)
std::cout << "Aperture size: " << apertureSize << std::endl;
std::cout << "Canny filtering type: " << filteringType << std::endl;
std::cout << "RGB colorspace? " << (gamma_colorspace == VISP_NAMESPACE_NAME::GAMMA_RGB) << std::endl;
std::cout << "Save in jpeg? " << jpeg << std::endl;
std::cout << "Output result folder: " << output << std::endl;

// Canny parameters
Expand Down Expand Up @@ -177,8 +195,12 @@ int main(int argc, const char **argv)

vpIoTools::makeDirectory(output);

const int nb_methods = VISP_NAMESPACE_NAME::GAMMA_METHOD_COUNT - 1; // all except GAMMA_MANUAL
std::vector<std::vector<double>> computation_times(nb_methods);
int nb_images = 0;

vpImage<vpRGBa> I_color_gamma_correction, I_res_stack;
vpImage<unsigned char> I_gray_gamma_correction, dIxy_uchar, I_canny_visp;
vpImage<unsigned char> I_gray, I_gray_gamma_correction, dIxy_uchar, I_canny_visp;
vpImage<vpRGBa> dIxy_uchar_color, I_canny_visp_color;
vpFont font(32);
bool read_single_image = false;
Expand All @@ -193,6 +215,7 @@ int main(int argc, const char **argv)
else {
I_color = I_color_ori;
}
nb_images++;

const int nb_methods = VISP_NAMESPACE_NAME::GAMMA_METHOD_COUNT - 1; // all except GAMMA_MANUAL
I_res_stack.init(nb_methods*I_color.getHeight(), 4*I_color.getWidth());
Expand All @@ -203,9 +226,14 @@ int main(int argc, const char **argv)
int offset_text_start_y = 25;
int text_h = 40;
int offset_idx = 0;
double offset_text1 = 0.01;
double offset_text2 = 0.26;
double start_time = 0, end_time = 0;
char buffer[FILENAME_MAX];

vpImageConvert::convert(I_color, I_gray);
const double img_ori_entropy = computeImageEntropy(I_gray);

for (int gamma_idx = 1; gamma_idx < VISP_NAMESPACE_NAME::GAMMA_METHOD_COUNT; ++gamma_idx, offset_idx++) {
VISP_NAMESPACE_NAME::vpGammaMethod gamma_method = static_cast<VISP_NAMESPACE_NAME::vpGammaMethod>(gamma_idx);
if (gamma_method == VISP_NAMESPACE_NAME::GAMMA_MANUAL) {
Expand All @@ -217,8 +245,12 @@ int main(int argc, const char **argv)
VISP_NAMESPACE_NAME::gammaCorrection(I_color, I_color_gamma_correction, static_cast<float>(gamma),
gamma_colorspace, gamma_method);
end_time = vpTime::measureTimeMs();
std::cout << "Computation time (" << VISP_NAMESPACE_NAME::vpGammaMethodToString(gamma_method)
<< "): " << (end_time-start_time) << " ms" << std::endl;
computation_times[offset_idx].push_back(end_time-start_time);

vpImageConvert::convert(I_color_gamma_correction, I_gray_gamma_correction);
const double img_corrected_entropy = computeImageEntropy(I_gray_gamma_correction);
computeCanny(I_gray_gamma_correction, cannyDetector, gaussianKernelSize, gaussianStdev, apertureSize,
filteringType, dIxy_uchar, I_canny_visp);
vpImageConvert::convert(dIxy_uchar, dIxy_uchar_color);
Expand All @@ -227,19 +259,27 @@ int main(int argc, const char **argv)
I_res_stack.insert(I_color_gamma_correction, vpImagePoint(offset_idx*I_color.getHeight(), I_color.getWidth()));
I_res_stack.insert(I_canny_visp_color, vpImagePoint(offset_idx*I_color.getHeight(), 2*I_color.getWidth()));
I_res_stack.insert(dIxy_uchar_color, vpImagePoint(offset_idx*I_color.getHeight(), 3*I_color.getWidth()));
// Entropy original
snprintf(buffer, FILENAME_MAX, "Entropy: %.4f", img_ori_entropy);
font.drawText(I_res_stack, buffer, vpImagePoint(offset_idx*I_color.getHeight() + offset_text_start_y, offset_text1*I_res_stack.getWidth()), vpColor::red);
// Computation time
std::ostringstream oss;
oss << VISP_NAMESPACE_NAME::vpGammaMethodToString(gamma_method) << " (%.2f ms)";
snprintf(buffer, FILENAME_MAX, oss.str().c_str(), (end_time-start_time));
font.drawText(I_res_stack, buffer, vpImagePoint(offset_idx*I_color.getHeight() + offset_text_start_y,
0.35*I_res_stack.getWidth()), vpColor::red);
offset_text2*I_res_stack.getWidth()), vpColor::red);
// Canny
snprintf(buffer, FILENAME_MAX, "Canny mean: (%.2f)", I_canny_visp.getMeanValue());
font.drawText(I_res_stack, buffer, vpImagePoint(offset_idx*I_color.getHeight() + offset_text_start_y+text_h,
0.35*I_res_stack.getWidth()), vpColor::red);
offset_text2*I_res_stack.getWidth()), vpColor::red);
// Entropy
snprintf(buffer, FILENAME_MAX, "Entropy: %.4f", img_corrected_entropy);
font.drawText(I_res_stack, buffer, vpImagePoint(offset_idx*I_color.getHeight() + offset_text_start_y+2*text_h, offset_text2*I_res_stack.getWidth()), vpColor::red);
}

if (!output.empty()) {
std::stringstream output_filename;
const std::string extension = jpeg ? ".jpeg" : ".png";
const std::string extension = ".jpeg";
if (single_image) {
output_filename << vpIoTools::createFilePath(output, vpIoTools::getNameWE(input)) << extension;
}
Expand All @@ -255,6 +295,19 @@ int main(int argc, const char **argv)
}
}

std::cout << "\nStats:" << std::endl;
std::cout << "Nb images: " << nb_images << std::endl;

for (int gamma_idx = 1; gamma_idx < VISP_NAMESPACE_NAME::GAMMA_METHOD_COUNT; ++gamma_idx) {
VISP_NAMESPACE_NAME::vpGammaMethod gamma_method = static_cast<VISP_NAMESPACE_NAME::vpGammaMethod>(gamma_idx);
if (gamma_method == VISP_NAMESPACE_NAME::GAMMA_MANUAL) {
continue;
}
std::cout << VISP_NAMESPACE_NAME::vpGammaMethodToString(gamma_method) << ": mean="
<< vpMath::getMean(computation_times[gamma_idx+1]) << " ms ; median="
<< vpMath::getMedian(computation_times[gamma_idx+1]) << " ms" << std::endl;
}

return EXIT_SUCCESS;
}
#else
Expand Down

0 comments on commit d1f9401

Please sign in to comment.