-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilter2D_demo.cpp
87 lines (64 loc) · 1.75 KB
/
filter2D_demo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#define D(x) fprintf(stderr, "%s = %g\n", #x, (double) (x))
/**
* @file filter2D_demo.cpp
* @brief Sample code that shows how to implement your own linear filters by using filter2D function
* @author OpenCV team
*/
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
using namespace cv;
#define P(mat, i, j) (mat.data + mat.step[0]*i + mat.step[1]*j)
#define V(mat, i, j, type) (*((type *) P(mat, i, j)))
/**
* @function main
*/
int main ( int, char** argv )
{
/// Declare variables
Point anchor;
double delta;
int ddepth;
int kernel_size;
const char* window_name = "filter2D Demo";
int c;
Mat src, dst, dst2, in, fin;
/// Load an image
fin = imread( argv[1] );
if( !in.data ){
printf("no image");
return -1; }
cvtColor( in, src, CV_BGR2GRAY);
float a[9]={ -1.0, 0, 1.0,
-2.0, 0, 2.0,
-1.0, 0, 1.0};
Mat kernel( 3, 3, CV_32F, a);
/// Create window
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
/// Initialize arguments for the filter
anchor = Point( -1, -1 );
delta = 0;
ddepth = -1;
int i;
int j;
int ind = 0;
/// Update kernel size for a normalized box filter
kernel_size = 3;
/// Apply filter
filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT );
filter2D(src, dst2, ddepth , -kernel, anchor, delta, BORDER_DEFAULT );
for(int i=0 ; i < kernel.rows; i++){
for (int j=0; j < kernel.cols; j++){
fprintf(stderr, "%f,", V(kernel, i, j, float));
}
printf("\n");
}
D(kernel.cols);
imshow( "right side", abs(dst*4) );
imshow( "left side", (dst2*4) );
c = waitKey(0);
D(c);
ind++;
return 0;
}