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
97f5d05d
Commit
97f5d05d
authored
Jan 11, 2017
by
Alexander Alekhin
Committed by
GitHub
Jan 11, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #7960 from catree:tutorial_parallel_for_
Add OpenCV parallel_for_ tutorial.
parents
f4fdd94c
e16e141c
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
129 additions
and
0 deletions
+129
-0
how_to_use_OpenCV_parallel_for_.markdown
...CV_parallel_for_/how_to_use_OpenCV_parallel_for_.markdown
+0
-0
how_to_use_OpenCV_parallel_for_640px-Mandelset_hires.png
.../how_to_use_OpenCV_parallel_for_640px-Mandelset_hires.png
+0
-0
how_to_use_OpenCV_parallel_for_Mandelbrot.png
...for_/images/how_to_use_OpenCV_parallel_for_Mandelbrot.png
+0
-0
how_to_use_OpenCV_parallel_for_sqrt_scale_transformation.png
..._to_use_OpenCV_parallel_for_sqrt_scale_transformation.png
+0
-0
table_of_content_core.markdown
doc/tutorials/core/table_of_content_core.markdown
+7
-0
how_to_use_OpenCV_parallel_for_.cpp
..._OpenCV_parallel_for_/how_to_use_OpenCV_parallel_for_.cpp
+122
-0
No files found.
doc/tutorials/core/how_to_use_OpenCV_parallel_for_/how_to_use_OpenCV_parallel_for_.markdown
0 → 100644
View file @
97f5d05d
This diff is collapsed.
Click to expand it.
doc/tutorials/core/how_to_use_OpenCV_parallel_for_/images/how_to_use_OpenCV_parallel_for_640px-Mandelset_hires.png
0 → 100644
View file @
97f5d05d
16.4 KB
doc/tutorials/core/how_to_use_OpenCV_parallel_for_/images/how_to_use_OpenCV_parallel_for_Mandelbrot.png
0 → 100644
View file @
97f5d05d
61.8 KB
doc/tutorials/core/how_to_use_OpenCV_parallel_for_/images/how_to_use_OpenCV_parallel_for_sqrt_scale_transformation.png
0 → 100644
View file @
97f5d05d
33 KB
doc/tutorials/core/table_of_content_core.markdown
View file @
97f5d05d
...
...
@@ -106,3 +106,10 @@ understanding how to manipulate the images on a pixel level.
*Author:* Elena Gvozdeva
You will see how to use the IPP Async with OpenCV.
-
@subpage tutorial_how_to_use_OpenCV_parallel_for_
*Compatibility:* \>= OpenCV 2.4.3
You will see how to use the OpenCV parallel_for_ to easily parallelize your code.
samples/cpp/tutorial_code/core/how_to_use_OpenCV_parallel_for_/how_to_use_OpenCV_parallel_for_.cpp
0 → 100644
View file @
97f5d05d
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
using
namespace
std
;
using
namespace
cv
;
namespace
{
//! [mandelbrot-escape-time-algorithm]
int
mandelbrot
(
const
complex
<
float
>
&
z0
,
const
int
max
)
{
complex
<
float
>
z
=
z0
;
for
(
int
t
=
0
;
t
<
max
;
t
++
)
{
if
(
z
.
real
()
*
z
.
real
()
+
z
.
imag
()
*
z
.
imag
()
>
4.0
f
)
return
t
;
z
=
z
*
z
+
z0
;
}
return
max
;
}
//! [mandelbrot-escape-time-algorithm]
//! [mandelbrot-grayscale-value]
int
mandelbrotFormula
(
const
complex
<
float
>
&
z0
,
const
int
maxIter
=
500
)
{
int
value
=
mandelbrot
(
z0
,
maxIter
);
if
(
maxIter
-
value
==
0
)
{
return
0
;
}
return
cvRound
(
sqrt
(
value
/
(
float
)
maxIter
)
*
255
);
}
//! [mandelbrot-grayscale-value]
//! [mandelbrot-parallel]
class
ParallelMandelbrot
:
public
ParallelLoopBody
{
public
:
ParallelMandelbrot
(
Mat
&
img
,
const
float
x1
,
const
float
y1
,
const
float
scaleX
,
const
float
scaleY
)
:
m_img
(
img
),
m_x1
(
x1
),
m_y1
(
y1
),
m_scaleX
(
scaleX
),
m_scaleY
(
scaleY
)
{
}
virtual
void
operator
()(
const
Range
&
range
)
const
{
for
(
int
r
=
range
.
start
;
r
<
range
.
end
;
r
++
)
{
int
i
=
r
/
m_img
.
cols
;
int
j
=
r
%
m_img
.
cols
;
float
x0
=
j
/
m_scaleX
+
m_x1
;
float
y0
=
i
/
m_scaleY
+
m_y1
;
complex
<
float
>
z0
(
x0
,
y0
);
uchar
value
=
(
uchar
)
mandelbrotFormula
(
z0
);
m_img
.
ptr
<
uchar
>
(
i
)[
j
]
=
value
;
}
}
ParallelMandelbrot
&
operator
=
(
const
ParallelMandelbrot
&
)
{
return
*
this
;
};
private
:
Mat
&
m_img
;
float
m_x1
;
float
m_y1
;
float
m_scaleX
;
float
m_scaleY
;
};
//! [mandelbrot-parallel]
//! [mandelbrot-sequential]
void
sequentialMandelbrot
(
Mat
&
img
,
const
float
x1
,
const
float
y1
,
const
float
scaleX
,
const
float
scaleY
)
{
for
(
int
i
=
0
;
i
<
img
.
rows
;
i
++
)
{
for
(
int
j
=
0
;
j
<
img
.
cols
;
j
++
)
{
float
x0
=
j
/
scaleX
+
x1
;
float
y0
=
i
/
scaleY
+
y1
;
complex
<
float
>
z0
(
x0
,
y0
);
uchar
value
=
(
uchar
)
mandelbrotFormula
(
z0
);
img
.
ptr
<
uchar
>
(
i
)[
j
]
=
value
;
}
}
}
//! [mandelbrot-sequential]
}
int
main
()
{
//! [mandelbrot-transformation]
Mat
mandelbrotImg
(
4800
,
5400
,
CV_8U
);
float
x1
=
-
2.1
f
,
x2
=
0.6
f
;
float
y1
=
-
1.2
f
,
y2
=
1.2
f
;
float
scaleX
=
mandelbrotImg
.
cols
/
(
x2
-
x1
);
float
scaleY
=
mandelbrotImg
.
rows
/
(
y2
-
y1
);
//! [mandelbrot-transformation]
double
t1
=
(
double
)
getTickCount
();
//! [mandelbrot-parallel-call]
ParallelMandelbrot
parallelMandelbrot
(
mandelbrotImg
,
x1
,
y1
,
scaleX
,
scaleY
);
parallel_for_
(
Range
(
0
,
mandelbrotImg
.
rows
*
mandelbrotImg
.
cols
),
parallelMandelbrot
);
//! [mandelbrot-parallel-call]
t1
=
((
double
)
getTickCount
()
-
t1
)
/
getTickFrequency
();
cout
<<
"Parallel Mandelbrot: "
<<
t1
<<
" s"
<<
endl
;
Mat
mandelbrotImgSequential
(
4800
,
5400
,
CV_8U
);
double
t2
=
(
double
)
getTickCount
();
sequentialMandelbrot
(
mandelbrotImgSequential
,
x1
,
y1
,
scaleX
,
scaleY
);
t2
=
((
double
)
getTickCount
()
-
t2
)
/
getTickFrequency
();
cout
<<
"Sequential Mandelbrot: "
<<
t2
<<
" s"
<<
endl
;
cout
<<
"Speed-up: "
<<
t2
/
t1
<<
" X"
<<
endl
;
imwrite
(
"Mandelbrot_parallel.png"
,
mandelbrotImg
);
imwrite
(
"Mandelbrot_sequential.png"
,
mandelbrotImgSequential
);
return
EXIT_SUCCESS
;
}
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