diff --git a/doc/tutorials/imgproc/hitOrMiss/hitOrMiss.markdown b/doc/tutorials/imgproc/hitOrMiss/hitOrMiss.markdown new file mode 100644 index 0000000000..085e7c2e00 --- /dev/null +++ b/doc/tutorials/imgproc/hitOrMiss/hitOrMiss.markdown @@ -0,0 +1,60 @@ +Hit-or-Miss {#tutorial_hitOrMiss} +================================= + +Goal +---- + +In this tutorial you will learn how to find a given configuration or pattern in a binary image by using the Hit-or-Miss transform (also known as Hit-and-Miss transform). +This transform is also the basis of more advanced morphological operations such as thinning or pruning. + +We will use the OpenCV function @ref cv::morphologyEx. + + + +Hit-or-Miss theory +------------------- + +Morphological operators process images based on their shape. These operators apply one or more *structuring elements* to an input image to obtain the output image. +The two basic morphological operations are the *erosion* and the *dilation*. The combination of these two operations generate advanced morphological transformations such as *opening*, *closing*, or *top-hat* transform. +To know more about these and other basic morphological operations refer to previous tutorials @ref tutorial_erosion_dilatation "here" and @ref tutorial_opening_closing_hats "here". + +The Hit-or-Miss transformation is useful to find patterns in binary images. In particular, it finds those pixels whose neighbourhood matches the shape of a first structuring element \f$B_1\f$ +while not matching the shape of a second structuring element \f$B_2\f$ at the same time. Mathematically, the operation applied to an image \f$A\f$ can be expressed as follows: +\f[ + A\circledast B = (A\ominus B_1) \cap (A^c\ominus B_2) +\f] + +Therefore, the hit-or-miss operation comprises three steps: + 1. Erode image \f$A\f$ with structuring element \f$B_1\f$. + 2. Erode the complement of image \f$A\f$ (\f$A^c\f$) with structuring element \f$B_2\f$. + 3. AND results from step 1 and step 2. + +The structuring elements \f$B_1\f$ and \f$B_2\f$ can be combined into a single element \f$B\f$. Let's see an example: +![Structuring elements (kernels). Left: kernel to 'hit'. Middle: kernel to 'miss'. Right: final combined kernel](images/hitmiss_kernels.png) + +In this case, we are looking for a pattern in which the central pixel belongs to the background while the north, south, east, and west pixels belong to the foreground. The rest of pixels in the neighbourhood can be of any kind, we don't care about them. Now, let's apply this kernel to an input image: + +![Input binary image](images/hitmiss_input.png) +![Output binary image](images/hitmiss_output.png) + +You can see that the pattern is found in just one location within the image. + + +Code +---- + +The code corresponding to the previous example is shown below. You can also download it from +[here](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/ImgProc/HitMiss.cpp) +@include samples/cpp/tutorial_code/ImgProc/HitMiss.cpp + +As you can see, it is as simple as using the function @ref cv::morphologyEx with the operation type @ref cv::MORPH_HITMISS and the chosen kernel. + +Other examples +-------------- + +Here you can find the output results of applying different kernels to the same input image used before: + +![Kernel and output result for finding top-right corners](images/hitmiss_example2.png) +![Kernel and output result for finding left end points](images/hitmiss_example3.png) + +Now try your own patterns! diff --git a/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_example2.png b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_example2.png new file mode 100644 index 0000000000..c4e0efba13 Binary files /dev/null and b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_example2.png differ diff --git a/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_example3.png b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_example3.png new file mode 100644 index 0000000000..dc97275422 Binary files /dev/null and b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_example3.png differ diff --git a/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_input.png b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_input.png new file mode 100644 index 0000000000..fec726f913 Binary files /dev/null and b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_input.png differ diff --git a/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_kernels.png b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_kernels.png new file mode 100644 index 0000000000..cdf5e7362e Binary files /dev/null and b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_kernels.png differ diff --git a/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_output.png b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_output.png new file mode 100644 index 0000000000..b7b81638ed Binary files /dev/null and b/doc/tutorials/imgproc/hitOrMiss/images/hitmiss_output.png differ diff --git a/doc/tutorials/imgproc/table_of_content_imgproc.markdown b/doc/tutorials/imgproc/table_of_content_imgproc.markdown index 486c6449c5..447f1970ba 100644 --- a/doc/tutorials/imgproc/table_of_content_imgproc.markdown +++ b/doc/tutorials/imgproc/table_of_content_imgproc.markdown @@ -27,6 +27,14 @@ In this section you will learn about the image processing (manipulation) functio Here we investigate different morphology operators +- @subpage tutorial_hitOrMiss + + *Compatibility:* \> OpenCV 2.4 + + *Author:* Lorena García + + Learn how to find patterns in binary images using the Hit-or-Miss operation + - @subpage tutorial_moprh_lines_detection *Compatibility:* \> OpenCV 2.0 diff --git a/doc/tutorials/introduction/windows_install/windows_install.markdown b/doc/tutorials/introduction/windows_install/windows_install.markdown index 179f430e23..e4e969f432 100644 --- a/doc/tutorials/introduction/windows_install/windows_install.markdown +++ b/doc/tutorials/introduction/windows_install/windows_install.markdown @@ -67,14 +67,14 @@ of them, you need to download and install them on your system. - [Numpy](http://numpy.scipy.org/) is a scientific computing package for Python. Required for the *Python interface*. - [Intel Threading Building Blocks (*TBB*)](http://threadingbuildingblocks.org/file.php?fid=77) is used inside OpenCV for parallel code snippets. Using this will make sure that the OpenCV library will take advantage of all the cores - you have in your systems CPU. + you have in your system's CPU. - [Intel Integrated Performance Primitives (*IPP*)](http://software.intel.com/en-us/articles/intel-ipp/) may be used to improve the performance of color conversion, Haar training and DFT functions of the OpenCV library. Watch out, since this isn't a free service. - [Intel IPP Asynchronous C/C++](http://software.intel.com/en-us/intel-ipp-preview) is currently focused delivering Intel Graphics support for advanced image processing and computer vision functions. - OpenCV offers a somewhat fancier and more useful graphical user interface, than the default one - by using the [Qt framework](http://qt.nokia.com/downloads). For a quick overview of what this has to offer look into the + by using the [Qt framework](http://qt.nokia.com/downloads). For a quick overview of what this has to offer, look into the documentations *highgui* module, under the *Qt New Functions* section. Version 4.6 or later of the framework is required. - [Eigen](http://eigen.tuxfamily.org/index.php?title=Main_Page#Download) is a C++ template library for linear algebra. @@ -83,7 +83,7 @@ of them, you need to download and install them on your system. more of our algorithms to work on the GPUs is a constant effort of the OpenCV team. - [OpenEXR](http://www.openexr.com/downloads.html) source files are required for the library to work with this high dynamic range (HDR) image file format. -- The OpenNI Framework contains a set of open source APIs that provide support for natural interaction with devices via methods such as voice command recognition, hand gestures and body +- The OpenNI Framework contains a set of open source APIs that provide support for natural interaction with devices via methods such as voice command recognition, hand gestures, and body motion tracking. Prebuilt binaries can be found [here](http://structure.io/openni). The source code of [OpenNI](https://github.com/OpenNI/OpenNI) and [OpenNI2](https://github.com/OpenNI/OpenNI2) are also available on Github. - [Miktex]( http://miktex.org/2.9/setup) is the best [TEX](https://secure.wikimedia.org/wikipedia/en/wiki/TeX) implementation on the Windows OS. It is required to build the *OpenCV documentation*. @@ -203,7 +203,7 @@ libraries). If you do not need the support for some of these you can just freely @code{.bash} nmake @endcode - After this set the Qt enviroment variables using the following command on Windows 7: + After this set the Qt environment variables using the following command on Windows 7: @code{.bash} setx -m QTDIR D:/OpenCV/dep/qt/qt-everywhere-opensource-src-4.7.3 @endcode @@ -266,8 +266,8 @@ libraries). If you do not need the support for some of these you can just freely fully functional on your computer. - *BUILD_PACKAGE* -\> Prior to version 2.3 with this you could build a project that will build an OpenCV installer. With this you can easily install your OpenCV flavor on other - systems. For the latest source files of OpenCV it generates a new project that simply - creates zip archive with OpenCV sources. + systems. For the latest source files of OpenCV, it generates a new project that simply + creates a zip archive with OpenCV sources. - *BUILD_SHARED_LIBS* -\> With this you can control to build DLL files (when turned on) or static library files (\*.lib) otherwise. - *BUILD_TESTS* -\> Each module of OpenCV has a test project assigned to it. Building these @@ -291,7 +291,7 @@ libraries). If you do not need the support for some of these you can just freely ![](images/OpenCVBuildResultWindows.jpg) - For the documentation you need to explicitly issue the build commands on the *doc* project for + For the documentation, you need to explicitly issue the build commands on the *doc* project for the PDF files and on the *doc_html* for the HTML ones. Each of these will call *Sphinx* to do all the hard work. You can find the generated documentation inside the `Build/Doc/_html` for the HTML pages and within the `Build/Doc` the PDF manuals. @@ -299,7 +299,7 @@ libraries). If you do not need the support for some of these you can just freely ![](images/WindowsBuildDoc.png) To collect the header and the binary files, that you will use during your own projects, into a - separate directory (simillary to how the pre-built binaries ship) you need to explicitely build + separate directory (simillary to how the pre-built binaries ship) you need to explicitly build the *Install* project. ![](images/WindowsBuildInstall.png) @@ -321,10 +321,10 @@ libraries). If you do not need the support for some of these you can just freely caused mostly by old video card drivers. For testing the GPU (if built) run the *performance_gpu.exe* sample application. -Set the OpenCV enviroment variable and add it to the systems path {#tutorial_windows_install_path} +Set the OpenCV environment variable and add it to the systems path {#tutorial_windows_install_path} ================================================================= -First we set an enviroment variable to make easier our work. This will hold the build directory of +First we set an environment variable to make easier our work. This will hold the build directory of our OpenCV library that we use in our projects. Start up a command window and enter: @code setx -m OPENCV_DIR D:\OpenCV\Build\x86\vc10 (suggested for Visual Studio 2010 - 32 bit Windows) diff --git a/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.markdown b/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.markdown index 3be7f5bb87..2c9d1903db 100644 --- a/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.markdown +++ b/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.markdown @@ -11,7 +11,7 @@ header files plus binaries and you have set the environment variables as describ The OpenCV libraries, distributed by us, on the Microsoft Windows operating system are in a Dynamic Linked Libraries (*DLL*). These have the advantage that all the content of the -library are loaded only at runtime, on demand, and that countless programs may use the same library +library is loaded only at runtime, on demand, and that countless programs may use the same library file. This means that if you have ten applications using the OpenCV library, no need to have around a version for each one of them. Of course you need to have the *dll* of the OpenCV on all systems where you want to run your application. @@ -42,9 +42,9 @@ To build an application with OpenCV you need to do two things: extension libraries. The good part is that at runtime only the *DLL* is required. To pass on all this information to the Visual Studio IDE you can either do it globally (so all your -future projects will get these information) or locally (so only for you current project). The +future projects will get this information) or locally (so only for you current project). The advantage of the global one is that you only need to do it once; however, it may be undesirable to -clump all your projects all the time with all these information. In case of the global one how you +clump all your projects all the time with all this information. In case of the global one how you do it depends on the Microsoft Visual Studio you use. There is a **2008 and previous versions** and a **2010 way** of doing it. Inside the global section of this tutorial I'll show what the main differences are. @@ -53,7 +53,7 @@ The base item of a project in Visual Studio is a solution. A solution may contai Projects are the building blocks of an application. Every project will realize something and you will have a main project in which you can put together this project puzzle. In case of the many simple applications (like many of the tutorials will be) you do not need to break down the -application into modules. In these cases your main project will be the only existing one. Now go +application into modules. In these cases, your main project will be the only existing one. Now go create a new solution inside Visual studio by going through the File --\> New --\> Project menu selection. Choose *Win32 Console Application* as type. Enter its name and select the path where to create it. Then in the upcoming dialog make sure you create an empty project. @@ -91,7 +91,7 @@ projects with custom rules that I do not use it. Go the C++ groups General entry *"Additional Include Directories"* add the path to your OpenCV include. If you don't have *"C/C++"* group, you should add any .c/.cpp file to the project. @code{.bash} -\f$(OPENCV_DIR)\..\..\include +$(OPENCV_DIR)\..\..\include @endcode ![](images/PropertySheetOpenCVInclude.jpg) @@ -152,7 +152,7 @@ them. ![](images/PropertySheetOpenCVLibrariesRelease.jpg) -You can find your property sheets inside your projects directory. At this point it is a wise +You can find your property sheets inside your projects directory. At this point, it is a wise decision to back them up into some special directory, to always have them at hand in the future, whenever you create an OpenCV project. Note that for Visual Studio 2010 the file extension is *props*, while for 2008 this is *vsprops*. @@ -167,7 +167,7 @@ entry inside the Property Manager to easily add the OpenCV build rules. The *global* method ------------------- -In case you find to troublesome to add the property pages to each and every one of your projects you +In case you find it too troublesome to add the property pages to each and every one of your projects you can also add this rules to a *"global property page"*. However, this applies only to the additional include and library directories. The name of the libraries to use you still need to specify manually by using for instance: a Property page. @@ -202,10 +202,11 @@ current working directory is the projects directory, while otherwise it is the f application file currently is (so usually your build directory). Moreover, in case of starting from the *IDE* the console window will not close once finished. It will wait for a keystroke of yours. -This is important to remember when you code inside the code open and save commands. You're resources +This is important to remember when you code inside the code open and save commands. Your resources will be saved ( and queried for at opening!!!) relatively to your working directory. This is unless -you give a full, explicit path as parameter for the I/O functions. In the code above we open [this -OpenCV logo](https://github.com/opencv/opencv/tree/master/samples/data/opencv-logo.png). Before starting up the application make sure you place +you give a full, explicit path as a parameter for the I/O functions. In the code above we open [this +OpenCV logo](https://github.com/opencv/opencv/tree/master/samples/data/opencv-logo.png). Before starting up the application, +make sure you place the image file in your current working directory. Modify the image file name inside the code to try it out on other images too. Run it and voil á: @@ -214,8 +215,8 @@ it out on other images too. Run it and voil á: Command line arguments with Visual Studio ----------------------------------------- -Throughout some of our future tutorials you'll see that the programs main input method will be by -giving a runtime argument. To do this you can just start up a commmand windows (cmd + Enter in the +Throughout some of our future tutorials, you'll see that the programs main input method will be by +giving a runtime argument. To do this you can just start up a command windows (cmd + Enter in the start menu), navigate to your executable file and start it with an argument. So for example in case of my upper project this would look like: @code{.bash} @@ -232,5 +233,5 @@ cumbersome task. Luckily, in the Visual Studio there is a menu to automate all t ![](images/VisualStudioCommandLineArguments.jpg) Specify here the name of the inputs and while you start your application from the Visual Studio -enviroment you have automatic argument passing. In the next introductionary tutorial you'll see an +environment you have automatic argument passing. In the next introductory tutorial you'll see an in-depth explanation of the upper source code: @ref tutorial_display_image. diff --git a/modules/core/src/command_line_parser.cpp b/modules/core/src/command_line_parser.cpp index 542b231ff9..4fc62238b8 100644 --- a/modules/core/src/command_line_parser.cpp +++ b/modules/core/src/command_line_parser.cpp @@ -10,7 +10,7 @@ static const char* noneValue = ""; static String cat_string(const String& str) { int left = 0, right = (int)str.length(); - while( left <= right && str[left] == ' ' ) + while( left < right && str[left] == ' ' ) left++; while( right > left && str[right-1] == ' ' ) right--; diff --git a/modules/core/src/persistence.cpp b/modules/core/src/persistence.cpp index ccdb135ec5..bf16c2f398 100644 --- a/modules/core/src/persistence.cpp +++ b/modules/core/src/persistence.cpp @@ -4025,7 +4025,14 @@ static void icvJSONWriteReal( CvFileStorage* fs, const char* key, double value ) { char buf[128]; - icvJSONWrite( fs, key, icvDoubleToString( buf, value )); + size_t len = strlen( icvDoubleToString( buf, value ) ); + if( len > 0 && buf[len-1] == '.' ) + { + // append zero if string ends with decimal place to match JSON standard + buf[len] = '0'; + buf[len+1] = '\0'; + } + icvJSONWrite( fs, key, buf ); } @@ -4829,6 +4836,17 @@ cvWriteRawData( CvFileStorage* fs, const void* _data, int len, const char* dt ) } else { + if( elem_type == CV_32F || elem_type == CV_64F ) + { + size_t buf_len = strlen(ptr); + if( buf_len > 0 && ptr[buf_len-1] == '.' ) + { + // append zero if CV_32F or CV_64F string ends with decimal place to match JSON standard + // ptr will point to buf, so can write to buf given ptr is const + buf[buf_len] = '0'; + buf[buf_len+1] = '\0'; + } + } icvJSONWrite( fs, 0, ptr ); } } diff --git a/modules/imgproc/include/opencv2/imgproc.hpp b/modules/imgproc/include/opencv2/imgproc.hpp index a196d928f8..dff1c3d477 100644 --- a/modules/imgproc/include/opencv2/imgproc.hpp +++ b/modules/imgproc/include/opencv2/imgproc.hpp @@ -245,8 +245,8 @@ enum MorphTypes{ //!< \f[\texttt{dst} = \mathrm{tophat} ( \texttt{src} , \texttt{element} )= \texttt{src} - \mathrm{open} ( \texttt{src} , \texttt{element} )\f] MORPH_BLACKHAT = 6, //!< "black hat" //!< \f[\texttt{dst} = \mathrm{blackhat} ( \texttt{src} , \texttt{element} )= \mathrm{close} ( \texttt{src} , \texttt{element} )- \texttt{src}\f] - MORPH_HITMISS = 7 //!< "hit and miss" - //!< .- Only supported for CV_8UC1 binary images. Tutorial can be found in [this page](https://web.archive.org/web/20160316070407/http://opencv-code.com/tutorials/hit-or-miss-transform-in-opencv/) + MORPH_HITMISS = 7 //!< "hit or miss" + //!< .- Only supported for CV_8UC1 binary images. A tutorial can be found in the documentation }; //! shape of the structuring element diff --git a/modules/imgproc/src/drawing.cpp b/modules/imgproc/src/drawing.cpp index 113f499d98..9099d7378a 100644 --- a/modules/imgproc/src/drawing.cpp +++ b/modules/imgproc/src/drawing.cpp @@ -1171,6 +1171,9 @@ FillConvexPoly( Mat& img, const Point2l* v, int npts, const void* color, int lin edge[0].di = 1; edge[1].di = npts - 1; + edge[0].x = edge[1].x = -XY_ONE; + edge[0].dx = edge[1].dx = 0; + ptr += img.step*y; do @@ -1213,6 +1216,9 @@ FillConvexPoly( Mat& img, const Point2l* v, int npts, const void* color, int lin } } + if (edges < 0) + break; + if (y >= 0) { int left = 0, right = 1; diff --git a/modules/ml/src/lr.cpp b/modules/ml/src/lr.cpp index 99692f3d16..57fcac7ef0 100644 --- a/modules/ml/src/lr.cpp +++ b/modules/ml/src/lr.cpp @@ -311,7 +311,7 @@ Mat LogisticRegressionImpl::calc_sigmoid(const Mat& data) const double LogisticRegressionImpl::compute_cost(const Mat& _data, const Mat& _labels, const Mat& _init_theta) { - int llambda = 0; + float llambda = 0; /*changed llambda from int to float to solve issue #7924*/ int m; int n; double cost = 0; diff --git a/samples/cpp/tutorial_code/ImgProc/HitMiss.cpp b/samples/cpp/tutorial_code/ImgProc/HitMiss.cpp new file mode 100644 index 0000000000..0463aabe39 --- /dev/null +++ b/samples/cpp/tutorial_code/ImgProc/HitMiss.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +using namespace cv; + +int main(){ + Mat input_image = (Mat_(8, 8) << + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 255, 255, 255, 0, 0, 0, 255, + 0, 255, 255, 255, 0, 0, 0, 0, + 0, 255, 255, 255, 0, 255, 0, 0, + 0, 0, 255, 0, 0, 0, 0, 0, + 0, 0, 255, 0, 0, 255, 255, 0, + 0, 255, 0, 255, 0, 0, 255, 0, + 0, 255, 255, 255, 0, 0, 0, 0); + + Mat kernel = (Mat_(3, 3) << + 0, 1, 0, + 1, -1, 1, + 0, 1, 0); + + Mat output_image; + morphologyEx(input_image, output_image, MORPH_HITMISS, kernel); + + namedWindow("Original", CV_WINDOW_NORMAL); + imshow("Original", input_image); + namedWindow("Hit or Miss", CV_WINDOW_NORMAL); + imshow("Hit or Miss", output_image); + waitKey(0); + return 0; +} \ No newline at end of file