42 #include <visp3/core/vpImageConvert.h> 43 #include <visp3/core/vpImageFilter.h> 44 #include <visp3/core/vpIoTools.h> 45 #include <visp3/io/vpImageIo.h> 46 #include <visp3/io/vpParseArgv.h> 48 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408) 49 #include <opencv2/imgproc/imgproc.hpp> 53 #define GETOPTARGS "cdi:p:h" 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\ 76 -i <input image path> %s\n\ 77 Set image input path.\n\ 78 From this path read \"Klimt/Klimt.pgm,\n\ 79 .ppm, .jpeg and .png images.\n\ 80 Setting the VISP_INPUT_IMAGE_PATH environment\n\ 81 variable produces the same behaviour than using\n\ 84 -p <personal image path> \n\ 85 Path to an image used to test image reading function.\n\ 86 Example: -p /my_path_to/image.png\n\ 89 Print the help.\n\n", ipath.c_str());
92 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
104 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &ppath)
118 usage(argv[0], NULL, ipath);
127 usage(argv[0], optarg_, ipath);
133 if ((c == 1) || (c == -1)) {
135 usage(argv[0], NULL, ipath);
136 std::cerr <<
"ERROR: " << std::endl;
137 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
144 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408) 145 bool check_results(
const cv::Mat &mat,
const vpImage<double> &I,
const unsigned int half_size_y,
146 const unsigned int half_size_x)
148 for (
unsigned int i = half_size_y; i < I.
getHeight() - half_size_y; i++) {
149 for (
unsigned int j = half_size_x; j < I.
getWidth() - half_size_x; j++) {
150 if (!
vpMath::equal(mat.at<
double>((
int)i, (
int)j), I[i][j], std::numeric_limits<double>::epsilon())) {
161 int main(
int argc,
const char *argv[])
164 std::string env_ipath;
165 std::string opt_ipath;
166 std::string opt_ppath;
168 std::string filename;
175 if (!env_ipath.empty())
179 if (getOptions(argc, argv, opt_ipath, opt_ppath) ==
false) {
184 if (!opt_ipath.empty())
189 if (!opt_ipath.empty() && !env_ipath.empty()) {
190 if (ipath != env_ipath) {
191 std::cout << std::endl <<
"WARNING: " << std::endl;
192 std::cout <<
" Since -i <visp image path=" << ipath <<
"> " 193 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
194 <<
" we skip the environment variable." << std::endl;
204 for (
unsigned int i = 0; i < I.
getSize(); i++) {
205 I.
bitmap[i] = (
unsigned char)i;
207 std::cout <<
"I:\n" << I << std::endl;
210 for (
unsigned int i = 0, cpt = 1; i < kernel_1.getRows(); i++) {
211 for (
unsigned int j = 0; j < kernel_1.getCols(); j++, cpt++) {
212 kernel_1[i][j] = cpt;
215 std::cout <<
"kernel_1:\n" << kernel_1 << std::endl;
218 for (
unsigned int i = 0, cpt = 1; i < kernel_2.getRows(); i++) {
219 for (
unsigned int j = 0; j < kernel_2.getCols(); j++, cpt++) {
220 kernel_2[i][j] = cpt;
223 std::cout <<
"kernel_2:\n" << kernel_2 << std::endl;
226 for (
unsigned int i = 0, cpt = 1; i < kernel_3.getRows(); i++) {
227 for (
unsigned int j = 0; j < kernel_3.getCols(); j++, cpt++) {
228 kernel_3[i][j] = cpt;
231 std::cout <<
"kernel_3:\n" << kernel_3 << std::endl;
239 std::cout <<
"\nI_correlation_1:\n" << I_correlation_1 << std::endl;
240 std::cout <<
"I_correlation_2:\n" << I_correlation_2 << std::endl;
241 std::cout <<
"I_correlation_3:\n" << I_correlation_3 << std::endl;
243 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408) 247 cv::Mat mat_kernel_1(2, 2, CV_64F);
248 for (
int i = 0, cpt = 1; i < mat_kernel_1.rows; i++) {
249 for (
int j = 0; j < mat_kernel_1.cols; j++, cpt++) {
250 mat_kernel_1.at<
double>(i, j) = cpt;
254 cv::Mat mat_kernel_2(3, 3, CV_64F);
255 for (
int i = 0, cpt = 1; i < mat_kernel_2.rows; i++) {
256 for (
int j = 0; j < mat_kernel_2.cols; j++, cpt++) {
257 mat_kernel_2.at<
double>(i, j) = cpt;
261 cv::Mat mat_kernel_3(2, 3, CV_64F);
262 for (
int i = 0, cpt = 1; i < mat_kernel_3.rows; i++) {
263 for (
int j = 0; j < mat_kernel_3.cols; j++, cpt++) {
264 mat_kernel_3.at<
double>(i, j) = cpt;
268 cv::Mat matImg_correlation_1, matImg_correlation_2, matImg_correlation_3;
269 cv::filter2D(matImg, matImg_correlation_1, CV_64F, mat_kernel_1);
270 cv::filter2D(matImg, matImg_correlation_2, CV_64F, mat_kernel_2);
271 cv::filter2D(matImg, matImg_correlation_3, CV_64F, mat_kernel_3);
273 std::cout <<
"\nTest correlation on small image:" << std::endl;
274 std::cout <<
"(I_correlation_1 == matImg_correlation_1)? " 275 << check_results(matImg_correlation_1, I_correlation_1, kernel_1.
getRows() / 2, kernel_1.getCols() / 2)
277 std::cout <<
"(I_correlation_2 == matImg_correlation_2)? " 278 << check_results(matImg_correlation_2, I_correlation_2, kernel_2.
getRows() / 2, kernel_2.getCols() / 2)
280 std::cout <<
"(I_correlation_3 == matImg_correlation_3)? " 281 << check_results(matImg_correlation_3, I_correlation_3, kernel_3.
getRows() / 2, kernel_3.getCols() / 2)
291 std::cout <<
"\nI_convolution_1:\n" << I_convolution_1 << std::endl;
292 std::cout <<
"I_convolution_2:\n" << I_convolution_2 << std::endl;
293 std::cout <<
"I_convolution_3:\n" << I_convolution_3 << std::endl;
295 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408) 296 cv::Mat mat_kernel_1_flip, mat_kernel_2_flip, mat_kernel_3_flip;
297 cv::flip(mat_kernel_1, mat_kernel_1_flip, -1);
298 cv::flip(mat_kernel_2, mat_kernel_2_flip, -1);
299 cv::flip(mat_kernel_3, mat_kernel_3_flip, -1);
301 cv::Mat matImg_convolution_1, matImg_convolution_2, matImg_convolution_3;
303 cv::Point anchor1(mat_kernel_1_flip.cols - mat_kernel_1_flip.cols / 2 - 1,
304 mat_kernel_1_flip.rows - mat_kernel_1_flip.rows / 2 - 1);
305 cv::filter2D(matImg, matImg_convolution_1, CV_64F, mat_kernel_1_flip, anchor1);
307 cv::Point anchor2(mat_kernel_2_flip.cols - mat_kernel_2_flip.cols / 2 - 1,
308 mat_kernel_2_flip.rows - mat_kernel_2_flip.rows / 2 - 1);
309 cv::filter2D(matImg, matImg_convolution_2, CV_64F, mat_kernel_2_flip, anchor2);
311 cv::Point anchor3(mat_kernel_3_flip.cols - mat_kernel_3_flip.cols / 2 - 1,
312 mat_kernel_3_flip.rows - mat_kernel_3_flip.rows / 2 - 1);
313 cv::filter2D(matImg, matImg_convolution_3, CV_64F, mat_kernel_3_flip, anchor3);
315 std::cout <<
"\nTest convolution on small image:" << std::endl;
316 std::cout <<
"(I_convolution_1 == matImg_convolution_1)? " 317 << check_results(matImg_convolution_1, I_convolution_1, kernel_1.getRows() / 2, kernel_1.getCols() / 2)
319 std::cout <<
"(I_convolution_2 == matImg_convolution_2)? " 320 << check_results(matImg_convolution_2, I_convolution_2, kernel_2.getRows() / 2, kernel_2.getCols() / 2)
322 std::cout <<
"(I_convolution_3 == matImg_convolution_3)? " 323 << check_results(matImg_convolution_3, I_convolution_3, kernel_3.getRows() / 2, kernel_3.getCols() / 2)
328 if (opt_ppath.empty()) {
332 filename = opt_ppath;
334 printf(
"Image \"%s\" read successfully\n", filename.c_str());
343 std::cout <<
"\nTime to do 3 correlation filtering: " << t <<
" ms ; Mean: " << t / 3.0 <<
" ms" << std::endl;
345 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408) 349 cv::filter2D(matImg, matImg_correlation_1, CV_64F, mat_kernel_1);
350 cv::filter2D(matImg, matImg_correlation_2, CV_64F, mat_kernel_2);
351 cv::filter2D(matImg, matImg_correlation_3, CV_64F, mat_kernel_3);
353 std::cout <<
"Time to do 3 cv::filter2D: " << t <<
" ms ; Mean: " << t / 3.0 <<
" ms" << std::endl;
355 std::cout <<
"\nTest correlation on Klimt image:" << std::endl;
356 bool test = check_results(matImg_correlation_1, I_correlation_1, kernel_1.
getRows() / 2, kernel_1.getCols() / 2);
357 std::cout <<
"(I_correlation_1 == matImg_correlation_1)? " << test << std::endl;
359 std::cerr <<
"Failed test1 correlation with vpImageFilter::filter()!" << std::endl;
363 test = check_results(matImg_correlation_2, I_correlation_2, kernel_2.
getRows() / 2, kernel_2.getCols() / 2);
364 std::cout <<
"(I_correlation_2 == matImg_correlation_2)? " << test << std::endl;
366 std::cerr <<
"Failed test2 correlation with vpImageFilter::filter()!" << std::endl;
370 test = check_results(matImg_correlation_3, I_correlation_3, kernel_3.
getRows() / 2, kernel_3.getCols() / 2);
371 std::cout <<
"(I_correlation_3 == matImg_correlation_3)? " << test << std::endl;
373 std::cerr <<
"Failed test3 correlation with vpImageFilter::filter()!" << std::endl;
384 std::cout <<
"\nTime to do 3 convolution filtering: " << t <<
" ms ; Mean: " << t / 3.0 <<
" ms" << std::endl;
386 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408) 389 cv::filter2D(matImg, matImg_convolution_1, CV_64F, mat_kernel_1_flip, anchor1);
390 cv::filter2D(matImg, matImg_convolution_2, CV_64F, mat_kernel_2_flip, anchor2);
391 cv::filter2D(matImg, matImg_convolution_3, CV_64F, mat_kernel_3_flip, anchor3);
393 std::cout <<
"Time to do 3 cv::filter2D: " << t <<
" ms ; Mean: " << t / 3.0 <<
" ms" << std::endl;
395 std::cout <<
"\nTest convolution on Klimt image:" << std::endl;
396 test = check_results(matImg_convolution_1, I_convolution_1, kernel_1.getRows() / 2, kernel_1.getCols() / 2);
397 std::cout <<
"(I_convolution_1 == matImg_convolution_1)? " << test << std::endl;
399 std::cerr <<
"Failed test1 convolution with vpImageFilter::filter()!" << std::endl;
403 test = check_results(matImg_convolution_2, I_convolution_2, kernel_2.getRows() / 2, kernel_2.getCols() / 2);
404 std::cout <<
"(I_convolution_2 == matImg_convolution_2)? " << test << std::endl;
406 std::cerr <<
"Failed test2 convolution with vpImageFilter::filter()!" << std::endl;
410 test = check_results(matImg_convolution_3, I_convolution_3, kernel_3.getRows() / 2, kernel_3.getCols() / 2);
411 std::cout <<
"(I_convolution_3 == matImg_convolution_3)? " << test << std::endl;
413 std::cerr <<
"Failed test3 convolution with vpImageFilter::filter()!" << std::endl;
422 for (
unsigned int i = 0; i < kernel_sobel_x.getRows(); i++) {
423 for (
unsigned int j = 0; j < kernel_sobel_x.getCols(); j++) {
424 kernel_sobel_x[i][j] = kernel_sobel_x_flip[i][kernel_sobel_x.getCols()-1-j];
432 std::cout <<
"\nTime to do Sobel: " << t <<
" ms" << std::endl;
434 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408) 435 cv::Mat matImg_sobel_x;
437 cv::Sobel(matImg, matImg_sobel_x, CV_64F, 1, 0, 5);
439 std::cout <<
"Time to do cv::Sobel: " << t <<
" ms" << std::endl;
441 std::cout <<
"\nTest Sobel on Klimt image:" << std::endl;
442 std::cout <<
"(I_sobel_x == matImg_sobel_x)? " 443 << check_results(matImg_sobel_x, I_sobel_x, kernel_sobel_x.
getRows() / 2, kernel_sobel_x.getCols() / 2)
452 std::cout <<
"\nTime to do Sobel Iu and Iv: " << t <<
" ms" << std::endl;
454 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408) 455 cv::Mat matImg_sobel_y;
456 cv::Sobel(matImg, matImg_sobel_y, CV_64F, 0, 1, 5);
458 std::cout <<
"(Iu == matImg_sobel_x)? " 459 << check_results(matImg_sobel_x, Iu, kernel_sobel_x.getRows() / 2, kernel_sobel_x.getCols() / 2)
461 std::cout <<
"(Iv == matImg_sobel_y)? " 462 << check_results(matImg_sobel_y, Iv, kernel_sobel_x.getRows() / 2, kernel_sobel_x.getCols() / 2)
469 kernel_sep_x[0] = 1.0;
470 kernel_sep_x[1] = 2.0;
471 kernel_sep_x[2] = 0.0;
472 kernel_sep_x[3] = -2.0;
473 kernel_sep_x[4] = -1.0;
475 kernel_sep_y[0] = 1.0;
476 kernel_sep_y[1] = 4.0;
477 kernel_sep_y[2] = 6.0;
478 kernel_sep_y[3] = 4.0;
479 kernel_sep_y[4] = 1.0;
484 std::cout <<
"\nTime to do sepFilter: " << t <<
" ms" << std::endl;
486 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408) 487 test = check_results(matImg_sobel_x, Iu, I_sep_filtered.getRows() / 2, kernel_sobel_x.getCols() / 2);
488 std::cout <<
"(I_sep_filtered == matImg_sobel_x)? " << test << std::endl;
491 std::cerr <<
"Failed separable filter!" << std::endl;
496 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
500 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)
static double getSobelKernelX(double *filter, unsigned int size)