Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
ff2af7d8
Commit
ff2af7d8
authored
Feb 21, 2012
by
Andrey Kamaev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed Canny
parent
5785a890
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
112 additions
and
170 deletions
+112
-170
canny.cpp
modules/imgproc/src/canny.cpp
+112
-170
No files found.
modules/imgproc/src/canny.cpp
View file @
ff2af7d8
...
@@ -50,54 +50,60 @@ void cv::Canny( InputArray _src, OutputArray _dst,
...
@@ -50,54 +50,60 @@ void cv::Canny( InputArray _src, OutputArray _dst,
_dst
.
create
(
src
.
size
(),
CV_8U
);
_dst
.
create
(
src
.
size
(),
CV_8U
);
Mat
dst
=
_dst
.
getMat
();
Mat
dst
=
_dst
.
getMat
();
if
(
!
L2gradient
&&
(
aperture_size
&
CV_CANNY_L2_GRADIENT
)
==
CV_CANNY_L2_GRADIENT
)
{
//backward compatibility
aperture_size
&=
~
CV_CANNY_L2_GRADIENT
;
L2gradient
=
true
;
}
if
((
aperture_size
&
1
)
==
0
||
(
aperture_size
!=
-
1
&&
(
aperture_size
<
3
||
aperture_size
>
7
)))
CV_Error
(
CV_StsBadFlag
,
""
);
#ifdef HAVE_TEGRA_OPTIMIZATION
#ifdef HAVE_TEGRA_OPTIMIZATION
if
(
tegra
::
canny
(
src
,
dst
,
low_thresh
,
high_thresh
,
aperture_size
,
L2gradient
))
if
(
tegra
::
canny
(
src
,
dst
,
low_thresh
,
high_thresh
,
aperture_size
,
L2gradient
))
return
;
return
;
#endif
#endif
if
(
low_thresh
>
high_thresh
)
const
int
cn
=
src
.
channels
();
std
::
swap
(
low_thresh
,
high_thresh
);
cv
::
Mat
dx
(
src
.
rows
,
src
.
cols
,
CV_16SC
(
cn
));
cv
::
Mat
dy
(
src
.
rows
,
src
.
cols
,
CV_16SC
(
cn
));
if
(
(
aperture_size
&
1
)
==
0
||
(
aperture_size
!=
-
1
&&
(
aperture_size
<
3
||
aperture_size
>
7
))
)
cv
::
Sobel
(
src
,
dx
,
CV_16S
,
1
,
0
,
aperture_size
,
1
,
0
,
cv
::
BORDER_REPLICATE
);
CV_Error
(
CV_StsBadFlag
,
""
);
cv
::
Sobel
(
src
,
dy
,
CV_16S
,
0
,
1
,
aperture_size
,
1
,
0
,
cv
::
BORDER_REPLICATE
);
Mat
dx
,
dy
;
if
(
low_thresh
>
high_thresh
)
Sobel
(
src
,
dx
,
CV_16S
,
1
,
0
,
aperture_size
,
1
,
0
,
BORDER_REFLECT_101
);
std
::
swap
(
low_thresh
,
high_thresh
);
Sobel
(
src
,
dy
,
CV_16S
,
0
,
1
,
aperture_size
,
1
,
0
,
BORDER_REFLECT_101
);
int
low
,
high
;
if
(
L2gradient
)
if
(
L2gradient
)
{
{
Cv32suf
ul
,
uh
;
low_thresh
=
std
::
min
(
32767.0
,
low_thresh
);
ul
.
f
=
(
float
)
low_thresh
;
high_thresh
=
std
::
min
(
32767.0
,
high_thresh
);
uh
.
f
=
(
float
)
high_thresh
;
low
=
ul
.
i
;
if
(
low_thresh
>
0
)
low_thresh
*=
low_thresh
;
high
=
uh
.
i
;
if
(
high_thresh
>
0
)
high_thresh
*=
high_thresh
;
}
else
{
low
=
cvFloor
(
low_thresh
);
high
=
cvFloor
(
high_thresh
);
}
}
int
low
=
cvFloor
(
low_thresh
);
int
high
=
cvFloor
(
high_thresh
);
Size
size
=
src
.
size
()
;
ptrdiff_t
mapstep
=
src
.
cols
+
2
;
int
i
,
j
,
k
,
mstep
=
size
.
width
+
2
,
cn
=
src
.
channels
(
);
cv
::
AutoBuffer
<
uchar
>
buffer
((
src
.
cols
+
2
)
*
(
src
.
rows
+
2
)
+
cn
*
mapstep
*
3
*
sizeof
(
int
)
);
Mat
mask
(
size
.
height
+
2
,
mstep
,
CV_8U
);
int
*
mag_buf
[
3
];
memset
(
mask
.
ptr
<
uchar
>
(
0
),
1
,
mstep
);
mag_buf
[
0
]
=
(
int
*
)(
uchar
*
)
buffer
;
memset
(
mask
.
ptr
<
uchar
>
(
size
.
height
+
1
),
1
,
mstep
);
mag_buf
[
1
]
=
mag_buf
[
0
]
+
mapstep
*
cn
;
mag_buf
[
2
]
=
mag_buf
[
1
]
+
mapstep
*
cn
;
memset
(
mag_buf
[
0
],
0
,
/* cn* */
mapstep
*
sizeof
(
int
));
Mat
mag
(
6
+
cn
,
mstep
,
CV_32S
);
uchar
*
map
=
(
uchar
*
)(
mag_buf
[
2
]
+
mapstep
*
cn
);
mag
=
Scalar
::
all
(
0
);
memset
(
map
,
1
,
mapstep
);
int
*
mag_buf
[
3
]
=
{
mag
.
ptr
<
int
>
(
0
),
mag
.
ptr
<
int
>
(
1
),
mag
.
ptr
<
int
>
(
2
)
};
memset
(
map
+
mapstep
*
(
src
.
rows
+
1
),
1
,
mapstep
);
short
*
dxybuf
[
3
]
=
{
(
short
*
)
mag
.
ptr
<
int
>
(
3
),
(
short
*
)
mag
.
ptr
<
int
>
(
4
),
(
short
*
)
mag
.
ptr
<
int
>
(
5
)
};
int
*
mbuf
=
mag
.
ptr
<
int
>
(
6
);
int
maxsize
=
MAX
(
1
<<
10
,
size
.
width
*
size
.
height
/
10
);
int
maxsize
=
std
::
max
(
1
<<
10
,
src
.
cols
*
src
.
rows
/
10
);
std
::
vector
<
uchar
*>
stack
(
maxsize
);
std
::
vector
<
uchar
*>
stack
(
maxsize
);
uchar
**
stack_top
,
**
stack_bottom
;
uchar
**
stack_top
=
&
stack
[
0
]
;
stack_top
=
stack_bottom
=
&
stack
[
0
];
uchar
**
stack_bottom
=
&
stack
[
0
];
/* sector numbers
/* sector numbers
(Top-Left Origin)
(Top-Left Origin)
...
@@ -111,7 +117,7 @@ void cv::Canny( InputArray _src, OutputArray _dst,
...
@@ -111,7 +117,7 @@ void cv::Canny( InputArray _src, OutputArray _dst,
3 2 1
3 2 1
*/
*/
#define CANNY_PUSH(d) *(d) =
(uchar)2
, *stack_top++ = (d)
#define CANNY_PUSH(d) *(d) =
uchar(2)
, *stack_top++ = (d)
#define CANNY_POP(d) (d) = *--stack_top
#define CANNY_POP(d) (d) = *--stack_top
// calculate magnitude and angle of gradient, perform non-maxima supression.
// calculate magnitude and angle of gradient, perform non-maxima supression.
...
@@ -119,184 +125,129 @@ void cv::Canny( InputArray _src, OutputArray _dst,
...
@@ -119,184 +125,129 @@ void cv::Canny( InputArray _src, OutputArray _dst,
// 0 - the pixel might belong to an edge
// 0 - the pixel might belong to an edge
// 1 - the pixel can not belong to an edge
// 1 - the pixel can not belong to an edge
// 2 - the pixel does belong to an edge
// 2 - the pixel does belong to an edge
for
(
i
=
0
;
i
<=
size
.
height
;
i
++
)
for
(
int
i
=
0
;
i
<=
src
.
rows
;
i
++
)
{
{
int
*
_mag
=
mag_buf
[(
i
>
0
)
+
1
]
+
1
;
int
*
_norm
=
mag_buf
[(
i
>
0
)
+
1
]
+
1
;
float
*
_magf
=
(
float
*
)
_mag
;
if
(
i
<
src
.
rows
)
const
short
*
_dx
,
*
_dy
;
short
*
_ddx
,
*
_ddy
;
uchar
*
_map
;
int
x
,
y
;
ptrdiff_t
magstep1
,
magstep2
;
int
prev_flag
=
0
;
if
(
i
<
size
.
height
)
{
{
_dx
=
dx
.
ptr
<
short
>
(
i
);
short
*
_dx
=
dx
.
ptr
<
short
>
(
i
);
_dy
=
dy
.
ptr
<
short
>
(
i
);
short
*
_dy
=
dy
.
ptr
<
short
>
(
i
);
_ddx
=
dxybuf
[(
i
>
0
)
+
1
];
_ddy
=
_ddx
+
size
.
width
;
if
(
cn
>
1
)
if
(
!
L2gradient
)
{
{
_mag
=
mbuf
;
for
(
int
j
=
0
;
j
<
src
.
cols
*
cn
;
j
++
)
_magf
=
(
float
*
)
_mag
;
_norm
[
j
]
=
std
::
abs
(
int
(
_dx
[
j
]))
+
std
::
abs
(
int
(
_dy
[
j
]))
;
}
}
if
(
!
L2gradient
)
for
(
j
=
0
;
j
<
size
.
width
*
cn
;
j
++
)
_mag
[
j
]
=
std
::
abs
(
_dx
[
j
])
+
std
::
abs
(
_dy
[
j
]);
else
else
{
{
for
(
j
=
0
;
j
<
size
.
width
*
cn
;
j
++
)
for
(
int
j
=
0
;
j
<
src
.
cols
*
cn
;
j
++
)
{
_norm
[
j
]
=
int
(
_dx
[
j
])
*
_dx
[
j
]
+
int
(
_dy
[
j
])
*
_dy
[
j
];
x
=
_dx
[
j
];
y
=
_dy
[
j
];
_magf
[
j
]
=
sqrtf
((
float
)
x
*
x
+
(
float
)
y
*
y
);
}
}
if
(
cn
>
1
)
{
_mag
=
mag_buf
[(
i
>
0
)
+
1
]
+
1
;
for
(
j
=
0
;
j
<
size
.
width
;
j
++
)
{
_mag
[
j
]
=
mbuf
[(
j
+
1
)
*
cn
];
_ddx
[
j
]
=
_dx
[
j
*
cn
];
_ddy
[
j
]
=
_dy
[
j
*
cn
];
}
}
for
(
k
=
1
;
k
<
cn
;
k
++
)
if
(
cn
>
1
)
{
{
for
(
j
=
0
;
j
<
size
.
width
;
j
++
)
for
(
int
j
=
0
,
jn
=
0
;
j
<
src
.
cols
;
++
j
,
jn
+=
cn
)
if
(
mbuf
[(
j
+
1
)
*
cn
+
k
]
>
_mag
[
j
]
)
{
{
_mag
[
j
]
=
mbuf
[(
j
+
1
)
*
cn
+
k
];
int
maxIdx
=
jn
;
_ddx
[
j
]
=
_dx
[
j
*
cn
+
k
];
for
(
int
k
=
1
;
k
<
cn
;
++
k
)
_ddy
[
j
]
=
_dy
[
j
*
cn
+
k
];
if
(
_norm
[
jn
+
k
]
>
_norm
[
maxIdx
])
maxIdx
=
jn
+
k
;
_norm
[
j
]
=
_norm
[
maxIdx
];
_dx
[
j
]
=
_dx
[
maxIdx
];
_dy
[
j
]
=
_dy
[
maxIdx
];
}
}
}
}
_norm
[
-
1
]
=
_norm
[
src
.
cols
]
=
0
;
}
}
else
else
{
memset
(
_norm
-
1
,
0
,
/* cn* */
mapstep
*
sizeof
(
int
));
for
(
j
=
0
;
j
<
size
.
width
;
j
++
)
_ddx
[
j
]
=
_dx
[
j
];
_ddy
[
j
]
=
_dy
[
j
];
}
_mag
[
-
1
]
=
_mag
[
size
.
width
]
=
0
;
}
else
memset
(
_mag
-
1
,
0
,
(
size
.
width
+
2
)
*
sizeof
(
_mag
[
0
])
);
// at the very beginning we do not have a complete ring
// at the very beginning we do not have a complete ring
// buffer of 3 magnitude rows for non-maxima suppression
// buffer of 3 magnitude rows for non-maxima suppression
if
(
i
==
0
)
if
(
i
==
0
)
continue
;
continue
;
_map
=
&
mask
.
at
<
uchar
>
(
i
,
1
)
;
uchar
*
_map
=
map
+
mapstep
*
i
+
1
;
_map
[
-
1
]
=
_map
[
s
ize
.
width
]
=
1
;
_map
[
-
1
]
=
_map
[
s
rc
.
cols
]
=
1
;
_mag
=
mag_buf
[
1
]
+
1
;
// take the central row
int
*
_mag
=
mag_buf
[
1
]
+
1
;
// take the central row
_dx
=
dxy
buf
[
1
];
ptrdiff_t
magstep1
=
mag_buf
[
2
]
-
mag_
buf
[
1
];
_dy
=
_dx
+
size
.
width
;
ptrdiff_t
magstep2
=
mag_buf
[
0
]
-
mag_buf
[
1
]
;
magstep1
=
mag_buf
[
2
]
-
mag_buf
[
1
]
;
const
short
*
_x
=
dx
.
ptr
<
short
>
(
i
-
1
)
;
magstep2
=
mag_buf
[
0
]
-
mag_buf
[
1
]
;
const
short
*
_y
=
dy
.
ptr
<
short
>
(
i
-
1
)
;
if
(
(
stack_top
-
stack_bottom
)
+
size
.
width
>
maxsize
)
if
((
stack_top
-
stack_bottom
)
+
src
.
cols
>
maxsize
)
{
{
int
sz
=
(
int
)(
stack_top
-
stack_bottom
);
int
sz
=
(
int
)(
stack_top
-
stack_bottom
);
maxsize
=
MAX
(
maxsize
*
3
/
2
,
maxsize
+
size
.
width
)
;
maxsize
=
maxsize
*
3
/
2
;
stack
.
resize
(
maxsize
);
stack
.
resize
(
maxsize
);
stack_bottom
=
&
stack
[
0
];
stack_bottom
=
&
stack
[
0
];
stack_top
=
stack_bottom
+
sz
;
stack_top
=
stack_bottom
+
sz
;
}
}
for
(
j
=
0
;
j
<
size
.
width
;
j
++
)
int
prev_flag
=
0
;
for
(
int
j
=
0
;
j
<
src
.
cols
;
j
++
)
{
{
#define CANNY_SHIFT 15
#define CANNY_SHIFT 15
#define TG22 (int)(0.4142135623730950488016887242097*(1<<CANNY_SHIFT) + 0.5)
const
int
TG22
=
(
int
)(
0.4142135623730950488016887242097
*
(
1
<<
CANNY_SHIFT
)
+
0.5
);
x
=
_dx
[
j
];
y
=
_dy
[
j
];
int
s
=
x
^
y
;
int
m
=
_mag
[
j
];
int
m
=
_mag
[
j
];
x
=
std
::
abs
(
x
);
if
(
m
>
low
)
y
=
std
::
abs
(
y
);
if
(
m
>
low
)
{
{
int
tg22x
=
x
*
TG22
;
int
xs
=
_x
[
j
];
int
tg67x
=
tg22x
+
((
x
+
x
)
<<
CANNY_SHIFT
);
int
ys
=
_y
[
j
];
int
x
=
std
::
abs
(
xs
);
int
y
=
std
::
abs
(
ys
)
<<
CANNY_SHIFT
;
y
<<=
CANNY_SHIFT
;
int
tg22x
=
x
*
TG22
;
if
(
y
<
tg22x
)
if
(
y
<
tg22x
)
{
if
(
m
>
_mag
[
j
-
1
]
&&
m
>=
_mag
[
j
+
1
]
)
{
if
(
m
>
high
&&
!
prev_flag
&&
_map
[
j
-
mstep
]
!=
2
)
{
{
CANNY_PUSH
(
_map
+
j
);
if
(
m
>
_mag
[
j
-
1
]
&&
m
>=
_mag
[
j
+
1
])
goto
__ocv_canny_push
;
prev_flag
=
1
;
}
}
else
else
_map
[
j
]
=
(
uchar
)
0
;
continue
;
}
}
else
if
(
y
>
tg67x
)
{
if
(
m
>
_mag
[
j
+
magstep2
]
&&
m
>=
_mag
[
j
+
magstep1
]
)
{
{
if
(
m
>
high
&&
!
prev_flag
&&
_map
[
j
-
mstep
]
!=
2
)
int
tg67x
=
tg22x
+
(
x
<<
(
CANNY_SHIFT
+
1
));
if
(
y
>
tg67x
)
{
{
CANNY_PUSH
(
_map
+
j
);
if
(
m
>
_mag
[
j
+
magstep2
]
&&
m
>=
_mag
[
j
+
magstep1
])
goto
__ocv_canny_push
;
prev_flag
=
1
;
}
}
else
else
_map
[
j
]
=
(
uchar
)
0
;
{
continue
;
int
s
=
(
xs
^
ys
)
<
0
?
-
1
:
1
;
if
(
m
>
_mag
[
j
+
magstep2
-
s
]
&&
m
>
_mag
[
j
+
magstep1
+
s
])
goto
__ocv_canny_push
;
}
}
}
}
else
}
{
prev_flag
=
0
;
s
=
s
<
0
?
-
1
:
1
;
_map
[
j
]
=
uchar
(
1
)
;
if
(
m
>
_mag
[
j
+
magstep2
-
s
]
&&
m
>
_mag
[
j
+
magstep1
+
s
]
)
continue
;
{
__ocv_canny_push
:
if
(
m
>
high
&&
!
prev_flag
&&
_map
[
j
-
mstep
]
!=
2
)
if
(
!
prev_flag
&&
m
>
high
&&
_map
[
j
-
mapstep
]
!=
2
)
{
{
CANNY_PUSH
(
_map
+
j
);
CANNY_PUSH
(
_map
+
j
);
prev_flag
=
1
;
prev_flag
=
1
;
}
}
else
else
_map
[
j
]
=
(
uchar
)
0
;
_map
[
j
]
=
0
;
continue
;
}
}
}
prev_flag
=
0
;
_map
[
j
]
=
(
uchar
)
1
;
}
}
// scroll the ring buffer
s
// scroll the ring buffer
_mag
=
mag_buf
[
0
];
_mag
=
mag_buf
[
0
];
mag_buf
[
0
]
=
mag_buf
[
1
];
mag_buf
[
0
]
=
mag_buf
[
1
];
mag_buf
[
1
]
=
mag_buf
[
2
];
mag_buf
[
1
]
=
mag_buf
[
2
];
mag_buf
[
2
]
=
_mag
;
mag_buf
[
2
]
=
_mag
;
_ddx
=
dxybuf
[
0
];
dxybuf
[
0
]
=
dxybuf
[
1
];
dxybuf
[
1
]
=
dxybuf
[
2
];
dxybuf
[
2
]
=
_ddx
;
}
}
// now track the edges (hysteresis thresholding)
// now track the edges (hysteresis thresholding)
while
(
stack_top
>
stack_bottom
)
while
(
stack_top
>
stack_bottom
)
{
{
uchar
*
m
;
uchar
*
m
;
if
(
(
stack_top
-
stack_bottom
)
+
8
>
maxsize
)
if
((
stack_top
-
stack_bottom
)
+
8
>
maxsize
)
{
{
int
sz
=
(
int
)(
stack_top
-
stack_bottom
);
int
sz
=
(
int
)(
stack_top
-
stack_bottom
);
maxsize
=
MAX
(
maxsize
*
3
/
2
,
maxsize
+
8
)
;
maxsize
=
maxsize
*
3
/
2
;
stack
.
resize
(
maxsize
);
stack
.
resize
(
maxsize
);
stack_bottom
=
&
stack
[
0
];
stack_bottom
=
&
stack
[
0
];
stack_top
=
stack_bottom
+
sz
;
stack_top
=
stack_bottom
+
sz
;
...
@@ -304,32 +255,23 @@ void cv::Canny( InputArray _src, OutputArray _dst,
...
@@ -304,32 +255,23 @@ void cv::Canny( InputArray _src, OutputArray _dst,
CANNY_POP
(
m
);
CANNY_POP
(
m
);
if
(
!
m
[
-
1
]
)
if
(
!
m
[
-
1
])
CANNY_PUSH
(
m
-
1
);
CANNY_PUSH
(
m
-
1
);
if
(
!
m
[
1
])
CANNY_PUSH
(
m
+
1
);
if
(
!
m
[
1
]
)
if
(
!
m
[
-
mapstep
-
1
])
CANNY_PUSH
(
m
-
mapstep
-
1
);
CANNY_PUSH
(
m
+
1
);
if
(
!
m
[
-
mapstep
])
CANNY_PUSH
(
m
-
mapstep
);
if
(
!
m
[
-
mstep
-
1
]
)
if
(
!
m
[
-
mapstep
+
1
])
CANNY_PUSH
(
m
-
mapstep
+
1
);
CANNY_PUSH
(
m
-
mstep
-
1
);
if
(
!
m
[
mapstep
-
1
])
CANNY_PUSH
(
m
+
mapstep
-
1
);
if
(
!
m
[
-
mstep
]
)
if
(
!
m
[
mapstep
])
CANNY_PUSH
(
m
+
mapstep
);
CANNY_PUSH
(
m
-
mstep
);
if
(
!
m
[
mapstep
+
1
])
CANNY_PUSH
(
m
+
mapstep
+
1
);
if
(
!
m
[
-
mstep
+
1
]
)
CANNY_PUSH
(
m
-
mstep
+
1
);
if
(
!
m
[
mstep
-
1
]
)
CANNY_PUSH
(
m
+
mstep
-
1
);
if
(
!
m
[
mstep
]
)
CANNY_PUSH
(
m
+
mstep
);
if
(
!
m
[
mstep
+
1
]
)
CANNY_PUSH
(
m
+
mstep
+
1
);
}
}
// the final pass, form the final image
// the final pass, form the final image
for
(
i
=
0
;
i
<
size
.
height
;
i
++
)
const
uchar
*
pmap
=
map
+
mapstep
+
1
;
uchar
*
pdst
=
dst
.
ptr
();
for
(
int
i
=
0
;
i
<
src
.
rows
;
i
++
,
pmap
+=
mapstep
,
pdst
+=
dst
.
step
)
{
{
const
uchar
*
_map
=
mask
.
ptr
<
uchar
>
(
i
+
1
)
+
1
;
for
(
int
j
=
0
;
j
<
src
.
cols
;
j
++
)
uchar
*
_dst
=
dst
.
ptr
<
uchar
>
(
i
);
pdst
[
j
]
=
(
uchar
)
-
(
pmap
[
j
]
>>
1
);
for
(
j
=
0
;
j
<
size
.
width
;
j
++
)
_dst
[
j
]
=
(
uchar
)
-
(
_map
[
j
]
>>
1
);
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment