From 85880397c411b58b665a877b2eef5b1dd0cb1863 Mon Sep 17 00:00:00 2001 From: Jason Newton Date: Mon, 5 Nov 2012 12:02:53 -0800 Subject: [PATCH] connectedcomponents: use opencv integral types, add to docs, fix up things for a python export --- .../structural_analysis_and_shape_descriptors.rst | 42 ++++++++++++++++++++++ .../imgproc/include/opencv2/imgproc/imgproc.hpp | 24 ++++++------- modules/imgproc/src/connectedcomponents.cpp | 12 ++++++- modules/python/src2/cv2.cpp | 12 +++++-- 4 files changed, 75 insertions(+), 15 deletions(-) diff --git a/modules/imgproc/doc/structural_analysis_and_shape_descriptors.rst b/modules/imgproc/doc/structural_analysis_and_shape_descriptors.rst index 55cea58..ad5c22c 100644 --- a/modules/imgproc/doc/structural_analysis_and_shape_descriptors.rst +++ b/modules/imgproc/doc/structural_analysis_and_shape_descriptors.rst @@ -118,6 +118,48 @@ These values are proved to be invariants to the image scale, rotation, and refle .. seealso:: :ocv:func:`matchShapes` +connectedComponents +----------- +computes the connected components labeled image of boolean image I with 4 or 8 way connectivity - returns N, the total +number of labels [0, N-1] where 0 represents the background label. L's value type determines the label type, an important +consideration based on the total number of labels or alternatively the total number of pixels. + +.. ocv:function:: uint64 connectedComponents(Mat &L, const Mat &I, int connectivity = 8) + +.. ocv:function:: uint64 connectedComponentsWithStats(Mat &L, const Mat &I, std::vector &statsv, int connectivity = 8) + + :param L: destitination Labeled image + + :param I: the image to be labeled + + :param connectivity: 8 or 4 for 8-way or 4-way connectivity respectively + + :param statsv: statistics for each label, including the background label + +Statistics information such as bounding box, area, and centroid is exported via the ``ConnectComponentStats`` structure defined as: :: + + class CV_EXPORTS ConnectedComponentStats + { + public: + //! lower left corner column + int lower_x; + //! lower left corner row + int lower_y; + //! upper right corner column + int upper_x; + //! upper right corner row + int upper_y; + //! centroid column + double centroid_x; + //! centroid row + double centroid_y; + //! sum of all columns where the image was non-zero + uint64 integral_x; + //! sum of all rows where the image was non-zero + uint64 integral_y; + //! count of all non-zero pixels + unsigned int area; + }; findContours ---------------- diff --git a/modules/imgproc/include/opencv2/imgproc/imgproc.hpp b/modules/imgproc/include/opencv2/imgproc/imgproc.hpp index 0cb761b..3c9d787 100644 --- a/modules/imgproc/include/opencv2/imgproc/imgproc.hpp +++ b/modules/imgproc/include/opencv2/imgproc/imgproc.hpp @@ -1091,24 +1091,24 @@ enum { TM_SQDIFF=0, TM_SQDIFF_NORMED=1, TM_CCORR=2, TM_CCORR_NORMED=3, TM_CCOEFF CV_EXPORTS_W void matchTemplate( InputArray image, InputArray templ, OutputArray result, int method ); - struct CV_EXPORTS ConnectedComponentStats { - int32_t lower_x; - int32_t lower_y; - int32_t upper_x; - int32_t upper_y; - double centroid_x; - double centroid_y; - uint64_t integral_x; - uint64_t integral_y; - uint32_t area; + int lower_x;//!< lower left corner column + int lower_y;//!< lower left corner row + int upper_x;//!< upper right corner column + int upper_y;//!< upper right corner row + double centroid_x;//!< centroid column + double centroid_y;//!< centroid row + uint64 integral_x;//!< sum of all columns where the image was non-zero + uint64 integral_y;//!< sum of all rows where the image was non-zero + unsigned int area;//!< count of all non-zero pixels }; + //! computes the connected components labeled image of boolean image I with 4 or 8 way connectivity - returns N, the total //number of labels [0, N-1] where 0 represents the background label. L's value type determines the label type, an important //consideration based on the total number of labels or alternatively the total number of pixels. -CV_EXPORTS_W uint64_t connectedComponents(Mat &L, const Mat &I, int connectivity = 8); -CV_EXPORTS_W uint64_t connectedComponents(Mat &L, const Mat &I, std::vector &statsv, int connectivity = 8); +CV_EXPORTS_W uint64 connectedComponents(CV_OUT Mat &L, const Mat &I, int connectivity = 8); +CV_EXPORTS_W uint64 connectedComponentsWithStats(CV_OUT Mat &L, const Mat &I, CV_OUT std::vector &statsv, int connectivity = 8); //! mode of the contour retrieval algorithm diff --git a/modules/imgproc/src/connectedcomponents.cpp b/modules/imgproc/src/connectedcomponents.cpp index 50a1ca1..8e75ce7 100644 --- a/modules/imgproc/src/connectedcomponents.cpp +++ b/modules/imgproc/src/connectedcomponents.cpp @@ -43,6 +43,16 @@ #include "precomp.hpp" #include +//It's 2012 and we still let compilers get by without defining standard integer types... +typedef schar int8_t; +typedef uchar uint8_t; +typedef short int16_t; +typedef unsigned short uint16_t; +typedef int int32_t; +typedef unsigned int uint32_t; +typedef int64 int64_t; +typedef uint64 uint64_t; + namespace cv{ namespace connectedcomponents{ @@ -463,7 +473,7 @@ uint64_t connectedComponents(Mat &L, const Mat &I, int connectivity){ } } -uint64_t connectedComponents(Mat &L, const Mat &I, std::vector &statsv, int connectivity){ +uint64_t connectedComponentsWithStats(Mat &L, const Mat &I, std::vector &statsv, int connectivity){ int lDepth = L.depth(); if(lDepth == CV_8U){ connectedcomponents::CCStatsOp sop(statsv); return connectedComponents_sub1(L, I, connectivity, sop); diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 28cf00e..5dbbbb4 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -123,6 +123,7 @@ typedef Ptr Ptr_FeatureDetector; typedef Ptr Ptr_DescriptorExtractor; typedef Ptr Ptr_Feature2D; typedef Ptr Ptr_DescriptorMatcher; +typedef vector vector_ConnectedComponentStats; typedef SimpleBlobDetector::Params SimpleBlobDetector_Params; @@ -410,7 +411,7 @@ static bool pyopencv_to(PyObject* obj, bool& value, const char* name = "