Doxygen tutorials: warnings cleared
This commit is contained in:
@@ -15,7 +15,9 @@ Cool Theory
|
||||
-----------
|
||||
|
||||
@note The explanation below belongs to the book **Learning OpenCV** by Bradski and Kaehler.
|
||||
Morphological Operations --------------------------
|
||||
|
||||
Morphological Operations
|
||||
------------------------
|
||||
|
||||
- In short: A set of operations that process images based on shapes. Morphological operations
|
||||
apply a *structuring element* to an input image and generate an output image.
|
||||
@@ -59,102 +61,8 @@ Code
|
||||
|
||||
This tutorial code's is shown lines below. You can also download it from
|
||||
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ImgProc/Morphology_1.cpp)
|
||||
@code{.cpp}
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "highgui.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
@includelineno samples/cpp/tutorial_code/ImgProc/Morphology_1.cpp
|
||||
|
||||
using namespace cv;
|
||||
|
||||
/// Global variables
|
||||
Mat src, erosion_dst, dilation_dst;
|
||||
|
||||
int erosion_elem = 0;
|
||||
int erosion_size = 0;
|
||||
int dilation_elem = 0;
|
||||
int dilation_size = 0;
|
||||
int const max_elem = 2;
|
||||
int const max_kernel_size = 21;
|
||||
|
||||
/* Function Headers */
|
||||
void Erosion( int, void* );
|
||||
void Dilation( int, void* );
|
||||
|
||||
/* @function main */
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Load an image
|
||||
src = imread( argv[1] );
|
||||
|
||||
if( !src.data )
|
||||
{ return -1; }
|
||||
|
||||
/// Create windows
|
||||
namedWindow( "Erosion Demo", WINDOW_AUTOSIZE );
|
||||
namedWindow( "Dilation Demo", WINDOW_AUTOSIZE );
|
||||
cvMoveWindow( "Dilation Demo", src.cols, 0 );
|
||||
|
||||
/// Create Erosion Trackbar
|
||||
createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Erosion Demo",
|
||||
&erosion_elem, max_elem,
|
||||
Erosion );
|
||||
|
||||
createTrackbar( "Kernel size:\n 2n +1", "Erosion Demo",
|
||||
&erosion_size, max_kernel_size,
|
||||
Erosion );
|
||||
|
||||
/// Create Dilation Trackbar
|
||||
createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Dilation Demo",
|
||||
&dilation_elem, max_elem,
|
||||
Dilation );
|
||||
|
||||
createTrackbar( "Kernel size:\n 2n +1", "Dilation Demo",
|
||||
&dilation_size, max_kernel_size,
|
||||
Dilation );
|
||||
|
||||
/// Default start
|
||||
Erosion( 0, 0 );
|
||||
Dilation( 0, 0 );
|
||||
|
||||
waitKey(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* @function Erosion */
|
||||
void Erosion( int, void* )
|
||||
{
|
||||
int erosion_type;
|
||||
if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
|
||||
else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
|
||||
else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
|
||||
|
||||
Mat element = getStructuringElement( erosion_type,
|
||||
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
|
||||
Point( erosion_size, erosion_size ) );
|
||||
|
||||
/// Apply the erosion operation
|
||||
erode( src, erosion_dst, element );
|
||||
imshow( "Erosion Demo", erosion_dst );
|
||||
}
|
||||
|
||||
/* @function Dilation */
|
||||
void Dilation( int, void* )
|
||||
{
|
||||
int dilation_type;
|
||||
if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
|
||||
else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
|
||||
else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
|
||||
|
||||
Mat element = getStructuringElement( dilation_type,
|
||||
Size( 2*dilation_size + 1, 2*dilation_size+1 ),
|
||||
Point( dilation_size, dilation_size ) );
|
||||
/// Apply the dilation operation
|
||||
dilate( src, dilation_dst, element );
|
||||
imshow( "Dilation Demo", dilation_dst );
|
||||
}
|
||||
@endcode
|
||||
Explanation
|
||||
-----------
|
||||
|
||||
@@ -195,9 +103,8 @@ Explanation
|
||||
- *src*: The source image
|
||||
- *erosion_dst*: The output image
|
||||
- *element*: This is the kernel we will use to perform the operation. If we do not
|
||||
specify, the default is a simple @ref cv::3x3\` matrix. Otherwise, we can specify its
|
||||
shape. For this, we need to use the function
|
||||
get_structuring_element:\`getStructuringElement :
|
||||
specify, the default is a simple `3x3` matrix. Otherwise, we can specify its
|
||||
shape. For this, we need to use the function cv::getStructuringElement :
|
||||
@code{.cpp}
|
||||
Mat element = getStructuringElement( erosion_type,
|
||||
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
|
||||
@@ -213,44 +120,42 @@ get_structuring_element:\`getStructuringElement :
|
||||
specified, it is assumed to be in the center.
|
||||
|
||||
- That is all. We are ready to perform the erosion of our image.
|
||||
|
||||
@note Additionally, there is another parameter that allows you to perform multiple erosions
|
||||
(iterations) at once. We are not using it in this simple tutorial, though. You can check out the
|
||||
Reference for more details.
|
||||
|
||||
1. **dilation:**
|
||||
3. **dilation:**
|
||||
|
||||
The code is below. As you can see, it is completely similar to the snippet of code for **erosion**.
|
||||
Here we also have the option of defining our kernel, its anchor point and the size of the operator
|
||||
to be used.
|
||||
@code{.cpp}
|
||||
/* @function Dilation */
|
||||
void Dilation( int, void* )
|
||||
{
|
||||
int dilation_type;
|
||||
if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
|
||||
else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
|
||||
else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
|
||||
The code is below. As you can see, it is completely similar to the snippet of code for **erosion**.
|
||||
Here we also have the option of defining our kernel, its anchor point and the size of the operator
|
||||
to be used.
|
||||
@code{.cpp}
|
||||
/* @function Dilation */
|
||||
void Dilation( int, void* )
|
||||
{
|
||||
int dilation_type;
|
||||
if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
|
||||
else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
|
||||
else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
|
||||
|
||||
Mat element = getStructuringElement( dilation_type,
|
||||
Size( 2*dilation_size + 1, 2*dilation_size+1 ),
|
||||
Point( dilation_size, dilation_size ) );
|
||||
/// Apply the dilation operation
|
||||
dilate( src, dilation_dst, element );
|
||||
imshow( "Dilation Demo", dilation_dst );
|
||||
}
|
||||
@endcode
|
||||
|
||||
Mat element = getStructuringElement( dilation_type,
|
||||
Size( 2*dilation_size + 1, 2*dilation_size+1 ),
|
||||
Point( dilation_size, dilation_size ) );
|
||||
/// Apply the dilation operation
|
||||
dilate( src, dilation_dst, element );
|
||||
imshow( "Dilation Demo", dilation_dst );
|
||||
}
|
||||
@endcode
|
||||
Results
|
||||
-------
|
||||
|
||||
- Compile the code above and execute it with an image as argument. For instance, using this image:
|
||||
Compile the code above and execute it with an image as argument. For instance, using this image:
|
||||
|
||||

|
||||
|
||||
We get the results below. Varying the indices in the Trackbars give different output images,
|
||||
naturally. Try them out! You can even try to add a third Trackbar to control the number of
|
||||
iterations.
|
||||
|
||||

|
||||

|
||||
|
||||
We get the results below. Varying the indices in the Trackbars give different output images,
|
||||
naturally. Try them out! You can even try to add a third Trackbar to control the number of
|
||||
iterations.
|
||||
|
||||

|
||||
|
||||
@@ -271,6 +271,6 @@ Results
|
||||
|
||||
We get the results below. Varying the indices in the Trackbars give different output images, naturally. Try them out! You can even try to add a third Trackbar to control the number of iterations.
|
||||
|
||||
.. image:: images/Morphology_1_Tutorial_Cover.jpg
|
||||
.. image:: images/Morphology_1_Result.jpg
|
||||
:alt: Dilation and Erosion application
|
||||
:align: center
|
||||
|
||||
|
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 26 KiB |
@@ -42,17 +42,17 @@ Theory
|
||||
|
||||
- What we want to do is to use our *model histogram* (that we know represents a skin tonality) to
|
||||
detect skin areas in our Test Image. Here are the steps
|
||||
a. In each pixel of our Test Image (i.e. \f$p(i,j)\f$ ), collect the data and find the
|
||||
-# In each pixel of our Test Image (i.e. \f$p(i,j)\f$ ), collect the data and find the
|
||||
correspondent bin location for that pixel (i.e. \f$( h_{i,j}, s_{i,j} )\f$ ).
|
||||
b. Lookup the *model histogram* in the correspondent bin - \f$( h_{i,j}, s_{i,j} )\f$ - and read
|
||||
-# Lookup the *model histogram* in the correspondent bin - \f$( h_{i,j}, s_{i,j} )\f$ - and read
|
||||
the bin value.
|
||||
c. Store this bin value in a new image (*BackProjection*). Also, you may consider to normalize
|
||||
-# Store this bin value in a new image (*BackProjection*). Also, you may consider to normalize
|
||||
the *model histogram* first, so the output for the Test Image can be visible for you.
|
||||
d. Applying the steps above, we get the following BackProjection image for our Test Image:
|
||||
-# Applying the steps above, we get the following BackProjection image for our Test Image:
|
||||
|
||||

|
||||
|
||||
e. In terms of statistics, the values stored in *BackProjection* represent the *probability*
|
||||
-# In terms of statistics, the values stored in *BackProjection* represent the *probability*
|
||||
that a pixel in *Test Image* belongs to a skin area, based on the *model histogram* that we
|
||||
use. For instance in our Test image, the brighter areas are more probable to be skin area
|
||||
(as they actually are), whereas the darker areas have less probability (notice that these
|
||||
@@ -72,13 +72,13 @@ Code
|
||||
- Display the backprojection and the histogram in windows.
|
||||
- **Downloadable code**:
|
||||
|
||||
a. Click
|
||||
-# Click
|
||||
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp)
|
||||
for the basic version (explained in this tutorial).
|
||||
b. For stuff slightly fancier (using H-S histograms and floodFill to define a mask for the
|
||||
-# For stuff slightly fancier (using H-S histograms and floodFill to define a mask for the
|
||||
skin area) you can check the [improved
|
||||
demo](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo2.cpp)
|
||||
c. ...or you can always check out the classical
|
||||
-# ...or you can always check out the classical
|
||||
[camshiftdemo](https://github.com/Itseez/opencv/tree/master/samples/cpp/camshiftdemo.cpp)
|
||||
in samples.
|
||||
|
||||
@@ -255,5 +255,3 @@ Results
|
||||
------ ------ ------
|
||||
|R0| |R1| |R2|
|
||||
------ ------ ------
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ In this tutorial you will learn how to:
|
||||
- To calculate histograms of arrays of images by using the OpenCV function @ref cv::calcHist
|
||||
- To normalize an array by using the function @ref cv::normalize
|
||||
|
||||
@note In the last tutorial (@ref histogram_equalization) we talked about a particular kind of
|
||||
@note In the last tutorial (@ref tutorial_histogram_equalization) we talked about a particular kind of
|
||||
histogram called *Image histogram*. Now we will considerate it in its more general concept. Read on!
|
||||
|
||||
### What are histograms?
|
||||
@@ -42,10 +42,10 @@ histogram called *Image histogram*. Now we will considerate it in its more gener
|
||||
keep count not only of color intensities, but of whatever image features that we want to measure
|
||||
(i.e. gradients, directions, etc).
|
||||
- Let's identify some parts of the histogram:
|
||||
a. **dims**: The number of parameters you want to collect data of. In our example, **dims = 1**
|
||||
-# **dims**: The number of parameters you want to collect data of. In our example, **dims = 1**
|
||||
because we are only counting the intensity values of each pixel (in a greyscale image).
|
||||
b. **bins**: It is the number of **subdivisions** in each dim. In our example, **bins = 16**
|
||||
c. **range**: The limits for the values to be measured. In this case: **range = [0,255]**
|
||||
-# **bins**: It is the number of **subdivisions** in each dim. In our example, **bins = 16**
|
||||
-# **range**: The limits for the values to be measured. In this case: **range = [0,255]**
|
||||
- What if you want to count two features? In this case your resulting histogram would be a 3D plot
|
||||
(in which x and y would be \f$bin_{x}\f$ and \f$bin_{y}\f$ for each feature and z would be the number of
|
||||
counts for each combination of \f$(bin_{x}, bin_{y})\f$. The same would apply for more features (of
|
||||
@@ -68,82 +68,8 @@ Code
|
||||
- **Downloadable code**: Click
|
||||
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/Histograms_Matching/calcHist_Demo.cpp)
|
||||
- **Code at glance:**
|
||||
@code{.cpp}
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
@includelineno samples/cpp/tutorial_code/Histograms_Matching/calcHist_Demo.cpp
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
/*
|
||||
* @function main
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
Mat src, dst;
|
||||
|
||||
/// Load image
|
||||
src = imread( argv[1], 1 );
|
||||
|
||||
if( !src.data )
|
||||
{ return -1; }
|
||||
|
||||
/// Separate the image in 3 places ( B, G and R )
|
||||
vector<Mat> bgr_planes;
|
||||
split( src, bgr_planes );
|
||||
|
||||
/// Establish the number of bins
|
||||
int histSize = 256;
|
||||
|
||||
/// Set the ranges ( for B,G,R) )
|
||||
float range[] = { 0, 256 } ;
|
||||
const float* histRange = { range };
|
||||
|
||||
bool uniform = true; bool accumulate = false;
|
||||
|
||||
Mat b_hist, g_hist, r_hist;
|
||||
|
||||
/// Compute the histograms:
|
||||
calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
|
||||
calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
|
||||
calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
|
||||
|
||||
// Draw the histograms for B, G and R
|
||||
int hist_w = 512; int hist_h = 400;
|
||||
int bin_w = cvRound( (double) hist_w/histSize );
|
||||
|
||||
Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
|
||||
|
||||
/// Normalize the result to [ 0, histImage.rows ]
|
||||
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
|
||||
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
|
||||
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
|
||||
|
||||
/// Draw for each channel
|
||||
for( int i = 1; i < histSize; i++ )
|
||||
{
|
||||
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
|
||||
Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
|
||||
Scalar( 255, 0, 0), 2, 8, 0 );
|
||||
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
|
||||
Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
|
||||
Scalar( 0, 255, 0), 2, 8, 0 );
|
||||
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
|
||||
Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
|
||||
Scalar( 0, 0, 255), 2, 8, 0 );
|
||||
}
|
||||
|
||||
/// Display
|
||||
namedWindow("calcHist Demo", WINDOW_AUTOSIZE );
|
||||
imshow("calcHist Demo", histImage );
|
||||
|
||||
waitKey(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@endcode
|
||||
Explanation
|
||||
-----------
|
||||
|
||||
@@ -169,26 +95,26 @@ Explanation
|
||||
|
||||
4. Now we are ready to start configuring the **histograms** for each plane. Since we are working
|
||||
with the B, G and R planes, we know that our values will range in the interval \f$[0,255]\f$
|
||||
a. Establish number of bins (5, 10...):
|
||||
-# Establish number of bins (5, 10...):
|
||||
@code{.cpp}
|
||||
int histSize = 256; //from 0 to 255
|
||||
@endcode
|
||||
b. Set the range of values (as we said, between 0 and 255 )
|
||||
-# Set the range of values (as we said, between 0 and 255 )
|
||||
@code{.cpp}
|
||||
/// Set the ranges ( for B,G,R) )
|
||||
float range[] = { 0, 256 } ; //the upper boundary is exclusive
|
||||
const float* histRange = { range };
|
||||
@endcode
|
||||
c. We want our bins to have the same size (uniform) and to clear the histograms in the
|
||||
-# We want our bins to have the same size (uniform) and to clear the histograms in the
|
||||
beginning, so:
|
||||
@code{.cpp}
|
||||
bool uniform = true; bool accumulate = false;
|
||||
@endcode
|
||||
d. Finally, we create the Mat objects to save our histograms. Creating 3 (one for each plane):
|
||||
-# Finally, we create the Mat objects to save our histograms. Creating 3 (one for each plane):
|
||||
@code{.cpp}
|
||||
Mat b_hist, g_hist, r_hist;
|
||||
@endcode
|
||||
e. We proceed to calculate the histograms by using the OpenCV function @ref cv::calcHist :
|
||||
-# We proceed to calculate the histograms by using the OpenCV function @ref cv::calcHist :
|
||||
@code{.cpp}
|
||||
/// Compute the histograms:
|
||||
calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
|
||||
@@ -254,18 +180,15 @@ Explanation
|
||||
Scalar( 0, 0, 255), 2, 8, 0 );
|
||||
}
|
||||
@endcode
|
||||
|
||||
we use the expression:
|
||||
|
||||
@code{.cpp}
|
||||
b_hist.at<float>(i)
|
||||
@endcode
|
||||
|
||||
where \f$i\f$ indicates the dimension. If it were a 2D-histogram we would use something like:
|
||||
|
||||
@code{.cpp}
|
||||
b_hist.at<float>( i, j )
|
||||
@endcode
|
||||
|
||||
8. Finally we display our histograms and wait for the user to exit:
|
||||
@code{.cpp}
|
||||
namedWindow("calcHist Demo", WINDOW_AUTOSIZE );
|
||||
@@ -275,6 +198,7 @@ Explanation
|
||||
|
||||
return 0;
|
||||
@endcode
|
||||
|
||||
Result
|
||||
------
|
||||
|
||||
@@ -285,5 +209,3 @@ Result
|
||||
2. Produces the following histogram:
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ Theory
|
||||
(\f$d(H_{1}, H_{2})\f$) to express how well both histograms match.
|
||||
- OpenCV implements the function @ref cv::compareHist to perform a comparison. It also offers 4
|
||||
different metrics to compute the matching:
|
||||
a. **Correlation ( CV_COMP_CORREL )**
|
||||
-# **Correlation ( CV_COMP_CORREL )**
|
||||
|
||||
\f[d(H_1,H_2) = \frac{\sum_I (H_1(I) - \bar{H_1}) (H_2(I) - \bar{H_2})}{\sqrt{\sum_I(H_1(I) - \bar{H_1})^2 \sum_I(H_2(I) - \bar{H_2})^2}}\f]
|
||||
|
||||
@@ -27,15 +27,15 @@ Theory
|
||||
|
||||
and \f$N\f$ is the total number of histogram bins.
|
||||
|
||||
b. **Chi-Square ( CV_COMP_CHISQR )**
|
||||
-# **Chi-Square ( CV_COMP_CHISQR )**
|
||||
|
||||
\f[d(H_1,H_2) = \sum _I \frac{\left(H_1(I)-H_2(I)\right)^2}{H_1(I)}\f]
|
||||
|
||||
c. **Intersection ( method=CV_COMP_INTERSECT )**
|
||||
-# **Intersection ( method=CV_COMP_INTERSECT )**
|
||||
|
||||
\f[d(H_1,H_2) = \sum _I \min (H_1(I), H_2(I))\f]
|
||||
|
||||
d. **Bhattacharyya distance ( CV_COMP_BHATTACHARYYA )**
|
||||
-# **Bhattacharyya distance ( CV_COMP_BHATTACHARYYA )**
|
||||
|
||||
\f[d(H_1,H_2) = \sqrt{1 - \frac{1}{\sqrt{\bar{H_1} \bar{H_2} N^2}} \sum_I \sqrt{H_1(I) \cdot H_2(I)}}\f]
|
||||
|
||||
@@ -165,4 +165,3 @@ match. As we can see, the match *base-base* is the highest of all as expected. A
|
||||
that the match *base-half* is the second best match (as we predicted). For the other two metrics,
|
||||
the less the result, the better the match. We can observe that the matches between the test 1 and
|
||||
test 2 with respect to the base are worse, which again, was expected.
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ Goal
|
||||
In this tutorial you will learn:
|
||||
|
||||
- What an image histogram is and why it is useful
|
||||
- To equalize histograms of images by using the OpenCV <function@ref> cv::equalizeHist
|
||||
- To equalize histograms of images by using the OpenCV function @ref cv::equalizeHist
|
||||
|
||||
Theory
|
||||
------
|
||||
@@ -59,54 +59,13 @@ Code
|
||||
- **What does this program do?**
|
||||
- Loads an image
|
||||
- Convert the original image to grayscale
|
||||
- Equalize the Histogram by using the OpenCV function @ref cv::EqualizeHist
|
||||
- Equalize the Histogram by using the OpenCV function @ref cv::equalizeHist
|
||||
- Display the source and equalized images in a window.
|
||||
- **Downloadable code**: Click
|
||||
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/Histograms_Matching/EqualizeHist_Demo.cpp)
|
||||
- **Code at glance:**
|
||||
@code{.cpp}
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
@includelineno samples/cpp/tutorial_code/Histograms_Matching/EqualizeHist_Demo.cpp
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
/* @function main */
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
Mat src, dst;
|
||||
|
||||
char* source_window = "Source image";
|
||||
char* equalized_window = "Equalized Image";
|
||||
|
||||
/// Load image
|
||||
src = imread( argv[1], 1 );
|
||||
|
||||
if( !src.data )
|
||||
{ cout<<"Usage: ./Histogram_Demo <path_to_image>"<<endl;
|
||||
return -1;}
|
||||
|
||||
/// Convert to grayscale
|
||||
cvtColor( src, src, COLOR_BGR2GRAY );
|
||||
|
||||
/// Apply Histogram Equalization
|
||||
equalizeHist( src, dst );
|
||||
|
||||
/// Display results
|
||||
namedWindow( source_window, WINDOW_AUTOSIZE );
|
||||
namedWindow( equalized_window, WINDOW_AUTOSIZE );
|
||||
|
||||
imshow( source_window, src );
|
||||
imshow( equalized_window, dst );
|
||||
|
||||
/// Wait until user exits the program
|
||||
waitKey(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@endcode
|
||||
Explanation
|
||||
-----------
|
||||
|
||||
@@ -149,6 +108,7 @@ Explanation
|
||||
waitKey(0);
|
||||
return 0;
|
||||
@endcode
|
||||
|
||||
Results
|
||||
-------
|
||||
|
||||
@@ -173,8 +133,6 @@ Results
|
||||
|
||||
Notice how the number of pixels is more distributed through the intensity range.
|
||||
|
||||
**note**
|
||||
|
||||
@note
|
||||
Are you wondering how did we draw the Histogram figures shown above? Check out the following
|
||||
tutorial!
|
||||
|
||||
|
||||
@@ -23,8 +23,8 @@ template image (patch).
|
||||
|
||||
- We need two primary components:
|
||||
|
||||
a. **Source image (I):** The image in which we expect to find a match to the template image
|
||||
b. **Template image (T):** The patch image which will be compared to the template image
|
||||
-# **Source image (I):** The image in which we expect to find a match to the template image
|
||||
-# **Template image (T):** The patch image which will be compared to the template image
|
||||
|
||||
our goal is to detect the highest matching area:
|
||||
|
||||
@@ -300,5 +300,3 @@ Results
|
||||
other possible high matches.
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
@@ -11,12 +11,12 @@ In this tutorial you will learn how to:
|
||||
Theory
|
||||
------
|
||||
|
||||
1. The *Canny Edge detector* was developed by John F. Canny in 1986. Also known to many as the
|
||||
*optimal detector*, Canny algorithm aims to satisfy three main criteria:
|
||||
- **Low error rate:** Meaning a good detection of only existent edges.
|
||||
- **Good localization:** The distance between edge pixels detected and real edge pixels have
|
||||
to be minimized.
|
||||
- **Minimal response:** Only one detector response per edge.
|
||||
The *Canny Edge detector* was developed by John F. Canny in 1986. Also known to many as the
|
||||
*optimal detector*, Canny algorithm aims to satisfy three main criteria:
|
||||
- **Low error rate:** Meaning a good detection of only existent edges.
|
||||
- **Good localization:** The distance between edge pixels detected and real edge pixels have
|
||||
to be minimized.
|
||||
- **Minimal response:** Only one detector response per edge.
|
||||
|
||||
### Steps
|
||||
|
||||
@@ -32,8 +32,7 @@ Theory
|
||||
\end{bmatrix}\f]
|
||||
|
||||
2. Find the intensity gradient of the image. For this, we follow a procedure analogous to Sobel:
|
||||
a. Apply a pair of convolution masks (in \f$x\f$ and \f$y\f$ directions:
|
||||
|
||||
1. Apply a pair of convolution masks (in \f$x\f$ and \f$y\f$ directions:
|
||||
\f[G_{x} = \begin{bmatrix}
|
||||
-1 & 0 & +1 \\
|
||||
-2 & 0 & +2 \\
|
||||
@@ -44,22 +43,20 @@ Theory
|
||||
+1 & +2 & +1
|
||||
\end{bmatrix}\f]
|
||||
|
||||
b. Find the gradient strength and direction with:
|
||||
|
||||
2. Find the gradient strength and direction with:
|
||||
\f[\begin{array}{l}
|
||||
G = \sqrt{ G_{x}^{2} + G_{y}^{2} } \\
|
||||
\theta = \arctan(\dfrac{ G_{y} }{ G_{x} })
|
||||
\end{array}\f]
|
||||
|
||||
The direction is rounded to one of four possible angles (namely 0, 45, 90 or 135)
|
||||
|
||||
3. *Non-maximum* suppression is applied. This removes pixels that are not considered to be part of
|
||||
an edge. Hence, only thin lines (candidate edges) will remain.
|
||||
4. *Hysteresis*: The final step. Canny does use two thresholds (upper and lower):
|
||||
|
||||
a. If a pixel gradient is higher than the *upper* threshold, the pixel is accepted as an edge
|
||||
b. If a pixel gradient value is below the *lower* threshold, then it is rejected.
|
||||
c. If the pixel gradient is between the two thresholds, then it will be accepted only if it is
|
||||
1. If a pixel gradient is higher than the *upper* threshold, the pixel is accepted as an edge
|
||||
2. If a pixel gradient value is below the *lower* threshold, then it is rejected.
|
||||
3. If the pixel gradient is between the two thresholds, then it will be accepted only if it is
|
||||
connected to a pixel that is above the *upper* threshold.
|
||||
|
||||
Canny recommended a *upper*:*lower* ratio between 2:1 and 3:1.
|
||||
@@ -78,76 +75,8 @@ Code
|
||||
|
||||
2. The tutorial code's is shown lines below. You can also download it from
|
||||
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ImgTrans/CannyDetector_Demo.cpp)
|
||||
@code{.cpp}
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
@includelineno samples/cpp/tutorial_code/ImgTrans/CannyDetector_Demo.cpp
|
||||
|
||||
using namespace cv;
|
||||
|
||||
/// Global variables
|
||||
|
||||
Mat src, src_gray;
|
||||
Mat dst, detected_edges;
|
||||
|
||||
int edgeThresh = 1;
|
||||
int lowThreshold;
|
||||
int const max_lowThreshold = 100;
|
||||
int ratio = 3;
|
||||
int kernel_size = 3;
|
||||
char* window_name = "Edge Map";
|
||||
|
||||
/*
|
||||
* @function CannyThreshold
|
||||
* @brief Trackbar callback - Canny thresholds input with a ratio 1:3
|
||||
*/
|
||||
void CannyThreshold(int, void*)
|
||||
{
|
||||
/// Reduce noise with a kernel 3x3
|
||||
blur( src_gray, detected_edges, Size(3,3) );
|
||||
|
||||
/// Canny detector
|
||||
Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
|
||||
|
||||
/// Using Canny's output as a mask, we display our result
|
||||
dst = Scalar::all(0);
|
||||
|
||||
src.copyTo( dst, detected_edges);
|
||||
imshow( window_name, dst );
|
||||
}
|
||||
|
||||
|
||||
/* @function main */
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Load an image
|
||||
src = imread( argv[1] );
|
||||
|
||||
if( !src.data )
|
||||
{ return -1; }
|
||||
|
||||
/// Create a matrix of the same type and size as src (for dst)
|
||||
dst.create( src.size(), src.type() );
|
||||
|
||||
/// Convert the image to grayscale
|
||||
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
||||
|
||||
/// Create a window
|
||||
namedWindow( window_name, WINDOW_AUTOSIZE );
|
||||
|
||||
/// Create a Trackbar for user to enter threshold
|
||||
createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );
|
||||
|
||||
/// Show the image
|
||||
CannyThreshold(0, 0);
|
||||
|
||||
/// Wait until user exit program by pressing a key
|
||||
waitKey(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@endcode
|
||||
Explanation
|
||||
-----------
|
||||
|
||||
@@ -164,11 +93,11 @@ Explanation
|
||||
char* window_name = "Edge Map";
|
||||
@endcode
|
||||
Note the following:
|
||||
|
||||
a. We establish a ratio of lower:upper threshold of 3:1 (with the variable *ratio*)
|
||||
b. We set the kernel size of \f$3\f$ (for the Sobel operations to be performed internally by the
|
||||
|
||||
1. We establish a ratio of lower:upper threshold of 3:1 (with the variable *ratio*)
|
||||
2. We set the kernel size of \f$3\f$ (for the Sobel operations to be performed internally by the
|
||||
Canny function)
|
||||
c. We set a maximum value for the lower Threshold of \f$100\f$.
|
||||
3. We set a maximum value for the lower Threshold of \f$100\f$.
|
||||
|
||||
2. Loads the source image:
|
||||
@code{.cpp}
|
||||
@@ -196,17 +125,17 @@ Explanation
|
||||
@endcode
|
||||
Observe the following:
|
||||
|
||||
a. The variable to be controlled by the Trackbar is *lowThreshold* with a limit of
|
||||
1. The variable to be controlled by the Trackbar is *lowThreshold* with a limit of
|
||||
*max_lowThreshold* (which we set to 100 previously)
|
||||
b. Each time the Trackbar registers an action, the callback function *CannyThreshold* will be
|
||||
2. Each time the Trackbar registers an action, the callback function *CannyThreshold* will be
|
||||
invoked.
|
||||
|
||||
7. Let's check the *CannyThreshold* function, step by step:
|
||||
a. First, we blur the image with a filter of kernel size 3:
|
||||
1. First, we blur the image with a filter of kernel size 3:
|
||||
@code{.cpp}
|
||||
blur( src_gray, detected_edges, Size(3,3) );
|
||||
@endcode
|
||||
b. Second, we apply the OpenCV function @ref cv::Canny :
|
||||
2. Second, we apply the OpenCV function @ref cv::Canny :
|
||||
@code{.cpp}
|
||||
Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
|
||||
@endcode
|
||||
@@ -224,12 +153,12 @@ Explanation
|
||||
@code{.cpp}
|
||||
dst = Scalar::all(0);
|
||||
@endcode
|
||||
9. Finally, we will use the function @ref cv::copyTo to map only the areas of the image that are
|
||||
9. Finally, we will use the function @ref cv::Mat::copyTo to map only the areas of the image that are
|
||||
identified as edges (on a black background).
|
||||
@code{.cpp}
|
||||
src.copyTo( dst, detected_edges);
|
||||
@endcode
|
||||
@ref cv::copyTo copy the *src* image onto *dst*. However, it will only copy the pixels in the
|
||||
@ref cv::Mat::copyTo copy the *src* image onto *dst*. However, it will only copy the pixels in the
|
||||
locations where they have non-zero values. Since the output of the Canny detector is the edge
|
||||
contours on a black background, the resulting *dst* will be black in all the area but the
|
||||
detected edges.
|
||||
@@ -251,4 +180,3 @@ Result
|
||||

|
||||
|
||||
- Notice how the image is superposed to the black background on the edge regions.
|
||||
|
||||
|
||||
@@ -24,8 +24,8 @@ Theory
|
||||
3. In this tutorial, we will briefly explore two ways of defining the extra padding (border) for an
|
||||
image:
|
||||
|
||||
a. **BORDER_CONSTANT**: Pad the image with a constant value (i.e. black or \f$0\f$
|
||||
b. **BORDER_REPLICATE**: The row or column at the very edge of the original is replicated to
|
||||
-# **BORDER_CONSTANT**: Pad the image with a constant value (i.e. black or \f$0\f$
|
||||
-# **BORDER_REPLICATE**: The row or column at the very edge of the original is replicated to
|
||||
the extra border.
|
||||
|
||||
This will be seen more clearly in the Code section.
|
||||
@@ -175,13 +175,13 @@ Explanation
|
||||
@endcode
|
||||
The arguments are:
|
||||
|
||||
a. *src*: Source image
|
||||
b. *dst*: Destination image
|
||||
c. *top*, *bottom*, *left*, *right*: Length in pixels of the borders at each side of the image.
|
||||
-# *src*: Source image
|
||||
-# *dst*: Destination image
|
||||
-# *top*, *bottom*, *left*, *right*: Length in pixels of the borders at each side of the image.
|
||||
We define them as being 5% of the original size of the image.
|
||||
d. *borderType*: Define what type of border is applied. It can be constant or replicate for
|
||||
-# *borderType*: Define what type of border is applied. It can be constant or replicate for
|
||||
this example.
|
||||
e. *value*: If *borderType* is *BORDER_CONSTANT*, this is the value used to fill the border
|
||||
-# *value*: If *borderType* is *BORDER_CONSTANT*, this is the value used to fill the border
|
||||
pixels.
|
||||
|
||||
8. We display our output image in the image created previously
|
||||
@@ -204,5 +204,3 @@ Results
|
||||
option looks:
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
@@ -159,15 +159,15 @@ Explanation
|
||||
@endcode
|
||||
The arguments denote:
|
||||
|
||||
a. *src*: Source image
|
||||
b. *dst*: Destination image
|
||||
c. *ddepth*: The depth of *dst*. A negative value (such as \f$-1\f$) indicates that the depth is
|
||||
-# *src*: Source image
|
||||
-# *dst*: Destination image
|
||||
-# *ddepth*: The depth of *dst*. A negative value (such as \f$-1\f$) indicates that the depth is
|
||||
the same as the source.
|
||||
d. *kernel*: The kernel to be scanned through the image
|
||||
e. *anchor*: The position of the anchor relative to its kernel. The location *Point(-1, -1)*
|
||||
-# *kernel*: The kernel to be scanned through the image
|
||||
-# *anchor*: The position of the anchor relative to its kernel. The location *Point(-1, -1)*
|
||||
indicates the center by default.
|
||||
f. *delta*: A value to be added to each pixel during the convolution. By default it is \f$0\f$
|
||||
g. *BORDER_DEFAULT*: We let this value by default (more details in the following tutorial)
|
||||
-# *delta*: A value to be added to each pixel during the convolution. By default it is \f$0\f$
|
||||
-# *BORDER_DEFAULT*: We let this value by default (more details in the following tutorial)
|
||||
|
||||
7. Our program will effectuate a *while* loop, each 500 ms the kernel size of our filter will be
|
||||
updated in the range indicated.
|
||||
@@ -180,5 +180,3 @@ Results
|
||||
the kernel size should change, as can be seen in the series of snapshots below:
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
@@ -20,8 +20,8 @@ straight lines. \#. To apply the Transform, first an edge detection pre-processi
|
||||
|
||||
1. As you know, a line in the image space can be expressed with two variables. For example:
|
||||
|
||||
a. In the **Cartesian coordinate system:** Parameters: \f$(m,b)\f$.
|
||||
b. In the **Polar coordinate system:** Parameters: \f$(r,\theta)\f$
|
||||
-# In the **Cartesian coordinate system:** Parameters: \f$(m,b)\f$.
|
||||
-# In the **Polar coordinate system:** Parameters: \f$(r,\theta)\f$
|
||||
|
||||

|
||||
|
||||
@@ -180,7 +180,7 @@ Explanation
|
||||
available for this purpose:
|
||||
|
||||
3. **Standard Hough Line Transform**
|
||||
a. First, you apply the Transform:
|
||||
-# First, you apply the Transform:
|
||||
@code{.cpp}
|
||||
vector<Vec2f> lines;
|
||||
HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 );
|
||||
@@ -196,7 +196,7 @@ Explanation
|
||||
- *threshold*: The minimum number of intersections to "*detect*" a line
|
||||
- *srn* and *stn*: Default parameters to zero. Check OpenCV reference for more info.
|
||||
|
||||
b. And then you display the result by drawing the lines.
|
||||
-# And then you display the result by drawing the lines.
|
||||
@code{.cpp}
|
||||
for( size_t i = 0; i < lines.size(); i++ )
|
||||
{
|
||||
@@ -212,7 +212,7 @@ Explanation
|
||||
}
|
||||
@endcode
|
||||
4. **Probabilistic Hough Line Transform**
|
||||
a. First you apply the transform:
|
||||
-# First you apply the transform:
|
||||
@code{.cpp}
|
||||
vector<Vec4i> lines;
|
||||
HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );
|
||||
@@ -231,7 +231,7 @@ Explanation
|
||||
this number of points are disregarded.
|
||||
- *maxLineGap*: The maximum gap between two points to be considered in the same line.
|
||||
|
||||
b. And then you display the result by drawing the lines.
|
||||
-# And then you display the result by drawing the lines.
|
||||
@code{.cpp}
|
||||
for( size_t i = 0; i < lines.size(); i++ )
|
||||
{
|
||||
@@ -267,4 +267,3 @@ We get the following result by using the Probabilistic Hough Line Transform:
|
||||
You may observe that the number of lines detected vary while you change the *threshold*. The
|
||||
explanation is sort of evident: If you establish a higher threshold, fewer lines will be detected
|
||||
(since you will need more points to declare a line detected).
|
||||
|
||||
|
||||
@@ -206,16 +206,16 @@ Explanation
|
||||
How do we update our mapping matrices *mat_x* and *mat_y*? Go on reading:
|
||||
|
||||
6. **Updating the mapping matrices:** We are going to perform 4 different mappings:
|
||||
a. Reduce the picture to half its size and will display it in the middle:
|
||||
-# Reduce the picture to half its size and will display it in the middle:
|
||||
|
||||
\f[h(i,j) = ( 2*i - src.cols/2 + 0.5, 2*j - src.rows/2 + 0.5)\f]
|
||||
|
||||
for all pairs \f$(i,j)\f$ such that: \f$\dfrac{src.cols}{4}<i<\dfrac{3 \cdot src.cols}{4}\f$ and
|
||||
\f$\dfrac{src.rows}{4}<j<\dfrac{3 \cdot src.rows}{4}\f$
|
||||
|
||||
b. Turn the image upside down: \f$h( i, j ) = (i, src.rows - j)\f$
|
||||
c. Reflect the image from left to right: \f$h(i,j) = ( src.cols - i, j )\f$
|
||||
d. Combination of b and c: \f$h(i,j) = ( src.cols - i, src.rows - j )\f$
|
||||
-# Turn the image upside down: \f$h( i, j ) = (i, src.rows - j)\f$
|
||||
-# Reflect the image from left to right: \f$h(i,j) = ( src.cols - i, j )\f$
|
||||
-# Combination of b and c: \f$h(i,j) = ( src.cols - i, src.rows - j )\f$
|
||||
|
||||
This is expressed in the following snippet. Here, *map_x* represents the first coordinate of
|
||||
*h(i,j)* and *map_y* the second coordinate.
|
||||
@@ -277,4 +277,3 @@ Result
|
||||
5. Reflecting it in both directions:
|
||||
|
||||

|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ Theory
|
||||
Assuming that the image to be operated is \f$I\f$:
|
||||
|
||||
1. We calculate two derivatives:
|
||||
a. **Horizontal changes**: This is computed by convolving \f$I\f$ with a kernel \f$G_{x}\f$ with odd
|
||||
1. **Horizontal changes**: This is computed by convolving \f$I\f$ with a kernel \f$G_{x}\f$ with odd
|
||||
size. For example for a kernel size of 3, \f$G_{x}\f$ would be computed as:
|
||||
|
||||
\f[G_{x} = \begin{bmatrix}
|
||||
@@ -62,7 +62,7 @@ Assuming that the image to be operated is \f$I\f$:
|
||||
-1 & 0 & +1
|
||||
\end{bmatrix} * I\f]
|
||||
|
||||
b. **Vertical changes**: This is computed by convolving \f$I\f$ with a kernel \f$G_{y}\f$ with odd
|
||||
2. **Vertical changes**: This is computed by convolving \f$I\f$ with a kernel \f$G_{y}\f$ with odd
|
||||
size. For example for a kernel size of 3, \f$G_{y}\f$ would be computed as:
|
||||
|
||||
\f[G_{y} = \begin{bmatrix}
|
||||
@@ -81,11 +81,10 @@ Assuming that the image to be operated is \f$I\f$:
|
||||
\f[G = |G_{x}| + |G_{y}|\f]
|
||||
|
||||
@note
|
||||
When the size of the kernel is @ref cv::3\`, the Sobel kernel shown above may produce noticeable
|
||||
When the size of the kernel is `3`, the Sobel kernel shown above may produce noticeable
|
||||
inaccuracies (after all, Sobel is only an approximation of the derivative). OpenCV addresses
|
||||
this inaccuracy for kernels of size 3 by using the :scharr:\`Scharr function. This is as fast
|
||||
but more accurate than the standar Sobel function. It implements the following kernels:
|
||||
|
||||
\f[G_{x} = \begin{bmatrix}
|
||||
-3 & 0 & +3 \\
|
||||
-10 & 0 & +10 \\
|
||||
@@ -95,11 +94,11 @@ Assuming that the image to be operated is \f$I\f$:
|
||||
0 & 0 & 0 \\
|
||||
+3 & +10 & +3
|
||||
\end{bmatrix}\f]
|
||||
|
||||
You can check out more information of this function in the OpenCV reference (@ref cv::Scharr ).
|
||||
Also, in the sample code below, you will notice that above the code for @ref cv::Sobel function
|
||||
there is also code for the @ref cv::Scharr function commented. Uncommenting it (and obviously
|
||||
commenting the Sobel stuff) should give you an idea of how this function works.
|
||||
@note
|
||||
You can check out more information of this function in the OpenCV reference (@ref cv::Scharr ).
|
||||
Also, in the sample code below, you will notice that above the code for @ref cv::Sobel function
|
||||
there is also code for the @ref cv::Scharr function commented. Uncommenting it (and obviously
|
||||
commenting the Sobel stuff) should give you an idea of how this function works.
|
||||
|
||||
Code
|
||||
----
|
||||
@@ -110,65 +109,8 @@ Code
|
||||
|
||||
2. The tutorial code's is shown lines below. You can also download it from
|
||||
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp)
|
||||
@code{.cpp}
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
@includelineno samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp
|
||||
|
||||
using namespace cv;
|
||||
|
||||
/* @function main */
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
|
||||
Mat src, src_gray;
|
||||
Mat grad;
|
||||
char* window_name = "Sobel Demo - Simple Edge Detector";
|
||||
int scale = 1;
|
||||
int delta = 0;
|
||||
int ddepth = CV_16S;
|
||||
|
||||
int c;
|
||||
|
||||
/// Load an image
|
||||
src = imread( argv[1] );
|
||||
|
||||
if( !src.data )
|
||||
{ return -1; }
|
||||
|
||||
GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
|
||||
|
||||
/// Convert it to gray
|
||||
cvtColor( src, src_gray, COLOR_RGB2GRAY );
|
||||
|
||||
/// Create window
|
||||
namedWindow( window_name, WINDOW_AUTOSIZE );
|
||||
|
||||
/// Generate grad_x and grad_y
|
||||
Mat grad_x, grad_y;
|
||||
Mat abs_grad_x, abs_grad_y;
|
||||
|
||||
/// Gradient X
|
||||
//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
|
||||
Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
|
||||
convertScaleAbs( grad_x, abs_grad_x );
|
||||
|
||||
/// Gradient Y
|
||||
//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
|
||||
Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
|
||||
convertScaleAbs( grad_y, abs_grad_y );
|
||||
|
||||
/// Total Gradient (approximate)
|
||||
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
|
||||
|
||||
imshow( window_name, grad );
|
||||
|
||||
waitKey(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@endcode
|
||||
Explanation
|
||||
-----------
|
||||
|
||||
@@ -239,5 +181,3 @@ Results
|
||||
1. Here is the output of applying our basic detector to *lena.jpg*:
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
@@ -18,9 +18,9 @@ Theory
|
||||
transformation) followed by a *vector addition* (translation).
|
||||
2. From the above, We can use an Affine Transformation to express:
|
||||
|
||||
a. Rotations (linear transformation)
|
||||
b. Translations (vector addition)
|
||||
c. Scale operations (linear transformation)
|
||||
-# Rotations (linear transformation)
|
||||
-# Translations (vector addition)
|
||||
-# Scale operations (linear transformation)
|
||||
|
||||
you can see that, in essence, an Affine Transformation represents a **relation** between two
|
||||
images.
|
||||
@@ -41,7 +41,7 @@ Theory
|
||||
|
||||
begin{bmatrix}
|
||||
a_{00} & a_{01} & b_{00} \\ a_{10} & a_{11} & b_{10}
|
||||
|
||||
|
||||
end{bmatrix}_{2 times 3}
|
||||
|
||||
Considering that we want to transform a 2D vector \f$X = \begin{bmatrix}x \\ y\end{bmatrix}\f$ by
|
||||
@@ -58,8 +58,8 @@ Theory
|
||||
|
||||
1. Excellent question. We mentioned that an Affine Transformation is basically a **relation**
|
||||
between two images. The information about this relation can come, roughly, in two ways:
|
||||
a. We know both \f$X\f$ and T and we also know that they are related. Then our job is to find \f$M\f$
|
||||
b. We know \f$M\f$ and \f$X\f$. To obtain \f$T\f$ we only need to apply \f$T = M \cdot X\f$. Our information
|
||||
-# We know both \f$X\f$ and T and we also know that they are related. Then our job is to find \f$M\f$
|
||||
-# We know \f$M\f$ and \f$X\f$. To obtain \f$T\f$ we only need to apply \f$T = M \cdot X\f$. Our information
|
||||
for \f$M\f$ may be explicit (i.e. have the 2-by-3 matrix) or it can come as a geometric relation
|
||||
between points.
|
||||
|
||||
@@ -219,9 +219,9 @@ Explanation
|
||||
|
||||
7. **Rotate:** To rotate an image, we need to know two things:
|
||||
|
||||
a. The center with respect to which the image will rotate
|
||||
b. The angle to be rotated. In OpenCV a positive angle is counter-clockwise
|
||||
c. *Optional:* A scale factor
|
||||
-# The center with respect to which the image will rotate
|
||||
-# The angle to be rotated. In OpenCV a positive angle is counter-clockwise
|
||||
-# *Optional:* A scale factor
|
||||
|
||||
We define these parameters with the following snippet:
|
||||
@code{.cpp}
|
||||
@@ -269,5 +269,3 @@ Result
|
||||
factor, we get:
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
@@ -12,8 +12,7 @@ In this tutorial you will learn how to:
|
||||
Theory
|
||||
------
|
||||
|
||||
@note The explanation below belongs to the book **Learning OpenCV** by Bradski and Kaehler. ..
|
||||
container:: enumeratevisibleitemswithsquare
|
||||
@note The explanation below belongs to the book **Learning OpenCV** by Bradski and Kaehler.
|
||||
|
||||
- Usually we need to convert an image to a size different than its original. For this, there are
|
||||
two possible options:
|
||||
@@ -53,161 +52,109 @@ container:: enumeratevisibleitemswithsquare
|
||||
predecessor. Iterating this process on the input image \f$G_{0}\f$ (original image) produces the
|
||||
entire pyramid.
|
||||
- The procedure above was useful to downsample an image. What if we want to make it bigger?:
|
||||
columns filled with zeros (\f$0\f$)
|
||||
- First, upsize the image to twice the original in each dimension, wit the new even rows and
|
||||
columns filled with zeros (\f$0\f$)
|
||||
- Perform a convolution with the same kernel shown above (multiplied by 4) to approximate the
|
||||
values of the "missing pixels"
|
||||
- These two procedures (downsampling and upsampling as explained above) are implemented by the
|
||||
OpenCV functions @ref cv::pyrUp and @ref cv::pyrDown , as we will see in an example with the
|
||||
code below:
|
||||
|
||||
@note When we reduce the size of an image, we are actually *losing* information of the image. Code
|
||||
======
|
||||
@note When we reduce the size of an image, we are actually *losing* information of the image.
|
||||
|
||||
Code
|
||||
----
|
||||
|
||||
This tutorial code's is shown lines below. You can also download it from
|
||||
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ImgProc/Pyramids.cpp)
|
||||
@code{.cpp}
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace cv;
|
||||
@includelineno samples/cpp/tutorial_code/ImgProc/Pyramids.cpp
|
||||
|
||||
/// Global variables
|
||||
Mat src, dst, tmp;
|
||||
char* window_name = "Pyramids Demo";
|
||||
|
||||
|
||||
/*
|
||||
* @function main
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// General instructions
|
||||
printf( "\n Zoom In-Out demo \n " );
|
||||
printf( "------------------ \n" );
|
||||
printf( " * [u] -> Zoom in \n" );
|
||||
printf( " * [d] -> Zoom out \n" );
|
||||
printf( " * [ESC] -> Close program \n \n" );
|
||||
|
||||
/// Test image - Make sure it s divisible by 2^{n}
|
||||
src = imread( "../images/chicky_512.jpg" );
|
||||
if( !src.data )
|
||||
{ printf(" No data! -- Exiting the program \n");
|
||||
return -1; }
|
||||
|
||||
tmp = src;
|
||||
dst = tmp;
|
||||
|
||||
/// Create window
|
||||
namedWindow( window_name, WINDOW_AUTOSIZE );
|
||||
imshow( window_name, dst );
|
||||
|
||||
/// Loop
|
||||
while( true )
|
||||
{
|
||||
int c;
|
||||
c = waitKey(10);
|
||||
|
||||
if( (char)c == 27 )
|
||||
{ break; }
|
||||
if( (char)c == 'u' )
|
||||
{ pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 ) );
|
||||
printf( "** Zoom In: Image x 2 \n" );
|
||||
}
|
||||
else if( (char)c == 'd' )
|
||||
{ pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 ) );
|
||||
printf( "** Zoom Out: Image / 2 \n" );
|
||||
}
|
||||
|
||||
imshow( window_name, dst );
|
||||
tmp = dst;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@endcode
|
||||
Explanation
|
||||
-----------
|
||||
|
||||
1. Let's check the general structure of the program:
|
||||
- Load an image (in this case it is defined in the program, the user does not have to enter it
|
||||
as an argument)
|
||||
@code{.cpp}
|
||||
/// Test image - Make sure it s divisible by 2^{n}
|
||||
src = imread( "../images/chicky_512.jpg" );
|
||||
if( !src.data )
|
||||
{ printf(" No data! -- Exiting the program \n");
|
||||
return -1; }
|
||||
@endcode
|
||||
- Create a Mat object to store the result of the operations (*dst*) and one to save temporal
|
||||
results (*tmp*).
|
||||
@code{.cpp}
|
||||
Mat src, dst, tmp;
|
||||
/* ... */
|
||||
tmp = src;
|
||||
dst = tmp;
|
||||
@endcode
|
||||
- Create a window to display the result
|
||||
@code{.cpp}
|
||||
namedWindow( window_name, WINDOW_AUTOSIZE );
|
||||
imshow( window_name, dst );
|
||||
@endcode
|
||||
- Perform an infinite loop waiting for user input.
|
||||
@code{.cpp}
|
||||
while( true )
|
||||
{
|
||||
int c;
|
||||
c = waitKey(10);
|
||||
Let's check the general structure of the program:
|
||||
|
||||
if( (char)c == 27 )
|
||||
{ break; }
|
||||
if( (char)c == 'u' )
|
||||
{ pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 ) );
|
||||
printf( "** Zoom In: Image x 2 \n" );
|
||||
}
|
||||
else if( (char)c == 'd' )
|
||||
{ pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 ) );
|
||||
printf( "** Zoom Out: Image / 2 \n" );
|
||||
}
|
||||
- Load an image (in this case it is defined in the program, the user does not have to enter it
|
||||
as an argument)
|
||||
@code{.cpp}
|
||||
/// Test image - Make sure it s divisible by 2^{n}
|
||||
src = imread( "../images/chicky_512.jpg" );
|
||||
if( !src.data )
|
||||
{ printf(" No data! -- Exiting the program \n");
|
||||
return -1; }
|
||||
@endcode
|
||||
|
||||
imshow( window_name, dst );
|
||||
tmp = dst;
|
||||
- Create a Mat object to store the result of the operations (*dst*) and one to save temporal
|
||||
results (*tmp*).
|
||||
@code{.cpp}
|
||||
Mat src, dst, tmp;
|
||||
/* ... */
|
||||
tmp = src;
|
||||
dst = tmp;
|
||||
@endcode
|
||||
|
||||
- Create a window to display the result
|
||||
@code{.cpp}
|
||||
namedWindow( window_name, WINDOW_AUTOSIZE );
|
||||
imshow( window_name, dst );
|
||||
@endcode
|
||||
|
||||
- Perform an infinite loop waiting for user input.
|
||||
@code{.cpp}
|
||||
while( true )
|
||||
{
|
||||
int c;
|
||||
c = waitKey(10);
|
||||
|
||||
if( (char)c == 27 )
|
||||
{ break; }
|
||||
if( (char)c == 'u' )
|
||||
{ pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 ) );
|
||||
printf( "** Zoom In: Image x 2 \n" );
|
||||
}
|
||||
else if( (char)c == 'd' )
|
||||
{ pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 ) );
|
||||
printf( "** Zoom Out: Image / 2 \n" );
|
||||
}
|
||||
|
||||
imshow( window_name, dst );
|
||||
tmp = dst;
|
||||
}
|
||||
@endcode
|
||||
Our program exits if the user presses *ESC*. Besides, it has two options:
|
||||
|
||||
- **Perform upsampling (after pressing 'u')**
|
||||
@code{.cpp}
|
||||
pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 )
|
||||
@endcode
|
||||
Our program exits if the user presses *ESC*. Besides, it has two options:
|
||||
We use the function @ref cv::pyrUp with 03 arguments:
|
||||
|
||||
- **Perform upsampling (after pressing 'u')**
|
||||
@code{.cpp}
|
||||
pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 )
|
||||
@endcode
|
||||
We use the function @ref cv::pyrUp with 03 arguments:
|
||||
- *tmp*: The current image, it is initialized with the *src* original image.
|
||||
- *dst*: The destination image (to be shown on screen, supposedly the double of the
|
||||
input image)
|
||||
- *Size( tmp.cols*2, tmp.rows\*2 )\* : The destination size. Since we are upsampling,
|
||||
@ref cv::pyrUp expects a size double than the input image (in this case *tmp*).
|
||||
- **Perform downsampling (after pressing 'd')**
|
||||
@code{.cpp}
|
||||
pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 )
|
||||
@endcode
|
||||
Similarly as with @ref cv::pyrUp , we use the function @ref cv::pyrDown with 03
|
||||
arguments:
|
||||
|
||||
- *tmp*: The current image, it is initialized with the *src* original image.
|
||||
- *dst*: The destination image (to be shown on screen, supposedly the double of the
|
||||
input image)
|
||||
- *Size( tmp.cols*2, tmp.rows\*2 )\* : The destination size. Since we are upsampling,
|
||||
@ref cv::pyrUp expects a size double than the input image (in this case *tmp*).
|
||||
- **Perform downsampling (after pressing 'd')**
|
||||
@code{.cpp}
|
||||
pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 )
|
||||
@endcode
|
||||
Similarly as with @ref cv::pyrUp , we use the function @ref cv::pyrDown with 03
|
||||
arguments:
|
||||
- *tmp*: The current image, it is initialized with the *src* original image.
|
||||
- *dst*: The destination image (to be shown on screen, supposedly half the input
|
||||
image)
|
||||
- *Size( tmp.cols/2, tmp.rows/2 )* : The destination size. Since we are upsampling,
|
||||
@ref cv::pyrDown expects half the size the input image (in this case *tmp*).
|
||||
- Notice that it is important that the input image can be divided by a factor of two (in
|
||||
both dimensions). Otherwise, an error will be shown.
|
||||
- Finally, we update the input image **tmp** with the current image displayed, so the
|
||||
subsequent operations are performed on it.
|
||||
@code{.cpp}
|
||||
tmp = dst;
|
||||
@endcode
|
||||
|
||||
- *tmp*: The current image, it is initialized with the *src* original image.
|
||||
- *dst*: The destination image (to be shown on screen, supposedly half the input
|
||||
image)
|
||||
- *Size( tmp.cols/2, tmp.rows/2 )* : The destination size. Since we are upsampling,
|
||||
@ref cv::pyrDown expects half the size the input image (in this case *tmp*).
|
||||
- Notice that it is important that the input image can be divided by a factor of two (in
|
||||
both dimensions). Otherwise, an error will be shown.
|
||||
- Finally, we update the input image **tmp** with the current image displayed, so the
|
||||
subsequent operations are performed on it.
|
||||
@code{.cpp}
|
||||
tmp = dst;
|
||||
@endcode
|
||||
Results
|
||||
-------
|
||||
|
||||
@@ -226,5 +173,3 @@ Results
|
||||
is now:
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
@@ -16,67 +16,8 @@ Code
|
||||
|
||||
This tutorial code's is shown lines below. You can also download it from
|
||||
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ShapeDescriptors/hull_demo.cpp)
|
||||
@code{.cpp}
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
Mat src; Mat src_gray;
|
||||
int thresh = 100;
|
||||
int max_thresh = 255;
|
||||
RNG rng(12345);
|
||||
|
||||
/// Function header
|
||||
void thresh_callback(int, void* );
|
||||
@endcode
|
||||
/\* @function main */ int main( int argc, char*\* argv )
|
||||
|
||||
|
||||
{
|
||||
/// Load source image and convert it to gray src = imread( argv[1], 1 );
|
||||
|
||||
/// Convert image to gray and blur it cvtColor( src, src_gray, COLOR_BGR2GRAY ); blur(
|
||||
src_gray, src_gray, Size(3,3) );
|
||||
|
||||
/// Create Window char\* source_window = "Source"; namedWindow( source_window,
|
||||
WINDOW_AUTOSIZE ); imshow( source_window, src );
|
||||
|
||||
createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
|
||||
thresh_callback( 0, 0 );
|
||||
|
||||
waitKey(0); return(0);
|
||||
|
||||
}
|
||||
|
||||
/\* @function thresh_callback */ void thresh_callback(int, void* ) { Mat src_copy =
|
||||
src.clone(); Mat threshold_output; vector\<vector\<Point\> \> contours; vector\<Vec4i\>
|
||||
hierarchy;
|
||||
|
||||
/// Detect edges using Threshold threshold( src_gray, threshold_output, thresh, 255,
|
||||
THRESH_BINARY );
|
||||
|
||||
/// Find contours findContours( threshold_output, contours, hierarchy, RETR_TREE,
|
||||
CHAIN_APPROX_SIMPLE, Point(0, 0) );
|
||||
|
||||
/// Find the convex hull object for each contour vector\<vector\<Point\> \>hull(
|
||||
contours.size() ); for( int i = 0; i \< contours.size(); i++ ) { convexHull(
|
||||
Mat(contours[i]), hull[i], false ); }
|
||||
|
||||
/// Draw contours + hull results Mat drawing = Mat::zeros( threshold_output.size(),
|
||||
CV_8UC3 ); for( int i = 0; i\< contours.size(); i++ ) { Scalar color = Scalar(
|
||||
rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) ); drawContours( drawing,
|
||||
contours, i, color, 1, 8, vector\<Vec4i\>(), 0, Point() ); drawContours( drawing, hull, i,
|
||||
color, 1, 8, vector\<Vec4i\>(), 0, Point() ); }
|
||||
|
||||
/// Show in a window namedWindow( "Hull demo", WINDOW_AUTOSIZE ); imshow( "Hull demo",
|
||||
drawing );
|
||||
|
||||
}
|
||||
@includelineno samples/cpp/tutorial_code/ShapeDescriptors/hull_demo.cpp
|
||||
|
||||
Explanation
|
||||
-----------
|
||||
@@ -84,10 +25,7 @@ Explanation
|
||||
Result
|
||||
------
|
||||
|
||||
1. Here it is:
|
||||
|
||||
----------- -----------
|
||||
|Hull_0| |Hull_1|
|
||||
----------- -----------
|
||||
|
||||
Here it is:
|
||||
|
||||

|
||||

|
||||
|
||||
Reference in New Issue
Block a user