41 #include <visp3/core/vpImageConvert.h>
42 #include <visp3/core/vpImageFilter.h>
43 #include <visp3/core/vpIoTools.h>
44 #include <visp3/core/vpRGBa.h>
45 #include <visp3/io/vpImageIo.h>
46 #include <visp3/io/vpParseArgv.h>
49 #define GETOPTARGS "cdi:p:h"
51 #ifdef ENABLE_VISP_NAMESPACE
64 void usage(
const char *name,
const char *badparam, std::string ipath)
67 Test vpImageFilter class.\n\
70 %s [-i <input image path>] [-p <personal image path>]\n\
77 -i <input image path> %s\n\
78 Set image input path.\n\
79 From this path read \"Klimt/Klimt.pgm,\n\
80 .ppm, .jpeg and .png images.\n\
81 Setting the VISP_INPUT_IMAGE_PATH environment\n\
82 variable produces the same behaviour than using\n\
85 -p <personal image path> \n\
86 Path to an image used to test image reading function.\n\
87 Example: -p /my_path_to/image.png\n\
94 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
106 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath)
120 usage(argv[0],
nullptr, ipath);
129 usage(argv[0], optarg_, ipath);
135 if ((c == 1) || (c == -1)) {
137 usage(argv[0],
nullptr, ipath);
138 std::cerr <<
"ERROR: " << std::endl;
139 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
146 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
147 bool check_results(
const cv::Mat &mat,
const vpImage<double> &I,
unsigned int half_size_y,
unsigned int half_size_x)
149 for (
unsigned int i = half_size_y; i < I.
getHeight() - half_size_y; i++) {
150 for (
unsigned int j = half_size_x; j < I.
getWidth() - half_size_x; j++) {
151 if (!
vpMath::equal(mat.at<
double>(
static_cast<int>(i),
static_cast<int>(j)), I[i][j],
152 std::numeric_limits<double>::epsilon())) {
161 bool check_results(
const cv::Mat &mat,
const vpImage<double> &I,
unsigned int margin,
double threshold)
163 for (
unsigned int i = margin; i < I.
getHeight() - margin; i++) {
164 for (
unsigned int j = margin; j < I.
getWidth() - margin; j++) {
165 if (!
vpMath::equal(mat.at<
unsigned char>(
static_cast<int>(i),
static_cast<int>(j)), I[i][j], threshold)) {
174 bool check_results(
const cv::Mat &mat,
const vpImage<vpRGBa> &I,
unsigned int margin,
double threshold)
176 for (
unsigned int i = margin; i < I.
getHeight() - margin; i++) {
177 for (
unsigned int j = margin; j < I.
getWidth() - margin; j++) {
178 if (!
vpMath::equal(
static_cast<double>(mat.at<cv::Vec3b>(
static_cast<int>(i),
static_cast<int>(j))[2]), I[i][j].R,
182 if (!
vpMath::equal(
static_cast<double>(mat.at<cv::Vec3b>(
static_cast<int>(i),
static_cast<int>(j))[1]), I[i][j].G,
186 if (!
vpMath::equal(
static_cast<double>(mat.at<cv::Vec3b>(
static_cast<int>(i),
static_cast<int>(j))[0]), I[i][j].B,
198 int main(
int argc,
const char *argv[])
201 std::string env_ipath;
202 std::string opt_ipath;
203 std::string opt_ppath;
205 std::string filename;
212 if (!env_ipath.empty())
216 if (getOptions(argc, argv, opt_ipath, opt_ppath) ==
false) {
221 if (!opt_ipath.empty())
226 if (!opt_ipath.empty() && !env_ipath.empty()) {
227 if (ipath != env_ipath) {
228 std::cout << std::endl <<
"WARNING: " << std::endl;
229 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
230 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
231 <<
" we skip the environment variable." << std::endl;
239 for (
unsigned int i = 0, cpt = 1; i < kernel_1.getRows(); i++) {
240 for (
unsigned int j = 0; j < kernel_1.getCols(); j++, cpt++) {
241 kernel_1[i][j] = cpt;
244 std::cout <<
"kernel_1:\n" << kernel_1 << std::endl;
247 for (
unsigned int i = 0, cpt = 1; i < kernel_2.getRows(); i++) {
248 for (
unsigned int j = 0; j < kernel_2.getCols(); j++, cpt++) {
249 kernel_2[i][j] = cpt;
252 std::cout <<
"kernel_2:\n" << kernel_2 << std::endl;
255 for (
unsigned int i = 0, cpt = 1; i < kernel_3.getRows(); i++) {
256 for (
unsigned int j = 0; j < kernel_3.getCols(); j++, cpt++) {
257 kernel_3[i][j] = cpt;
260 std::cout <<
"kernel_3:\n" << kernel_3 << std::endl;
265 for (
unsigned int i = 0; i < I.
getSize(); i++) {
266 I.
bitmap[i] = (
unsigned char)i;
268 std::cout <<
"I:\n" << I << std::endl;
276 std::cout <<
"\nI_correlation_1:\n" << I_correlation_1 << std::endl;
277 std::cout <<
"I_correlation_2:\n" << I_correlation_2 << std::endl;
278 std::cout <<
"I_correlation_3:\n" << I_correlation_3 << std::endl;
280 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
284 cv::Mat mat_kernel_1(2, 2, CV_64F);
285 for (
int i = 0, cpt = 1; i < mat_kernel_1.rows; i++) {
286 for (
int j = 0; j < mat_kernel_1.cols; j++, cpt++) {
287 mat_kernel_1.at<
double>(i, j) = cpt;
291 cv::Mat mat_kernel_2(3, 3, CV_64F);
292 for (
int i = 0, cpt = 1; i < mat_kernel_2.rows; i++) {
293 for (
int j = 0; j < mat_kernel_2.cols; j++, cpt++) {
294 mat_kernel_2.at<
double>(i, j) = cpt;
298 cv::Mat mat_kernel_3(2, 3, CV_64F);
299 for (
int i = 0, cpt = 1; i < mat_kernel_3.rows; i++) {
300 for (
int j = 0; j < mat_kernel_3.cols; j++, cpt++) {
301 mat_kernel_3.at<
double>(i, j) = cpt;
305 cv::Mat matImg_correlation_1, matImg_correlation_2, matImg_correlation_3;
306 cv::filter2D(matImg, matImg_correlation_1, CV_64F, mat_kernel_1);
307 cv::filter2D(matImg, matImg_correlation_2, CV_64F, mat_kernel_2);
308 cv::filter2D(matImg, matImg_correlation_3, CV_64F, mat_kernel_3);
310 std::cout <<
"\nTest correlation on small image:" << std::endl;
311 std::cout <<
"(I_correlation_1 == matImg_correlation_1)? "
312 << check_results(matImg_correlation_1, I_correlation_1, kernel_1.getRows() / 2, kernel_1.getCols() / 2)
314 std::cout <<
"(I_correlation_2 == matImg_correlation_2)? "
315 << check_results(matImg_correlation_2, I_correlation_2, kernel_2.getRows() / 2, kernel_2.getCols() / 2)
317 std::cout <<
"(I_correlation_3 == matImg_correlation_3)? "
318 << check_results(matImg_correlation_3, I_correlation_3, kernel_3.getRows() / 2, kernel_3.getCols() / 2)
328 std::cout <<
"\nI_convolution_1:\n" << I_convolution_1 << std::endl;
329 std::cout <<
"I_convolution_2:\n" << I_convolution_2 << std::endl;
330 std::cout <<
"I_convolution_3:\n" << I_convolution_3 << std::endl;
332 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
333 cv::Mat mat_kernel_1_flip, mat_kernel_2_flip, mat_kernel_3_flip;
334 cv::flip(mat_kernel_1, mat_kernel_1_flip, -1);
335 cv::flip(mat_kernel_2, mat_kernel_2_flip, -1);
336 cv::flip(mat_kernel_3, mat_kernel_3_flip, -1);
338 cv::Mat matImg_convolution_1, matImg_convolution_2, matImg_convolution_3;
340 cv::Point anchor1(mat_kernel_1_flip.cols - mat_kernel_1_flip.cols / 2 - 1,
341 mat_kernel_1_flip.rows - mat_kernel_1_flip.rows / 2 - 1);
342 cv::filter2D(matImg, matImg_convolution_1, CV_64F, mat_kernel_1_flip, anchor1);
344 cv::Point anchor2(mat_kernel_2_flip.cols - mat_kernel_2_flip.cols / 2 - 1,
345 mat_kernel_2_flip.rows - mat_kernel_2_flip.rows / 2 - 1);
346 cv::filter2D(matImg, matImg_convolution_2, CV_64F, mat_kernel_2_flip, anchor2);
348 cv::Point anchor3(mat_kernel_3_flip.cols - mat_kernel_3_flip.cols / 2 - 1,
349 mat_kernel_3_flip.rows - mat_kernel_3_flip.rows / 2 - 1);
350 cv::filter2D(matImg, matImg_convolution_3, CV_64F, mat_kernel_3_flip, anchor3);
352 std::cout <<
"\nTest convolution on small image:" << std::endl;
353 std::cout <<
"(I_convolution_1 == matImg_convolution_1)? "
354 << check_results(matImg_convolution_1, I_convolution_1, kernel_1.getRows() / 2, kernel_1.getCols() / 2)
356 std::cout <<
"(I_convolution_2 == matImg_convolution_2)? "
357 << check_results(matImg_convolution_2, I_convolution_2, kernel_2.getRows() / 2, kernel_2.getCols() / 2)
359 std::cout <<
"(I_convolution_3 == matImg_convolution_3)? "
360 << check_results(matImg_convolution_3, I_convolution_3, kernel_3.getRows() / 2, kernel_3.getCols() / 2)
363 if (opt_ppath.empty()) {
368 filename = opt_ppath;
370 printf(
"Image \"%s\" read successfully\n", filename.c_str());
379 std::cout <<
"\nTime to do 3 correlation filtering: " << t <<
" ms ; Mean: " << t / 3.0 <<
" ms" << std::endl;
381 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
385 cv::filter2D(matImg, matImg_correlation_1, CV_64F, mat_kernel_1);
386 cv::filter2D(matImg, matImg_correlation_2, CV_64F, mat_kernel_2);
387 cv::filter2D(matImg, matImg_correlation_3, CV_64F, mat_kernel_3);
389 std::cout <<
"Time to do 3 cv::filter2D: " << t <<
" ms ; Mean: " << t / 3.0 <<
" ms" << std::endl;
391 std::cout <<
"\nTest correlation on Klimt image:" << std::endl;
392 bool test = check_results(matImg_correlation_1, I_correlation_1, kernel_1.getRows() / 2, kernel_1.getCols() / 2);
393 std::cout <<
"(I_correlation_1 == matImg_correlation_1)? " << test << std::endl;
395 std::cerr <<
"Failed test1 correlation with vpImageFilter::filter()!" << std::endl;
399 test = check_results(matImg_correlation_2, I_correlation_2, kernel_2.getRows() / 2, kernel_2.getCols() / 2);
400 std::cout <<
"(I_correlation_2 == matImg_correlation_2)? " << test << std::endl;
402 std::cerr <<
"Failed test2 correlation with vpImageFilter::filter()!" << std::endl;
406 test = check_results(matImg_correlation_3, I_correlation_3, kernel_3.getRows() / 2, kernel_3.getCols() / 2);
407 std::cout <<
"(I_correlation_3 == matImg_correlation_3)? " << test << std::endl;
409 std::cerr <<
"Failed test3 correlation with vpImageFilter::filter()!" << std::endl;
420 std::cout <<
"\nTime to do 3 convolution filtering: " << t <<
" ms ; Mean: " << t / 3.0 <<
" ms" << std::endl;
422 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
425 cv::filter2D(matImg, matImg_convolution_1, CV_64F, mat_kernel_1_flip, anchor1);
426 cv::filter2D(matImg, matImg_convolution_2, CV_64F, mat_kernel_2_flip, anchor2);
427 cv::filter2D(matImg, matImg_convolution_3, CV_64F, mat_kernel_3_flip, anchor3);
429 std::cout <<
"Time to do 3 cv::filter2D: " << t <<
" ms ; Mean: " << t / 3.0 <<
" ms" << std::endl;
431 std::cout <<
"\nTest convolution on Klimt image:" << std::endl;
432 test = check_results(matImg_convolution_1, I_convolution_1, kernel_1.getRows() / 2, kernel_1.getCols() / 2);
433 std::cout <<
"(I_convolution_1 == matImg_convolution_1)? " << test << std::endl;
435 std::cerr <<
"Failed test1 convolution with vpImageFilter::filter()!" << std::endl;
439 test = check_results(matImg_convolution_2, I_convolution_2, kernel_2.getRows() / 2, kernel_2.getCols() / 2);
440 std::cout <<
"(I_convolution_2 == matImg_convolution_2)? " << test << std::endl;
442 std::cerr <<
"Failed test2 convolution with vpImageFilter::filter()!" << std::endl;
446 test = check_results(matImg_convolution_3, I_convolution_3, kernel_3.getRows() / 2, kernel_3.getCols() / 2);
447 std::cout <<
"(I_convolution_3 == matImg_convolution_3)? " << test << std::endl;
449 std::cerr <<
"Failed test3 convolution with vpImageFilter::filter()!" << std::endl;
458 for (
unsigned int i = 0; i < kernel_sobel_x.getRows(); i++) {
459 for (
unsigned int j = 0; j < kernel_sobel_x.getCols(); j++) {
460 kernel_sobel_x[i][j] = kernel_sobel_x_flip[i][kernel_sobel_x.getCols() - 1 - j];
468 std::cout <<
"\nTime to do Sobel: " << t <<
" ms" << std::endl;
470 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
471 cv::Mat matImg_sobel_x;
473 cv::Sobel(matImg, matImg_sobel_x, CV_64F, 1, 0, 5);
475 std::cout <<
"Time to do cv::Sobel: " << t <<
" ms" << std::endl;
477 std::cout <<
"\nTest Sobel on Klimt image:" << std::endl;
478 std::cout <<
"(I_sobel_x == matImg_sobel_x)? "
479 << check_results(matImg_sobel_x, I_sobel_x, kernel_sobel_x.getRows() / 2, kernel_sobel_x.getCols() / 2)
488 std::cout <<
"\nTime to do Sobel Iu and Iv: " << t <<
" ms" << std::endl;
490 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
491 cv::Mat matImg_sobel_y;
492 cv::Sobel(matImg, matImg_sobel_y, CV_64F, 0, 1, 5);
494 std::cout <<
"(Iu == matImg_sobel_x)? "
495 << check_results(matImg_sobel_x, Iu, kernel_sobel_x.getRows() / 2, kernel_sobel_x.getCols() / 2)
497 std::cout <<
"(Iv == matImg_sobel_y)? "
498 << check_results(matImg_sobel_y, Iv, kernel_sobel_x.getRows() / 2, kernel_sobel_x.getCols() / 2)
505 kernel_sep_x[0] = 1.0;
506 kernel_sep_x[1] = 2.0;
507 kernel_sep_x[2] = 0.0;
508 kernel_sep_x[3] = -2.0;
509 kernel_sep_x[4] = -1.0;
511 kernel_sep_y[0] = 1.0;
512 kernel_sep_y[1] = 4.0;
513 kernel_sep_y[2] = 6.0;
514 kernel_sep_y[3] = 4.0;
515 kernel_sep_y[4] = 1.0;
520 std::cout <<
"\nTime to do sepFilter: " << t <<
" ms" << std::endl;
522 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
523 test = check_results(matImg_sobel_x, Iu, I_sep_filtered.
getRows() / 2, kernel_sobel_x.getCols() / 2);
524 std::cout <<
"(I_sep_filtered == matImg_sobel_x)? " << test << std::endl;
527 std::cerr <<
"Failed separable filter!" << std::endl;
532 std::cout <<
"\nTest median on grayscale image:" << std::endl;
534 for (
unsigned int r = 0; r < 3; r++) {
535 for (
unsigned int c = 0; c < 3; c++) {
536 I_median[r][c] = r * 3 + c;
540 double expectedMedian = 4.;
541 test = (median == expectedMedian);
542 std::cout <<
"(median (=" << median <<
") == expectedMedian(" << expectedMedian <<
"))? " << test << std::endl;
545 std::cerr <<
"Failed median filter on gray-scale image!" << std::endl;
549 std::cout <<
"\nTest median on vpRGBa image:" << std::endl;
551 for (
unsigned int r = 0; r < 3; r++) {
552 for (
unsigned int c = 0; c < 3; c++) {
553 I_median_rgba[r][c].R = r * 3 + c;
554 I_median_rgba[r][c].G = 2 * (r * 3 + c);
555 I_median_rgba[r][c].B = 3 * (r * 3 + c);
559 std::vector<float> expected_median_rgba = { 4.f, 8.f, 12.f };
560 for (
unsigned int i = 0; i < 3; i++) {
561 bool test_local = (median_rgba[i] == expected_median_rgba[i]);
563 std::cout <<
"(median_rgba[" << i <<
"] (=" << median_rgba[i] <<
") == expected_median_rgba[" << i <<
"] ( " << expected_median_rgba[i] <<
"))? " << test_local << std::endl;
566 std::cerr <<
"Failed median filter on vpRGBa image!" << std::endl;
574 std::cout <<
"\nTest Gaussian Blur on Klimt grayscale image:" << std::endl;
579 if (opt_ppath.empty()) {
584 filename = opt_ppath;
586 printf(
"Image \"%s\" read successfully\n", filename.c_str());
589 unsigned int gaussian_filter_size = 7;
594 std::cout <<
"Time to do ViSP Gaussian Blur on grayscale images: " << t <<
" ms" << std::endl;
596 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
597 cv::Mat matImg, matImg_blur;
600 cv::GaussianBlur(matImg, matImg_blur, cv::Size(gaussian_filter_size, gaussian_filter_size), sigma, 0);
602 std::cout <<
"Time to do OpenCV Gaussian Blur on grayscale images: " << t <<
" ms" << std::endl;
604 double threshold = 3.;
605 unsigned int margin = 3;
606 bool test = check_results(matImg_blur, I_blur, margin, threshold);
607 std::cout <<
"(I_blur == matImg_blur)? " << test << std::endl;
610 std::cerr <<
"Failed Gaussian blur filter on grayscale image!" << std::endl;
618 std::cout <<
"\nTest Gaussian Blur on Klimt color image:" << std::endl;
623 if (opt_ppath.empty()) {
628 filename = opt_ppath;
630 printf(
"Image \"%s\" read successfully\n", filename.c_str());
633 unsigned int gaussian_filter_size = 7;
638 std::cout <<
"Time to do ViSP Gaussian Blur on color images: " << t <<
" ms" << std::endl;
640 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
641 cv::Mat matImg_rgb, matImg_rgb_blur;
644 cv::GaussianBlur(matImg_rgb, matImg_rgb_blur, cv::Size(gaussian_filter_size, gaussian_filter_size), sigma, 0);
646 std::cout <<
"Time to do OpenCV Gaussian Blur on color images: " << t <<
" ms" << std::endl;
648 double threshold = 3.;
649 unsigned int margin = 3;
650 bool test = check_results(matImg_rgb_blur, I_rgb_blur, margin, threshold);
651 std::cout <<
"(I_rgb_blur == matImg_rgb_blur)? " << test << std::endl;
654 std::cerr <<
"Failed Gaussian blur filter on color image!" << std::endl;
662 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
666 std::cout <<
"\ntestImageFilter is ok." << std::endl;
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
const char * what() const
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static FilterType getSobelKernelX(FilterType *filter, unsigned int size)
static void gaussianBlur(const vpImage< ImageType > &I, vpImage< FilterType > &GI, unsigned int size=7, FilterType sigma=0., bool normalize=true, const vpImage< bool > *p_mask=nullptr)
static void filter(const vpImage< ImageType > &I, vpImage< FilterType > &If, const vpArray2D< FilterType > &M, bool convolve=false, const vpImage< bool > *p_mask=nullptr)
static void sepFilter(const vpImage< unsigned char > &I, vpImage< double > &If, const vpColVector &kernelH, const vpColVector &kernelV)
static float median(const cv::Mat &cv_I)
Calculates the median value of a single channel. The algorithm is based on based on https://github....
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
unsigned int getWidth() const
unsigned int getSize() const
Type * bitmap
points toward the bitmap
unsigned int getHeight() const
unsigned int getRows() const
static bool equal(double x, double y, double threshold=0.001)
Implementation of a matrix and operations on matrices.
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
VISP_EXPORT double measureTimeMs()