mirror of
https://github.com/opencv/opencv.git
synced 2025-06-12 20:42:53 +08:00
Include code snippets with doxygen notation and small fix in table_of_content_objdetect.markdown
This commit is contained in:
parent
c3b83b8354
commit
e2a9cff3dc
@ -27,8 +27,7 @@ Example
|
|||||||
4. Set the required parameters for both GeneralizedHough variants
|
4. Set the required parameters for both GeneralizedHough variants
|
||||||
5. Detect and show found results
|
5. Detect and show found results
|
||||||
|
|
||||||
Note:
|
@note
|
||||||
|
|
||||||
- Both variants can't be instantiated directly. Using the create methods is required.
|
- Both variants can't be instantiated directly. Using the create methods is required.
|
||||||
- Guil Hough is very slow. Calculating the results for the "mini" files used in this tutorial
|
- Guil Hough is very slow. Calculating the results for the "mini" files used in this tutorial
|
||||||
takes only a few seconds. With image and template in a higher resolution, as shown below,
|
takes only a few seconds. With image and template in a higher resolution, as shown below,
|
||||||
@ -40,31 +39,14 @@ Note:
|
|||||||
### Code
|
### Code
|
||||||
|
|
||||||
The complete code for this tutorial is shown below.
|
The complete code for this tutorial is shown below.
|
||||||
@include generalizedHoughTransform.cpp
|
@include samples/cpp/tutorial_code/objectDetection/generalizedHoughTransform.cpp
|
||||||
|
|
||||||
Explanation
|
Explanation
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
### Load image, template and setup variables
|
### Load image, template and setup variables
|
||||||
|
|
||||||
```c++
|
@snippet samples/cpp/tutorial_code/objectDetection/generalizedHoughTransform.cpp generalized-hough-transform-load-and-setup
|
||||||
// load source images
|
|
||||||
Mat image = imread("images/generalized_hough_mini_image.jpg");
|
|
||||||
Mat imgTemplate = imread("images/generalized_hough_mini_template.jpg");
|
|
||||||
|
|
||||||
// create grayscale image and template
|
|
||||||
Mat templ = Mat(imgTemplate.rows, imgTemplate.cols, CV_8UC1);
|
|
||||||
Mat grayImage;
|
|
||||||
cvtColor(imgTemplate, templ, COLOR_RGB2GRAY);
|
|
||||||
cvtColor(image, grayImage, COLOR_RGB2GRAY);
|
|
||||||
|
|
||||||
// create variable for location, scale and rotation of detected templates
|
|
||||||
vector<Vec4f> positionBallard, positionGuil;
|
|
||||||
|
|
||||||
// template width and height
|
|
||||||
int w = templ.cols;
|
|
||||||
int h = templ.rows;
|
|
||||||
```
|
|
||||||
|
|
||||||
The position vectors will contain the matches the detectors will find.
|
The position vectors will contain the matches the detectors will find.
|
||||||
Every entry contains four floating point values:
|
Every entry contains four floating point values:
|
||||||
@ -79,87 +61,19 @@ An example could look as follows: `[200, 100, 0.9, 120]`
|
|||||||
|
|
||||||
### Setup parameters
|
### Setup parameters
|
||||||
|
|
||||||
```c++
|
@snippet samples/cpp/tutorial_code/objectDetection/generalizedHoughTransform.cpp generalized-hough-transform-setup-parameters
|
||||||
// create ballard and set options
|
|
||||||
Ptr<GeneralizedHoughBallard> ballard = createGeneralizedHoughBallard();
|
|
||||||
ballard->setMinDist(10);
|
|
||||||
ballard->setLevels(360);
|
|
||||||
ballard->setDp(2);
|
|
||||||
ballard->setMaxBufferSize(1000);
|
|
||||||
ballard->setVotesThreshold(40);
|
|
||||||
|
|
||||||
ballard->setCannyLowThresh(30);
|
|
||||||
ballard->setCannyHighThresh(110);
|
|
||||||
ballard->setTemplate(templ);
|
|
||||||
|
|
||||||
|
|
||||||
// create guil and set options
|
|
||||||
Ptr<GeneralizedHoughGuil> guil = createGeneralizedHoughGuil();
|
|
||||||
guil->setMinDist(10);
|
|
||||||
guil->setLevels(360);
|
|
||||||
guil->setDp(3);
|
|
||||||
guil->setMaxBufferSize(1000);
|
|
||||||
|
|
||||||
guil->setMinAngle(0);
|
|
||||||
guil->setMaxAngle(360);
|
|
||||||
guil->setAngleStep(1);
|
|
||||||
guil->setAngleThresh(1500);
|
|
||||||
|
|
||||||
guil->setMinScale(0.5);
|
|
||||||
guil->setMaxScale(2.0);
|
|
||||||
guil->setScaleStep(0.05);
|
|
||||||
guil->setScaleThresh(50);
|
|
||||||
|
|
||||||
guil->setPosThresh(10);
|
|
||||||
|
|
||||||
guil->setCannyLowThresh(30);
|
|
||||||
guil->setCannyHighThresh(110);
|
|
||||||
|
|
||||||
guil->setTemplate(templ);
|
|
||||||
```
|
|
||||||
|
|
||||||
Finding the optimal values can end up in trial and error and depends on many factors, such as the image resolution.
|
Finding the optimal values can end up in trial and error and depends on many factors, such as the image resolution.
|
||||||
|
|
||||||
### Run detection
|
### Run detection
|
||||||
|
|
||||||
```c++
|
@snippet samples/cpp/tutorial_code/objectDetection/generalizedHoughTransform.cpp generalized-hough-transform-run
|
||||||
// execute ballard detection
|
|
||||||
ballard->detect(grayImage, positionBallard);
|
|
||||||
// execute guil detection
|
|
||||||
guil->detect(grayImage, positionGuil);
|
|
||||||
```
|
|
||||||
|
|
||||||
As mentioned above, this step will take some time, especially with larger images and when using Guil.
|
As mentioned above, this step will take some time, especially with larger images and when using Guil.
|
||||||
|
|
||||||
### Draw results and show image
|
### Draw results and show image
|
||||||
|
|
||||||
```c++
|
@snippet samples/cpp/tutorial_code/objectDetection/generalizedHoughTransform.cpp generalized-hough-transform-draw-results
|
||||||
// draw ballard
|
|
||||||
for (vector<Vec4f>::iterator iter = positionBallard.begin(); iter != positionBallard.end(); ++iter) {
|
|
||||||
RotatedRect rRect = RotatedRect(Point2f((*iter)[0], (*iter)[1]),
|
|
||||||
Size2f(w * (*iter)[2], h * (*iter)[2]),
|
|
||||||
(*iter)[3]);
|
|
||||||
Point2f vertices[4];
|
|
||||||
rRect.points(vertices);
|
|
||||||
for (int i = 0; i < 4; i++)
|
|
||||||
line(image, vertices[i], vertices[(i + 1) % 4], Scalar(255, 0, 0), 6);
|
|
||||||
}
|
|
||||||
|
|
||||||
// draw guil
|
|
||||||
for (vector<Vec4f>::iterator iter = positionGuil.begin(); iter != positionGuil.end(); ++iter) {
|
|
||||||
RotatedRect rRect = RotatedRect(Point2f((*iter)[0], (*iter)[1]),
|
|
||||||
Size2f(w * (*iter)[2], h * (*iter)[2]),
|
|
||||||
(*iter)[3]);
|
|
||||||
Point2f vertices[4];
|
|
||||||
rRect.points(vertices);
|
|
||||||
for (int i = 0; i < 4; i++)
|
|
||||||
line(image, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0), 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
imshow("result_img", image);
|
|
||||||
waitKey();
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
```
|
|
||||||
|
|
||||||
Result
|
Result
|
||||||
------
|
------
|
||||||
|
@ -19,10 +19,10 @@ Ever wondered how your digital camera detects peoples and faces? Look here to fi
|
|||||||
|
|
||||||
- @subpage tutorial_generalized_hough_ballard_guil
|
- @subpage tutorial_generalized_hough_ballard_guil
|
||||||
|
|
||||||
Detect an object in a picture with the help of GeneralizedHoughBallard and GeneralizedHoughGuil.
|
|
||||||
|
|
||||||
*Languages:* C++
|
*Languages:* C++
|
||||||
|
|
||||||
*Compatibility:* \> OpenCV 3.4
|
*Compatibility:* \>= OpenCV 3.4
|
||||||
|
|
||||||
*Author:* Markus Heck
|
*Author:* Markus Heck
|
||||||
|
|
||||||
|
Detect an object in a picture with the help of GeneralizedHoughBallard and GeneralizedHoughGuil.
|
@ -11,6 +11,7 @@ using namespace cv;
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
//! [generalized-hough-transform-load-and-setup]
|
||||||
// load source images
|
// load source images
|
||||||
Mat image = imread("images/generalized_hough_mini_image.jpg");
|
Mat image = imread("images/generalized_hough_mini_image.jpg");
|
||||||
Mat imgTemplate = imread("images/generalized_hough_mini_template.jpg");
|
Mat imgTemplate = imread("images/generalized_hough_mini_template.jpg");
|
||||||
@ -27,7 +28,10 @@ int main() {
|
|||||||
// template width and height
|
// template width and height
|
||||||
int w = templ.cols;
|
int w = templ.cols;
|
||||||
int h = templ.rows;
|
int h = templ.rows;
|
||||||
|
//! [generalized-hough-transform-load-and-setup]
|
||||||
|
|
||||||
|
|
||||||
|
//! [generalized-hough-transform-setup-parameters]
|
||||||
// create ballard and set options
|
// create ballard and set options
|
||||||
Ptr<GeneralizedHoughBallard> ballard = createGeneralizedHoughBallard();
|
Ptr<GeneralizedHoughBallard> ballard = createGeneralizedHoughBallard();
|
||||||
ballard->setMinDist(10);
|
ballard->setMinDist(10);
|
||||||
@ -64,14 +68,18 @@ int main() {
|
|||||||
guil->setCannyHighThresh(110);
|
guil->setCannyHighThresh(110);
|
||||||
|
|
||||||
guil->setTemplate(templ);
|
guil->setTemplate(templ);
|
||||||
|
//! [generalized-hough-transform-setup-parameters]
|
||||||
|
|
||||||
|
|
||||||
|
//! [generalized-hough-transform-run]
|
||||||
// execute ballard detection
|
// execute ballard detection
|
||||||
ballard->detect(grayImage, positionBallard);
|
ballard->detect(grayImage, positionBallard);
|
||||||
// execute guil detection
|
// execute guil detection
|
||||||
guil->detect(grayImage, positionGuil);
|
guil->detect(grayImage, positionGuil);
|
||||||
|
//! [generalized-hough-transform-run]
|
||||||
|
|
||||||
|
|
||||||
|
//! [generalized-hough-transform-draw-results]
|
||||||
// draw ballard
|
// draw ballard
|
||||||
for (vector<Vec4f>::iterator iter = positionBallard.begin(); iter != positionBallard.end(); ++iter) {
|
for (vector<Vec4f>::iterator iter = positionBallard.begin(); iter != positionBallard.end(); ++iter) {
|
||||||
RotatedRect rRect = RotatedRect(Point2f((*iter)[0], (*iter)[1]),
|
RotatedRect rRect = RotatedRect(Point2f((*iter)[0], (*iter)[1]),
|
||||||
@ -96,5 +104,7 @@ int main() {
|
|||||||
|
|
||||||
imshow("result_img", image);
|
imshow("result_img", image);
|
||||||
waitKey();
|
waitKey();
|
||||||
|
//! [generalized-hough-transform-draw-results]
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user