#include <iostream>
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpImage.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/core/vpImageTools.h>
#include <visp3/core/vpCannyEdgeDetection.h>
#include <visp3/core/vpImageFilter.h>
#include <visp3/core/vpFont.h>
#include <visp3/io/vpImageIo.h>
#include <visp3/io/vpVideoReader.h>
#if defined(VISP_HAVE_MODULE_IMGPROC) && defined(VISP_HAVE_SIMDLIB) && \
((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
#include <visp3/imgproc/vpImgproc.h>
#include <memory>
#ifdef ENABLE_VISP_NAMESPACE
#endif
namespace
{
template <class T>
void computeMeanMaxStdev(
const vpImage<T> &I,
float &mean,
float &max,
float &stdev)
{
max = std::numeric_limits<float>::epsilon();
mean = 0.;
stdev = 0.;
float scale = 1.f / (static_cast<float>(nbRows) * static_cast<float>(nbCols));
for (unsigned int r = 0; r < nbRows; r++) {
for (unsigned int c = 0; c < nbCols; c++) {
mean += I[r][c];
max = std::max<float>(max, static_cast<float>(I[r][c]));
}
}
mean *= scale;
for (unsigned int r = 0; r < nbRows; r++) {
for (unsigned int c = 0; c < nbCols; c++) {
stdev += (I[r][c] - mean) * (I[r][c] - mean);
}
}
stdev *= scale;
stdev = std::sqrt(stdev);
}
{
apertureSize, filteringType);
for (
unsigned int i = 0; i < dIx.
getHeight(); i++) {
for (
unsigned int j = 0; j < dIx.
getWidth(); j++) {
dIxy[i][j] = std::sqrt(dIx[i][j]*dIx[i][j] + dIy[i][j]*dIy[i][j]);
}
}
float mean, max, stdev;
computeMeanMaxStdev(dIxy, mean, max, stdev);
I_canny_visp = cannyDetector.
detect(I);
}
{
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 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;
}
}
int main(int argc, const char **argv)
{
std::string input = "Sample_low_brightness.png";
std::string output = "Results";
int gaussianKernelSize = 3;
float gaussianStdev = 1.0f;
int apertureSize = 3;
bool half = false;
for (int i = 1; i < argc; i++) {
if (std::string(argv[i]) == "--input" && i + 1 < argc) {
++i;
input = std::string(argv[i]);
}
else if (std::string(argv[i]) == "--half") {
half = true;
}
else if (std::string(argv[i]) == "--gaussian-kernel-size" && i + 1 < argc) {
++i;
gaussianKernelSize = std::atoi(argv[i]);
}
else if (std::string(argv[i]) == "--gaussian-std" && i + 1 < argc) {
++i;
gaussianStdev = static_cast<float>(std::atof(argv[i]));
}
else if (std::string(argv[i]) == "--aperture-size" && i + 1 < argc) {
++i;
apertureSize = std::atoi(argv[i]);
}
else if (std::string(argv[i]) == "--canny-filtering-type" && i + 1 < argc) {
++i;
int type = std::atoi(argv[i]);
if (type == 1) {
}
}
else if (std::string(argv[i]) == "--gamma-rgb") {
}
else if (std::string(argv[i]) == "--output" && i + 1 < argc) {
++i;
output = std::string(argv[i]);
}
else {
std::cout << "Usage: " << argv[0]
<< " [--input <input path or image sequence pattern>]"
" [--half (use half image resolution)]"
" [--gaussian-kernel-size <e.g. 3, 5, 7>]"
" [--gaussian-std <e.g. 1>]"
" [--aperture-size <e.g. 3>]"
" [--canny-filtering-type <0=CANNY_GBLUR_SOBEL_FILTERING, 1=CANNY_GBLUR_SCHARR_FILTERING>]"
" [--gamma-rgb (RGB colorspace, else HSV]"
" [--output <folder path> (to save results)]"
<< std::endl;
return EXIT_SUCCESS;
}
}
std::cout << "Input: " << input << std::endl;
std::cout << "Process on half image resolution? " << half << std::endl;
std::cout << "Gaussian kernel size: " << gaussianKernelSize << std::endl;
std::cout << "Gaussian standard deviation: " << gaussianStdev << std::endl;
std::cout << "Aperture size: " << apertureSize << std::endl;
std::cout << "Canny filtering type: " << filteringType << std::endl;
std::cout << "Output result folder: " << output << std::endl;
float lowerThresh = -1.;
float upperThresh = -1.;
float lowerThreshRatio = 0.6f;
float upperThreshRatio = 0.8f;
lowerThresh, upperThresh, lowerThreshRatio, upperThreshRatio,
filteringType);
if (single_image) {
}
else {
reader.
open(I_color_ori);
}
if (half) {
}
else {
I_color = I_color_ori;
}
std::vector<std::vector<double>> computation_times(nb_methods);
int nb_images = 0;
bool read_single_image = false;
while (!read_single_image && (single_image || !reader.
end())) {
if (!single_image) {
}
if (half) {
}
else {
I_color = I_color_ori;
}
nb_images++;
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];
const double img_ori_entropy = computeImageEntropy(I_gray);
continue;
}
const double gamma = -1;
gamma_colorspace, gamma_method);
<< "): " << (end_time-start_time) << " ms" << std::endl;
computation_times[offset_idx].push_back(end_time-start_time);
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);
snprintf(buffer, FILENAME_MAX, "Entropy: %.4f", img_ori_entropy);
std::ostringstream oss;
snprintf(buffer, FILENAME_MAX, oss.str().c_str(), (end_time-start_time));
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,
snprintf(buffer, FILENAME_MAX, "Entropy: %.4f", img_corrected_entropy);
}
if (!output.empty()) {
std::stringstream output_filename;
const std::string extension = ".jpeg";
if (single_image) {
}
else {
}
std::cout << "Write result to: " << output_filename.str() << std::endl;
}
if (single_image) {
read_single_image = true;
}
}
std::cout << "\nStats:" << std::endl;
std::cout << "Nb images: " << nb_images << std::endl;
continue;
}
}
return EXIT_SUCCESS;
}
#else
int main()
{
std::cerr << "C++11 is required." << std::endl;
return EXIT_SUCCESS;
}
#endif
Class that implements the Canny's edge detector. It is possible to use a boolean mask to ignore some ...
vpImage< unsigned char > detect(const vpImage< vpRGBa > &I_color)
Detect the edges in an image. Convert the color image into a gray-scale image.
void setGradients(const vpImage< float > &dIx, const vpImage< float > &dIy)
Set the Gradients of the image that will be processed.
Font drawing functions for image.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
vpCannyFilteringAndGradientType
Canny filter and gradient operators to apply on the image before the edge detection stage.
@ CANNY_GBLUR_SOBEL_FILTERING
Apply Gaussian blur + Sobel operator on the input image.
@ CANNY_GBLUR_SCHARR_FILTERING
Apply Gaussian blur + Scharr operator on the input image.
static void computePartialDerivatives(const cv::Mat &cv_I, cv::Mat &cv_dIx, cv::Mat &cv_dIy, const bool &computeDx=true, const bool &computeDy=true, const bool &normalize=true, const unsigned int &gaussianKernelSize=5, const float &gaussianStdev=2.f, const unsigned int &apertureGradient=3, const vpCannyFilteringAndGradientType &filteringType=CANNY_GBLUR_SOBEL_FILTERING)
Compute the partial derivatives (i.e. horizontal and vertical gradients) of the input image.
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
void init(unsigned int height, unsigned int width)
Set the size of the image.
unsigned int getWidth() const
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
unsigned int getSize() const
unsigned int getCols() const
unsigned int getHeight() const
unsigned int getRows() const
double getMeanValue(const vpImage< bool > *p_mask=nullptr, unsigned int *nbValidPoints=nullptr) const
Return the mean value of the bitmap.
static double getMedian(const std::vector< double > &v)
static double getMean(const std::vector< double > &v)
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
std::string getFrameName() const
VISP_EXPORT void gammaCorrection(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, const float &gamma, const vpGammaMethod &method=GAMMA_MANUAL, const VISP_NAMESPACE_ADDRESSING vpImage< bool > *p_mask=nullptr)
vpGammaColorHandling
How to handle color images when applying Gamma Correction.
vpGammaMethod
Gamma Correction automatic methods.
VISP_EXPORT std::string vpGammaMethodToString(const vpGammaMethod &type)
Cast a vpGammaMethod into a string, to know its name.
VISP_EXPORT double measureTimeMs()