42 #include <visp3/core/vpImageConvert.h>
43 #include <visp3/core/vpImageFilter.h>
44 #include <visp3/core/vpIoTools.h>
45 #include <visp3/core/vpRGBa.h>
46 #include <visp3/io/vpImageIo.h>
47 #include <visp3/io/vpParseArgv.h>
50 #define GETOPTARGS "cdi:p:h"
61 void usage(
const char *name,
const char *badparam, std::string ipath)
64 Test vpImageFilter class.\n\
67 %s [-i <input image path>] [-p <personal image path>]\n\
74 -i <input image path> %s\n\
75 Set image input path.\n\
76 From this path read \"Klimt/Klimt.pgm,\n\
77 .ppm, .jpeg and .png images.\n\
78 Setting the VISP_INPUT_IMAGE_PATH environment\n\
79 variable produces the same behaviour than using\n\
82 -p <personal image path> \n\
83 Path to an image used to test image reading function.\n\
84 Example: -p /my_path_to/image.png\n\
91 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
103 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath)
117 usage(argv[0],
nullptr, ipath);
126 usage(argv[0], optarg_, ipath);
132 if ((c == 1) || (c == -1)) {
134 usage(argv[0],
nullptr, ipath);
135 std::cerr <<
"ERROR: " << std::endl;
136 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
143 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
144 bool check_results(
const cv::Mat &mat,
const vpImage<double> &I,
unsigned int half_size_y,
unsigned int half_size_x)
146 for (
unsigned int i = half_size_y; i < I.
getHeight() - half_size_y; i++) {
147 for (
unsigned int j = half_size_x; j < I.
getWidth() - half_size_x; j++) {
148 if (!
vpMath::equal(mat.at<
double>(
static_cast<int>(i),
static_cast<int>(j)), I[i][j],
149 std::numeric_limits<double>::epsilon())) {
158 bool check_results(
const cv::Mat &mat,
const vpImage<double> &I,
unsigned int margin,
double threshold)
160 for (
unsigned int i = margin; i < I.
getHeight() - margin; i++) {
161 for (
unsigned int j = margin; j < I.
getWidth() - margin; j++) {
162 if (!
vpMath::equal(mat.at<
unsigned char>(
static_cast<int>(i),
static_cast<int>(j)), I[i][j], threshold)) {
171 bool check_results(
const cv::Mat &mat,
const vpImage<vpRGBa> &I,
unsigned int margin,
double threshold)
173 for (
unsigned int i = margin; i < I.
getHeight() - margin; i++) {
174 for (
unsigned int j = margin; j < I.
getWidth() - margin; j++) {
175 if (!
vpMath::equal(
static_cast<double>(mat.at<cv::Vec3b>(
static_cast<int>(i),
static_cast<int>(j))[2]), I[i][j].R,
179 if (!
vpMath::equal(
static_cast<double>(mat.at<cv::Vec3b>(
static_cast<int>(i),
static_cast<int>(j))[1]), I[i][j].G,
183 if (!
vpMath::equal(
static_cast<double>(mat.at<cv::Vec3b>(
static_cast<int>(i),
static_cast<int>(j))[0]), I[i][j].B,
195 int main(
int argc,
const char *argv[])
198 std::string env_ipath;
199 std::string opt_ipath;
200 std::string opt_ppath;
202 std::string filename;
209 if (!env_ipath.empty())
213 if (getOptions(argc, argv, opt_ipath, opt_ppath) ==
false) {
218 if (!opt_ipath.empty())
223 if (!opt_ipath.empty() && !env_ipath.empty()) {
224 if (ipath != env_ipath) {
225 std::cout << std::endl <<
"WARNING: " << std::endl;
226 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
227 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
228 <<
" we skip the environment variable." << std::endl;
236 for (
unsigned int i = 0, cpt = 1; i < kernel_1.getRows(); i++) {
237 for (
unsigned int j = 0; j < kernel_1.getCols(); j++, cpt++) {
238 kernel_1[i][j] = cpt;
241 std::cout <<
"kernel_1:\n" << kernel_1 << std::endl;
244 for (
unsigned int i = 0, cpt = 1; i < kernel_2.getRows(); i++) {
245 for (
unsigned int j = 0; j < kernel_2.getCols(); j++, cpt++) {
246 kernel_2[i][j] = cpt;
249 std::cout <<
"kernel_2:\n" << kernel_2 << std::endl;
252 for (
unsigned int i = 0, cpt = 1; i < kernel_3.getRows(); i++) {
253 for (
unsigned int j = 0; j < kernel_3.getCols(); j++, cpt++) {
254 kernel_3[i][j] = cpt;
257 std::cout <<
"kernel_3:\n" << kernel_3 << std::endl;
262 for (
unsigned int i = 0; i < I.
getSize(); i++) {
263 I.
bitmap[i] = (
unsigned char)i;
265 std::cout <<
"I:\n" << I << std::endl;
273 std::cout <<
"\nI_correlation_1:\n" << I_correlation_1 << std::endl;
274 std::cout <<
"I_correlation_2:\n" << I_correlation_2 << std::endl;
275 std::cout <<
"I_correlation_3:\n" << I_correlation_3 << std::endl;
277 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
281 cv::Mat mat_kernel_1(2, 2, CV_64F);
282 for (
int i = 0, cpt = 1; i < mat_kernel_1.rows; i++) {
283 for (
int j = 0; j < mat_kernel_1.cols; j++, cpt++) {
284 mat_kernel_1.at<
double>(i, j) = cpt;
288 cv::Mat mat_kernel_2(3, 3, CV_64F);
289 for (
int i = 0, cpt = 1; i < mat_kernel_2.rows; i++) {
290 for (
int j = 0; j < mat_kernel_2.cols; j++, cpt++) {
291 mat_kernel_2.at<
double>(i, j) = cpt;
295 cv::Mat mat_kernel_3(2, 3, CV_64F);
296 for (
int i = 0, cpt = 1; i < mat_kernel_3.rows; i++) {
297 for (
int j = 0; j < mat_kernel_3.cols; j++, cpt++) {
298 mat_kernel_3.at<
double>(i, j) = cpt;
302 cv::Mat matImg_correlation_1, matImg_correlation_2, matImg_correlation_3;
303 cv::filter2D(matImg, matImg_correlation_1, CV_64F, mat_kernel_1);
304 cv::filter2D(matImg, matImg_correlation_2, CV_64F, mat_kernel_2);
305 cv::filter2D(matImg, matImg_correlation_3, CV_64F, mat_kernel_3);
307 std::cout <<
"\nTest correlation on small image:" << std::endl;
308 std::cout <<
"(I_correlation_1 == matImg_correlation_1)? "
309 << check_results(matImg_correlation_1, I_correlation_1, kernel_1.getRows() / 2, kernel_1.getCols() / 2)
311 std::cout <<
"(I_correlation_2 == matImg_correlation_2)? "
312 << check_results(matImg_correlation_2, I_correlation_2, kernel_2.getRows() / 2, kernel_2.getCols() / 2)
314 std::cout <<
"(I_correlation_3 == matImg_correlation_3)? "
315 << check_results(matImg_correlation_3, I_correlation_3, kernel_3.getRows() / 2, kernel_3.getCols() / 2)
325 std::cout <<
"\nI_convolution_1:\n" << I_convolution_1 << std::endl;
326 std::cout <<
"I_convolution_2:\n" << I_convolution_2 << std::endl;
327 std::cout <<
"I_convolution_3:\n" << I_convolution_3 << std::endl;
329 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
330 cv::Mat mat_kernel_1_flip, mat_kernel_2_flip, mat_kernel_3_flip;
331 cv::flip(mat_kernel_1, mat_kernel_1_flip, -1);
332 cv::flip(mat_kernel_2, mat_kernel_2_flip, -1);
333 cv::flip(mat_kernel_3, mat_kernel_3_flip, -1);
335 cv::Mat matImg_convolution_1, matImg_convolution_2, matImg_convolution_3;
337 cv::Point anchor1(mat_kernel_1_flip.cols - mat_kernel_1_flip.cols / 2 - 1,
338 mat_kernel_1_flip.rows - mat_kernel_1_flip.rows / 2 - 1);
339 cv::filter2D(matImg, matImg_convolution_1, CV_64F, mat_kernel_1_flip, anchor1);
341 cv::Point anchor2(mat_kernel_2_flip.cols - mat_kernel_2_flip.cols / 2 - 1,
342 mat_kernel_2_flip.rows - mat_kernel_2_flip.rows / 2 - 1);
343 cv::filter2D(matImg, matImg_convolution_2, CV_64F, mat_kernel_2_flip, anchor2);
345 cv::Point anchor3(mat_kernel_3_flip.cols - mat_kernel_3_flip.cols / 2 - 1,
346 mat_kernel_3_flip.rows - mat_kernel_3_flip.rows / 2 - 1);
347 cv::filter2D(matImg, matImg_convolution_3, CV_64F, mat_kernel_3_flip, anchor3);
349 std::cout <<
"\nTest convolution on small image:" << std::endl;
350 std::cout <<
"(I_convolution_1 == matImg_convolution_1)? "
351 << check_results(matImg_convolution_1, I_convolution_1, kernel_1.getRows() / 2, kernel_1.getCols() / 2)
353 std::cout <<
"(I_convolution_2 == matImg_convolution_2)? "
354 << check_results(matImg_convolution_2, I_convolution_2, kernel_2.getRows() / 2, kernel_2.getCols() / 2)
356 std::cout <<
"(I_convolution_3 == matImg_convolution_3)? "
357 << check_results(matImg_convolution_3, I_convolution_3, kernel_3.getRows() / 2, kernel_3.getCols() / 2)
360 if (opt_ppath.empty()) {
365 filename = opt_ppath;
367 printf(
"Image \"%s\" read successfully\n", filename.c_str());
376 std::cout <<
"\nTime to do 3 correlation filtering: " << t <<
" ms ; Mean: " << t / 3.0 <<
" ms" << std::endl;
378 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
382 cv::filter2D(matImg, matImg_correlation_1, CV_64F, mat_kernel_1);
383 cv::filter2D(matImg, matImg_correlation_2, CV_64F, mat_kernel_2);
384 cv::filter2D(matImg, matImg_correlation_3, CV_64F, mat_kernel_3);
386 std::cout <<
"Time to do 3 cv::filter2D: " << t <<
" ms ; Mean: " << t / 3.0 <<
" ms" << std::endl;
388 std::cout <<
"\nTest correlation on Klimt image:" << std::endl;
389 bool test = check_results(matImg_correlation_1, I_correlation_1, kernel_1.getRows() / 2, kernel_1.getCols() / 2);
390 std::cout <<
"(I_correlation_1 == matImg_correlation_1)? " << test << std::endl;
392 std::cerr <<
"Failed test1 correlation with vpImageFilter::filter()!" << std::endl;
396 test = check_results(matImg_correlation_2, I_correlation_2, kernel_2.getRows() / 2, kernel_2.getCols() / 2);
397 std::cout <<
"(I_correlation_2 == matImg_correlation_2)? " << test << std::endl;
399 std::cerr <<
"Failed test2 correlation with vpImageFilter::filter()!" << std::endl;
403 test = check_results(matImg_correlation_3, I_correlation_3, kernel_3.getRows() / 2, kernel_3.getCols() / 2);
404 std::cout <<
"(I_correlation_3 == matImg_correlation_3)? " << test << std::endl;
406 std::cerr <<
"Failed test3 correlation with vpImageFilter::filter()!" << std::endl;
417 std::cout <<
"\nTime to do 3 convolution filtering: " << t <<
" ms ; Mean: " << t / 3.0 <<
" ms" << std::endl;
419 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
422 cv::filter2D(matImg, matImg_convolution_1, CV_64F, mat_kernel_1_flip, anchor1);
423 cv::filter2D(matImg, matImg_convolution_2, CV_64F, mat_kernel_2_flip, anchor2);
424 cv::filter2D(matImg, matImg_convolution_3, CV_64F, mat_kernel_3_flip, anchor3);
426 std::cout <<
"Time to do 3 cv::filter2D: " << t <<
" ms ; Mean: " << t / 3.0 <<
" ms" << std::endl;
428 std::cout <<
"\nTest convolution on Klimt image:" << std::endl;
429 test = check_results(matImg_convolution_1, I_convolution_1, kernel_1.getRows() / 2, kernel_1.getCols() / 2);
430 std::cout <<
"(I_convolution_1 == matImg_convolution_1)? " << test << std::endl;
432 std::cerr <<
"Failed test1 convolution with vpImageFilter::filter()!" << std::endl;
436 test = check_results(matImg_convolution_2, I_convolution_2, kernel_2.getRows() / 2, kernel_2.getCols() / 2);
437 std::cout <<
"(I_convolution_2 == matImg_convolution_2)? " << test << std::endl;
439 std::cerr <<
"Failed test2 convolution with vpImageFilter::filter()!" << std::endl;
443 test = check_results(matImg_convolution_3, I_convolution_3, kernel_3.getRows() / 2, kernel_3.getCols() / 2);
444 std::cout <<
"(I_convolution_3 == matImg_convolution_3)? " << test << std::endl;
446 std::cerr <<
"Failed test3 convolution with vpImageFilter::filter()!" << std::endl;
455 for (
unsigned int i = 0; i < kernel_sobel_x.getRows(); i++) {
456 for (
unsigned int j = 0; j < kernel_sobel_x.getCols(); j++) {
457 kernel_sobel_x[i][j] = kernel_sobel_x_flip[i][kernel_sobel_x.getCols() - 1 - j];
465 std::cout <<
"\nTime to do Sobel: " << t <<
" ms" << std::endl;
467 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
468 cv::Mat matImg_sobel_x;
470 cv::Sobel(matImg, matImg_sobel_x, CV_64F, 1, 0, 5);
472 std::cout <<
"Time to do cv::Sobel: " << t <<
" ms" << std::endl;
474 std::cout <<
"\nTest Sobel on Klimt image:" << std::endl;
475 std::cout <<
"(I_sobel_x == matImg_sobel_x)? "
476 << check_results(matImg_sobel_x, I_sobel_x, kernel_sobel_x.getRows() / 2, kernel_sobel_x.getCols() / 2)
485 std::cout <<
"\nTime to do Sobel Iu and Iv: " << t <<
" ms" << std::endl;
487 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
488 cv::Mat matImg_sobel_y;
489 cv::Sobel(matImg, matImg_sobel_y, CV_64F, 0, 1, 5);
491 std::cout <<
"(Iu == matImg_sobel_x)? "
492 << check_results(matImg_sobel_x, Iu, kernel_sobel_x.getRows() / 2, kernel_sobel_x.getCols() / 2)
494 std::cout <<
"(Iv == matImg_sobel_y)? "
495 << check_results(matImg_sobel_y, Iv, kernel_sobel_x.getRows() / 2, kernel_sobel_x.getCols() / 2)
502 kernel_sep_x[0] = 1.0;
503 kernel_sep_x[1] = 2.0;
504 kernel_sep_x[2] = 0.0;
505 kernel_sep_x[3] = -2.0;
506 kernel_sep_x[4] = -1.0;
508 kernel_sep_y[0] = 1.0;
509 kernel_sep_y[1] = 4.0;
510 kernel_sep_y[2] = 6.0;
511 kernel_sep_y[3] = 4.0;
512 kernel_sep_y[4] = 1.0;
517 std::cout <<
"\nTime to do sepFilter: " << t <<
" ms" << std::endl;
519 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
520 test = check_results(matImg_sobel_x, Iu, I_sep_filtered.
getRows() / 2, kernel_sobel_x.getCols() / 2);
521 std::cout <<
"(I_sep_filtered == matImg_sobel_x)? " << test << std::endl;
524 std::cerr <<
"Failed separable filter!" << std::endl;
529 std::cout <<
"\nTest median on grayscale image:" << std::endl;
531 for (
unsigned int r = 0; r < 3; r++) {
532 for (
unsigned int c = 0; c < 3; c++) {
533 I_median[r][c] = r * 3 + c;
537 double expectedMedian = 4.;
538 test = (median == expectedMedian);
539 std::cout <<
"(median (=" << median <<
") == expectedMedian(" << expectedMedian <<
"))? " << test << std::endl;
542 std::cerr <<
"Failed median filter on gray-scale image!" << std::endl;
546 std::cout <<
"\nTest median on vpRGBa image:" << std::endl;
548 for (
unsigned int r = 0; r < 3; r++) {
549 for (
unsigned int c = 0; c < 3; c++) {
550 I_median_rgba[r][c].R = r * 3 + c;
551 I_median_rgba[r][c].G = 2 * (r * 3 + c);
552 I_median_rgba[r][c].B = 3 * (r * 3 + c);
556 std::vector<float> expected_median_rgba = { 4.f, 8.f, 12.f };
557 for (
unsigned int i = 0; i < 3; i++) {
558 bool test_local = (median_rgba[i] == expected_median_rgba[i]);
560 std::cout <<
"(median_rgba[" << i <<
"] (=" << median_rgba[i] <<
") == expected_median_rgba[" << i <<
"] ( " << expected_median_rgba[i] <<
"))? " << test_local << std::endl;
563 std::cerr <<
"Failed median filter on vpRGBa image!" << std::endl;
571 std::cout <<
"\nTest Gaussian Blur on Klimt grayscale image:" << std::endl;
576 if (opt_ppath.empty()) {
581 filename = opt_ppath;
583 printf(
"Image \"%s\" read successfully\n", filename.c_str());
586 unsigned int gaussian_filter_size = 7;
591 std::cout <<
"Time to do ViSP Gaussian Blur on grayscale images: " << t <<
" ms" << std::endl;
593 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
594 cv::Mat matImg, matImg_blur;
597 cv::GaussianBlur(matImg, matImg_blur, cv::Size(gaussian_filter_size, gaussian_filter_size), sigma, 0);
599 std::cout <<
"Time to do OpenCV Gaussian Blur on grayscale images: " << t <<
" ms" << std::endl;
601 double threshold = 3.;
602 unsigned int margin = 3;
603 bool test = check_results(matImg_blur, I_blur, margin, threshold);
604 std::cout <<
"(I_blur == matImg_blur)? " << test << std::endl;
607 std::cerr <<
"Failed Gaussian blur filter on grayscale image!" << std::endl;
615 std::cout <<
"\nTest Gaussian Blur on Klimt color image:" << std::endl;
620 if (opt_ppath.empty()) {
625 filename = opt_ppath;
627 printf(
"Image \"%s\" read successfully\n", filename.c_str());
630 unsigned int gaussian_filter_size = 7;
635 std::cout <<
"Time to do ViSP Gaussian Blur on color images: " << t <<
" ms" << std::endl;
637 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
638 cv::Mat matImg_rgb, matImg_rgb_blur;
641 cv::GaussianBlur(matImg_rgb, matImg_rgb_blur, cv::Size(gaussian_filter_size, gaussian_filter_size), sigma, 0);
643 std::cout <<
"Time to do OpenCV Gaussian Blur on color images: " << t <<
" ms" << std::endl;
645 double threshold = 3.;
646 unsigned int margin = 3;
647 bool test = check_results(matImg_rgb_blur, I_rgb_blur, margin, threshold);
648 std::cout <<
"(I_rgb_blur == matImg_rgb_blur)? " << test << std::endl;
651 std::cerr <<
"Failed Gaussian blur filter on color image!" << std::endl;
659 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
663 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 sepFilter(const vpImage< unsigned char > &I, vpImage< double > &If, const vpColVector &kernelH, const vpColVector &kernelV)
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 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()