39 #include <visp3/core/vpImageConvert.h> 40 #include <visp3/core/vpImageFilter.h> 41 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408) 42 #include <opencv2/imgproc/imgproc.hpp> 43 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020101) 44 #include <opencv2/imgproc/imgproc_c.h> 45 #elif defined(VISP_HAVE_OPENCV) 79 unsigned int half_size_y = size_y / 2, half_size_x = size_x / 2;
84 for (
unsigned int i = half_size_y; i < I.
getHeight() - half_size_y; i++) {
85 for (
unsigned int j = half_size_x; j < I.
getWidth() - half_size_x; j++) {
88 for (
unsigned int a = 0; a < size_y; a++) {
89 for (
unsigned int b = 0; b < size_x; b++) {
90 double val = I[i + half_size_y - a][j + half_size_x - b];
91 conv += M[a][b] * val;
98 for (
unsigned int i = half_size_y; i < I.
getHeight() - half_size_y; i++) {
99 for (
unsigned int j = half_size_x; j < I.
getWidth() - half_size_x; j++) {
102 for (
unsigned int a = 0; a < size_y; a++) {
103 for (
unsigned int b = 0; b < size_x; b++) {
104 double val = I[i - half_size_y + a][j - half_size_x + b];
105 corr += M[a][b] * val;
129 unsigned int size = M.
getRows();
130 unsigned int half_size = size / 2;
136 for (
unsigned int v = half_size; v < I.
getHeight() - half_size; v++) {
137 for (
unsigned int u = half_size; u < I.
getWidth() - half_size; u++) {
141 for (
unsigned int a = 0; a < size; a++) {
142 for (
unsigned int b = 0; b < size; b++) {
143 double val = I[v + half_size - a][u + half_size - b];
144 conv_u += M[a][b] * val;
145 conv_v += M[b][a] * val;
153 for (
unsigned int v = half_size; v < I.
getHeight() - half_size; v++) {
154 for (
unsigned int u = half_size; u < I.
getWidth() - half_size; u++) {
158 for (
unsigned int a = 0; a < size; a++) {
159 for (
unsigned int b = 0; b < size; b++) {
160 double val = I[v - half_size + a][u - half_size + b];
161 conv_u += M[a][b] * val;
162 conv_v += M[b][a] * val;
227 unsigned int size = kernelH.
size();
228 unsigned int half_size = size / 2;
233 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
234 for (
unsigned int j = half_size; j < I.
getWidth() - half_size; j++) {
236 for (
unsigned int a = 0; a < kernelH.
size(); a++) {
237 conv += kernelH[a] * I[i][j + half_size - a];
240 I_filter[i][j] = conv;
244 for (
unsigned int i = half_size; i < I.
getHeight() - half_size; i++) {
245 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
247 for (
unsigned int a = 0; a < kernelV.
size(); a++) {
248 conv += kernelV[a] * I_filter[i + half_size - a][j];
256 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100) 298 const unsigned int gaussianFilterSize,
const double thresholdCanny,
299 const unsigned int apertureSobel)
301 #if (VISP_HAVE_OPENCV_VERSION < 0x020408) 302 IplImage *img_ipl = NULL;
305 edges_ipl = cvCreateImage(cvSize(img_ipl->width, img_ipl->height), img_ipl->depth, img_ipl->nChannels);
307 cvSmooth(img_ipl, img_ipl, CV_GAUSSIAN, (
int)gaussianFilterSize, (
int)gaussianFilterSize, 0, 0);
308 cvCanny(img_ipl, edges_ipl, thresholdCanny, thresholdCanny, (
int)apertureSobel);
311 cvReleaseImage(&img_ipl);
312 cvReleaseImage(&edges_ipl);
314 cv::Mat img_cvmat, edges_cvmat;
316 cv::GaussianBlur(img_cvmat, img_cvmat, cv::Size((
int)gaussianFilterSize, (
int)gaussianFilterSize), 0, 0);
317 cv::Canny(img_cvmat, edges_cvmat, thresholdCanny, thresholdCanny, (
int)apertureSobel);
331 filterY(GIx, GI, filter, size);
342 filterY(GIx, GI, filter, size);
350 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
351 for (
unsigned int j = 0; j < (size - 1) / 2; j++) {
355 for (
unsigned int j = (size - 1) / 2; j < I.
getWidth() - (size - 1) / 2; j++) {
358 for (
unsigned int j = I.
getWidth() - (size - 1) / 2; j < I.
getWidth(); j++) {
367 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
368 for (
unsigned int j = 0; j < (size - 1) / 2; j++) {
372 for (
unsigned int j = (size - 1) / 2; j < I.
getWidth() - (size - 1) / 2; j++) {
375 for (
unsigned int j = I.
getWidth() - (size - 1) / 2; j < I.
getWidth(); j++) {
385 for (
unsigned int i = 0; i < (size - 1) / 2; i++) {
386 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
390 for (
unsigned int i = (size - 1) / 2; i < I.
getHeight() - (size - 1) / 2; i++) {
391 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
396 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
404 for (
unsigned int i = 0; i < (size - 1) / 2; i++) {
405 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
409 for (
unsigned int i = (size - 1) / 2; i < I.
getHeight() - (size - 1) / 2; i++) {
410 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
415 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
435 double *fg =
new double[(size + 1) / 2];
458 double *fg =
new double[(size + 1) / 2];
484 sigma = (size - 1) / 6.0;
486 int middle = (int)(size - 1) / 2;
488 for (
int i = 0; i <= middle; i++) {
489 filter[i] = (1. / (sigma * sqrt(2. * M_PI))) * exp(-(i * i) / (2. * sigma2));
494 for (
int i = 1; i <= middle; i++) {
495 sum += 2 * filter[i];
499 for (
int i = 0; i <= middle; i++) {
500 filter[i] = filter[i] / sum;
523 sigma = (size - 1) / 6.0;
525 int middle = (int)(size - 1) / 2;
528 for (
int i = 1; i <= middle; i++) {
529 filter[i] = -(1. / (sigma * sqrt(2. * M_PI))) *
530 (exp(-((i + 1) * (i + 1)) / (2. * sigma2)) - exp(-((i - 1) * (i - 1)) / (2. * sigma2))) / 2.;
535 for (
int i = 1; i <= middle; i++) {
536 sum += 2. * (1. / (sigma * sqrt(2. * M_PI))) * exp(-(i * i) / (2. * sigma2));
538 sum += (1. / (sigma * sqrt(2. * M_PI)));
540 for (
int i = 1; i <= middle; i++) {
541 filter[i] = filter[i] / sum;
550 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
551 for (
unsigned int j = 0; j < 3; j++) {
554 for (
unsigned int j = 3; j < I.
getWidth() - 3; j++) {
567 for (
unsigned int i = 0; i < 3; i++) {
568 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
572 for (
unsigned int i = 3; i < I.
getHeight() - 3; i++) {
573 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
578 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
589 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
590 for (
unsigned int j = 0; j < (size - 1) / 2; j++) {
593 for (
unsigned int j = (size - 1) / 2; j < I.
getWidth() - (size - 1) / 2; j++) {
596 for (
unsigned int j = I.
getWidth() - (size - 1) / 2; j < I.
getWidth(); j++) {
605 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
606 for (
unsigned int j = 0; j < (size - 1) / 2; j++) {
609 for (
unsigned int j = (size - 1) / 2; j < I.
getWidth() - (size - 1) / 2; j++) {
612 for (
unsigned int j = I.
getWidth() - (size - 1) / 2; j < I.
getWidth(); j++) {
623 for (
unsigned int i = 0; i < (size - 1) / 2; i++) {
624 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
629 for (
unsigned int i = (size - 1) / 2; i < I.
getHeight() - (size - 1) / 2; i++) {
630 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
636 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
646 for (
unsigned int i = 0; i < (size - 1) / 2; i++) {
647 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
651 for (
unsigned int i = (size - 1) / 2; i < I.
getHeight() - (size - 1) / 2; i++) {
652 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
657 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
674 const double *gaussianDerivativeKernel,
unsigned int size)
692 const double *gaussianDerivativeKernel,
unsigned int size)
703 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000) 704 cv::Mat imgsrc, imgdest;
706 cv::pyrDown(imgsrc, imgdest, cv::Size((
int)I.
getWidth() / 2, (int)I.
getHeight() / 2));
708 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408) 709 cv::Mat imgsrc, imgdest;
711 cv::pyrDown(imgsrc, imgdest, cvSize((
int)I.
getWidth() / 2, (int)I.
getHeight() / 2));
713 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100) 714 IplImage *imgsrc = NULL;
715 IplImage *imgdest = NULL;
716 imgsrc = cvCreateImage(cvSize((
int)I.
getWidth(), (int)I.
getHeight()), IPL_DEPTH_8U, 1);
717 imgdest = cvCreateImage(cvSize((
int)I.
getWidth() / 2, (int)I.
getHeight() / 2), IPL_DEPTH_8U, 1);
719 cvPyrDown(imgsrc, imgdest);
722 cvReleaseImage(&imgsrc);
723 cvReleaseImage(&imgdest);
736 for (
unsigned int i=0 ; i < I.
getHeight() ; i++)
739 for (
unsigned int j=1 ; j < ((I.
getWidth()+1.)/2.)-1 ; j++)
743 GI[i][(int)((I.
getWidth()+1.)/2.)-1]=I[i][2*((int)((I.
getWidth()+1.)/2.)-1)];
749 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
751 for (
unsigned int j = 1; j < w - 1; j++) {
754 GI[i][w - 1] = I[i][2 * w - 1];
764 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
766 for (
unsigned int i = 1; i < ((I.
getHeight() + 1.) / 2.) - 1; i++) {
769 GI[(int)((I.
getHeight() + 1.) / 2.) - 1][j] = I[2 * ((int)((I.
getHeight() + 1.) / 2.) - 1)][j];
776 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
778 for (
unsigned int i = 1; i < h - 1; i++) {
781 GI[h - 1][j] = I[2 * h - 1][j];
800 vpMatrix SobelY(size*2+1, size*2+1);
816 static const double SobelY3x3[9] = {-1.0, -2.0, -1.0,
819 static const double SobelY5x5[25] = {-1.0, -4.0, -6.0, -4.0, -1.0,
820 -2.0, -8.0, -12.0, -8.0, -2.0,
821 0.0, 0.0, 0.0, 0.0, 0.0,
822 2.0, 8.0, 12.0, 8.0, 2.0,
823 1.0, 4.0, 6.0, 4.0, 1.0};
824 static const double SobelY7x7[49] = {-1, -6, -15, -20, -15, -6, -1,
825 -4, -24, -60, -80, -60, -24, -4,
826 -5, -30, -75, -100, -75, -30, -5,
828 5, 30, 75, 100, 75, 30, 5,
829 4, 24, 60, 80, 60, 24, 4,
830 1, 6, 15, 20, 15, 6, 1};
831 static const vpMatrix smoothingKernel(3,3);
832 smoothingKernel[0][0] = 1.0; smoothingKernel[0][1] = 2.0; smoothingKernel[0][2] = 1.0;
833 smoothingKernel[1][0] = 2.0; smoothingKernel[1][1] = 4.0; smoothingKernel[1][2] = 2.0;
834 smoothingKernel[2][0] = 1.0; smoothingKernel[2][1] = 2.0; smoothingKernel[2][2] = 1.0;
841 const unsigned int kernel_size = size*2+1;
842 if (kernel_size == 3) {
843 memcpy(filter, SobelY3x3, kernel_size*kernel_size*
sizeof(
double));
846 if (kernel_size == 5) {
847 memcpy(filter, SobelY5x5, kernel_size*kernel_size*
sizeof(
double));
850 if (kernel_size == 7) {
851 memcpy(filter, SobelY7x7, kernel_size*kernel_size*
sizeof(
double));
857 for (
unsigned int i = 4; i <= size; i++) {
static double getSobelKernelY(double *filter, unsigned int size)
static void getGaussPyramidal(const vpImage< unsigned char > &I, vpImage< unsigned char > &GI)
Implementation of a matrix and operations on matrices.
static double filterXLeftBorder(const vpImage< unsigned char > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
static vpMatrix conv2(const vpMatrix &M, const vpMatrix &kernel, const std::string &mode="full")
unsigned int getWidth() const
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void getGradX(const vpImage< unsigned char > &I, vpImage< double > &dIx)
static void getGaussYPyramidal(const vpImage< unsigned char > &I, vpImage< unsigned char > &GI)
static void getGradY(const vpImage< unsigned char > &I, vpImage< double > &dIy)
static double derivativeFilterY(const vpImage< T > &I, const unsigned int r, const unsigned int c)
static void sepFilter(const vpImage< unsigned char > &I, vpImage< double > &If, const vpColVector &kernelH, const vpColVector &kernelV)
static void getGaussianKernel(double *filter, unsigned int size, double sigma=0., bool normalize=true)
error that can be emited by ViSP classes.
static unsigned char filterGaussYPyramidal(const vpImage< unsigned char > &I, unsigned int i, unsigned int j)
Type * data
Address of the first element of the data array.
static void getGradYGauss2D(const vpImage< unsigned char > &I, vpImage< double > &dIy, const double *gaussianKernel, const double *gaussianDerivativeKernel, unsigned int size)
unsigned int size() const
Return the number of elements of the 2D array.
unsigned int getCols() const
Error that can be emited by the vpImage class and its derivates.
static void getGradXGauss2D(const vpImage< unsigned char > &I, vpImage< double > &dIx, const double *gaussianKernel, const double *gaussianDerivativeKernel, unsigned int size)
static void filterY(const vpImage< unsigned char > &I, vpImage< double > &dIx, const double *filter, unsigned int size)
static void getGaussXPyramidal(const vpImage< unsigned char > &I, vpImage< unsigned char > &GI)
static double sqr(double x)
static void gaussianBlur(const vpImage< unsigned char > &I, vpImage< double > &GI, unsigned int size=7, double sigma=0., bool normalize=true)
static double filterYTopBorder(const vpImage< unsigned char > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
static double filterXRightBorder(const vpImage< unsigned char > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
void resize(const unsigned int h, const unsigned int w)
resize the image : Image initialization
unsigned int getRows() const
void destroy()
Destructor : Memory de-allocation.
static void filterX(const vpImage< unsigned char > &I, vpImage< double > &dIx, const double *filter, unsigned int size)
static void getGaussianDerivativeKernel(double *filter, unsigned int size, double sigma=0., bool normalize=true)
static double filterYBottomBorder(const vpImage< unsigned char > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
Implementation of column vector and the associated operations.
static unsigned char filterGaussXPyramidal(const vpImage< unsigned char > &I, unsigned int i, unsigned int j)
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)
static void canny(const vpImage< unsigned char > &I, vpImage< unsigned char > &Ic, const unsigned int gaussianFilterSize, const double thresholdCanny, const unsigned int apertureSobel)
static double derivativeFilterX(const vpImage< T > &I, const unsigned int r, const unsigned int c)