41 #include <visp3/core/vpImageFilter.h>
42 #include <visp3/core/vpImageConvert.h>
43 #include <visp3/core/vpIoTools.h>
44 #include <visp3/io/vpParseArgv.h>
45 #include <visp3/io/vpImageIo.h>
47 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
48 #include <opencv2/imgproc/imgproc.hpp>
52 #define GETOPTARGS "cdi:p:h"
62 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\
73 -i <input image path> %s\n\
74 Set image input path.\n\
75 From this path read \"ViSP-images/Klimt/Klimt.pgm,\n\
76 .ppm, .jpeg and .png images.\n\
77 Setting the VISP_INPUT_IMAGE_PATH environment\n\
78 variable produces the same behaviour than using\n\
81 -p <personal image path> \n\
82 Path to an image used to test image reading function.\n\
83 Example: -p /my_path_to/image.png\n\
90 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
102 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath) {
108 case 'i': ipath = optarg_;
break;
109 case 'p': ppath = optarg_;
break;
110 case 'h': usage(argv[0], NULL, ipath);
return false;
break;
117 usage(argv[0], optarg_, ipath);
return false;
break;
121 if ((c == 1) || (c == -1)) {
123 usage(argv[0], NULL, ipath);
124 std::cerr <<
"ERROR: " << std::endl;
125 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
132 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
133 bool check_results(
const cv::Mat &mat,
const vpImage<double> &I,
const unsigned int half_size_y,
const unsigned int half_size_x) {
134 for (
unsigned int i = half_size_y; i < I.
getHeight()-half_size_y; i++) {
135 for (
unsigned int j = half_size_x; j < I.
getWidth()-half_size_x; j++) {
136 if (!
vpMath::equal(mat.at<
double>((
int) i, (
int)j), I[i][j], std::numeric_limits<double>::epsilon())) {
147 int main(
int argc,
const char *argv[]) {
149 std::string env_ipath;
150 std::string opt_ipath;
151 std::string opt_ppath;
153 std::string filename;
159 if (! env_ipath.empty())
163 if (getOptions(argc, argv, opt_ipath, opt_ppath) ==
false) {
168 if (!opt_ipath.empty())
173 if (!opt_ipath.empty() && !env_ipath.empty()) {
174 if (ipath != env_ipath) {
175 std::cout << std::endl
176 <<
"WARNING: " << std::endl;
177 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
178 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
179 <<
" we skip the environment variable." << std::endl;
190 for (
unsigned int i = 0; i < I.
getSize(); i++) {
191 I.
bitmap[i] = (
unsigned char) i;
193 std::cout <<
"I:\n" << I << std::endl;
196 for (
unsigned int i = 0, cpt = 1; i < kernel_1.getRows(); i++) {
197 for (
unsigned int j = 0; j < kernel_1.getCols(); j++, cpt++) {
198 kernel_1[i][j] = cpt;
201 std::cout <<
"kernel_1:\n" << kernel_1 << std::endl;
204 for (
unsigned int i = 0, cpt = 1; i < kernel_2.getRows(); i++) {
205 for (
unsigned int j = 0; j < kernel_2.getCols(); j++, cpt++) {
206 kernel_2[i][j] = cpt;
209 std::cout <<
"kernel_2:\n" << kernel_2 << std::endl;
212 for (
unsigned int i = 0, cpt = 1; i < kernel_3.getRows(); i++) {
213 for (
unsigned int j = 0; j < kernel_3.getCols(); j++, cpt++) {
214 kernel_3[i][j] = cpt;
217 std::cout <<
"kernel_3:\n" << kernel_3 << std::endl;
226 std::cout <<
"\nI_correlation_1:\n" << I_correlation_1 << std::endl;
227 std::cout <<
"I_correlation_2:\n" << I_correlation_2 << std::endl;
228 std::cout <<
"I_correlation_3:\n" << I_correlation_3 << std::endl;
230 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
234 cv::Mat mat_kernel_1(2, 2, CV_64F);
235 for (
int i = 0, cpt = 1; i < mat_kernel_1.rows; i++) {
236 for (
int j = 0; j < mat_kernel_1.cols; j++, cpt++) {
237 mat_kernel_1.at<
double>(i, j) = cpt;
241 cv::Mat mat_kernel_2(3, 3, CV_64F);
242 for (
int i = 0, cpt = 1; i < mat_kernel_2.rows; i++) {
243 for (
int j = 0; j < mat_kernel_2.cols; j++, cpt++) {
244 mat_kernel_2.at<
double>(i, j) = cpt;
248 cv::Mat mat_kernel_3(2, 3, CV_64F);
249 for (
int i = 0, cpt = 1; i < mat_kernel_3.rows; i++) {
250 for (
int j = 0; j < mat_kernel_3.cols; j++, cpt++) {
251 mat_kernel_3.at<
double>(i, j) = cpt;
255 cv::Mat matImg_correlation_1, matImg_correlation_2, matImg_correlation_3;
256 cv::filter2D(matImg, matImg_correlation_1, CV_64F, mat_kernel_1);
257 cv::filter2D(matImg, matImg_correlation_2, CV_64F, mat_kernel_2);
258 cv::filter2D(matImg, matImg_correlation_3, CV_64F, mat_kernel_3);
260 std::cout <<
"\nTest correlation on small image:" << std::endl;
261 std::cout <<
"(I_correlation_1 == matImg_correlation_1)? " << check_results(matImg_correlation_1, I_correlation_1, kernel_1.
getRows()/2, kernel_1.getCols()/2) << std::endl;
262 std::cout <<
"(I_correlation_2 == matImg_correlation_2)? " << check_results(matImg_correlation_2, I_correlation_2, kernel_2.
getRows()/2, kernel_2.getCols()/2) << std::endl;
263 std::cout <<
"(I_correlation_3 == matImg_correlation_3)? " << check_results(matImg_correlation_3, I_correlation_3, kernel_3.
getRows()/2, kernel_3.getCols()/2) << std::endl;
273 std::cout <<
"\nI_convolution_1:\n" << I_convolution_1 << std::endl;
274 std::cout <<
"I_convolution_2:\n" << I_convolution_2 << std::endl;
275 std::cout <<
"I_convolution_3:\n" << I_convolution_3 << std::endl;
277 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
278 cv::Mat mat_kernel_1_flip, mat_kernel_2_flip, mat_kernel_3_flip;
279 cv::flip(mat_kernel_1, mat_kernel_1_flip, -1);
280 cv::flip(mat_kernel_2, mat_kernel_2_flip, -1);
281 cv::flip(mat_kernel_3, mat_kernel_3_flip, -1);
283 cv::Mat matImg_convolution_1, matImg_convolution_2, matImg_convolution_3;
285 cv::Point anchor1(mat_kernel_1_flip.cols - mat_kernel_1_flip.cols/2 - 1, mat_kernel_1_flip.rows - mat_kernel_1_flip.rows/2 - 1);
286 cv::filter2D(matImg, matImg_convolution_1, CV_64F, mat_kernel_1_flip, anchor1);
288 cv::Point anchor2(mat_kernel_2_flip.cols - mat_kernel_2_flip.cols/2 - 1, mat_kernel_2_flip.rows - mat_kernel_2_flip.rows/2 - 1);
289 cv::filter2D(matImg, matImg_convolution_2, CV_64F, mat_kernel_2_flip, anchor2);
291 cv::Point anchor3(mat_kernel_3_flip.cols - mat_kernel_3_flip.cols/2 - 1, mat_kernel_3_flip.rows - mat_kernel_3_flip.rows/2 - 1);
292 cv::filter2D(matImg, matImg_convolution_3, CV_64F, mat_kernel_3_flip, anchor3);
294 std::cout <<
"\nTest convolution on small image:" << std::endl;
295 std::cout <<
"(I_convolution_1 == matImg_convolution_1)? " << check_results(matImg_convolution_1, I_convolution_1, kernel_1.getRows()/2, kernel_1.getCols()/2) << std::endl;
296 std::cout <<
"(I_convolution_2 == matImg_convolution_2)? " << check_results(matImg_convolution_2, I_convolution_2, kernel_2.getRows()/2, kernel_2.getCols()/2) << std::endl;
297 std::cout <<
"(I_convolution_3 == matImg_convolution_3)? " << check_results(matImg_convolution_3, I_convolution_3, kernel_3.getRows()/2, kernel_3.getCols()/2) << std::endl;
302 if (opt_ppath.empty()) {
306 filename = opt_ppath;
308 printf(
"Image \"%s\" read successfully\n", filename.c_str());
318 std::cout <<
"\nTime to do 3 correlation filtering: " << t <<
" ms ; Mean: " << t/3.0 <<
" ms" << std::endl;
320 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
324 cv::filter2D(matImg, matImg_correlation_1, CV_64F, mat_kernel_1);
325 cv::filter2D(matImg, matImg_correlation_2, CV_64F, mat_kernel_2);
326 cv::filter2D(matImg, matImg_correlation_3, CV_64F, mat_kernel_3);
328 std::cout <<
"Time to do 3 cv::filter2D: " << t <<
" ms ; Mean: " << t/3.0 <<
" ms" << std::endl;
330 std::cout <<
"\nTest correlation on Klimt image:" << std::endl;
331 bool test = check_results(matImg_correlation_1, I_correlation_1, kernel_1.
getRows()/2, kernel_1.getCols()/2);
332 std::cout <<
"(I_correlation_1 == matImg_correlation_1)? " << test << std::endl;
334 std::cerr <<
"Failed test1 correlation with vpImageFilter::filter()!" << std::endl;
338 test = check_results(matImg_correlation_2, I_correlation_2, kernel_2.
getRows()/2, kernel_2.getCols()/2);
339 std::cout <<
"(I_correlation_2 == matImg_correlation_2)? " << test << std::endl;
341 std::cerr <<
"Failed test2 correlation with vpImageFilter::filter()!" << std::endl;
345 test = check_results(matImg_correlation_3, I_correlation_3, kernel_3.
getRows()/2, kernel_3.getCols()/2);
346 std::cout <<
"(I_correlation_3 == matImg_correlation_3)? " << test << std::endl;
348 std::cerr <<
"Failed test3 correlation with vpImageFilter::filter()!" << std::endl;
360 std::cout <<
"\nTime to do 3 convolution filtering: " << t <<
" ms ; Mean: " << t/3.0 <<
" ms" << std::endl;
362 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
365 cv::filter2D(matImg, matImg_convolution_1, CV_64F, mat_kernel_1_flip, anchor1);
366 cv::filter2D(matImg, matImg_convolution_2, CV_64F, mat_kernel_2_flip, anchor2);
367 cv::filter2D(matImg, matImg_convolution_3, CV_64F, mat_kernel_3_flip, anchor3);
369 std::cout <<
"Time to do 3 cv::filter2D: " << t <<
" ms ; Mean: " << t/3.0 <<
" ms" << std::endl;
371 std::cout <<
"\nTest convolution on Klimt image:" << std::endl;
372 test = check_results(matImg_convolution_1, I_convolution_1, kernel_1.getRows()/2, kernel_1.getCols()/2);
373 std::cout <<
"(I_convolution_1 == matImg_convolution_1)? " << test << std::endl;
375 std::cerr <<
"Failed test1 convolution with vpImageFilter::filter()!" << std::endl;
379 test = check_results(matImg_convolution_2, I_convolution_2, kernel_2.getRows()/2, kernel_2.getCols()/2);
380 std::cout <<
"(I_convolution_2 == matImg_convolution_2)? " << test << std::endl;
382 std::cerr <<
"Failed test2 convolution with vpImageFilter::filter()!" << std::endl;
386 test = check_results(matImg_convolution_3, I_convolution_3, kernel_3.getRows()/2, kernel_3.getCols()/2);
387 std::cout <<
"(I_convolution_3 == matImg_convolution_3)? " << test << std::endl;
389 std::cerr <<
"Failed test3 convolution with vpImageFilter::filter()!" << std::endl;
397 kernel_sobel_x[0][0] = 1.0; kernel_sobel_x[0][1] = 2.0; kernel_sobel_x[0][2] = 0.0; kernel_sobel_x[0][3] = -2.0; kernel_sobel_x[0][4] = -1.0;
398 kernel_sobel_x[1][0] = 4.0; kernel_sobel_x[1][1] = 8.0; kernel_sobel_x[1][2] = 0.0; kernel_sobel_x[1][3] = -8.0; kernel_sobel_x[1][4] = -4.0;
399 kernel_sobel_x[2][0] = 6.0; kernel_sobel_x[2][1] = 12.0; kernel_sobel_x[2][2] = 0.0; kernel_sobel_x[2][3] = -12.0; kernel_sobel_x[2][4] = -6.0;
400 kernel_sobel_x[3][0] = 4.0; kernel_sobel_x[3][1] = 8.0; kernel_sobel_x[3][2] = 0.0; kernel_sobel_x[3][3] = -8.0; kernel_sobel_x[3][4] = -4.0;
401 kernel_sobel_x[4][0] = 1.0; kernel_sobel_x[4][1] = 2.0; kernel_sobel_x[4][2] = 0.0; kernel_sobel_x[4][3] = -2.0; kernel_sobel_x[4][4] = -1.0;
407 std::cout <<
"\nTime to do Sobel: " << t <<
" ms" << std::endl;
409 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
410 cv::Mat matImg_sobel_x;
412 cv::Sobel(matImg, matImg_sobel_x, CV_64F, 1, 0, 5);
414 std::cout <<
"Time to do cv::Sobel: " << t <<
" ms" << std::endl;
416 std::cout <<
"\nTest Sobel on Klimt image:" << std::endl;
417 std::cout <<
"(I_sobel_x == matImg_sobel_x)? " << check_results(matImg_sobel_x, I_sobel_x, kernel_sobel_x.
getRows()/2, kernel_sobel_x.getCols()/2) << std::endl;
425 std::cout <<
"\nTime to do Sobel Iu and Iv: " << t <<
" ms" << std::endl;
427 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
428 cv::Mat matImg_sobel_y;
429 cv::Sobel(matImg, matImg_sobel_y, CV_64F, 0, 1, 5);
431 std::cout <<
"(Iu == matImg_sobel_x)? " << check_results(matImg_sobel_x, Iu, kernel_sobel_x.getRows()/2, kernel_sobel_x.getCols()/2) << std::endl;
432 std::cout <<
"(Iv == matImg_sobel_y)? " << check_results(matImg_sobel_y, Iv, kernel_sobel_x.getRows()/2, kernel_sobel_x.getCols()/2) << std::endl;
439 kernel_sep_x[0] = 1.0; kernel_sep_x[1] = 2.0; kernel_sep_x[2] = 0.0; kernel_sep_x[3] = -2.0; kernel_sep_x[4] = -1.0;
441 kernel_sep_y[0] = 1.0;
442 kernel_sep_y[1] = 4.0;
443 kernel_sep_y[2] = 6.0;
444 kernel_sep_y[3] = 4.0;
445 kernel_sep_y[4] = 1.0;
450 std::cout <<
"\nTime to do sepFilter: " << t <<
" ms" << std::endl;
452 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
453 test = check_results(matImg_sobel_x, Iu, I_sep_filtered.getRows()/2, kernel_sobel_x.getCols()/2);
454 std::cout <<
"(I_sep_filtered == matImg_sobel_x)? " << test << std::endl;
457 std::cerr <<
"Failed separable filter!" << std::endl;
463 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
467 std::cout <<
"\ntestImageFilter is ok." << std::endl;
Implementation of a matrix and operations on matrices.
unsigned int getWidth() const
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void sepFilter(const vpImage< unsigned char > &I, vpImage< double > &If, const vpColVector &kernelH, const vpColVector &kernelV)
Type * bitmap
points toward the bitmap
static bool equal(double x, double y, double s=0.001)
error that can be emited by ViSP classes.
VISP_EXPORT double measureTimeMs()
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
const char * what() const
unsigned int getRows() const
unsigned int getSize() const
static void read(vpImage< unsigned char > &I, const std::string &filename)
Implementation of column vector and the associated operations.
unsigned int getHeight() const
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M, const bool convolve=false)