connectedcomponents: use opencv integral types, add to docs, fix up things for a...
authorJason Newton <jason@utopiacompression.com>
Mon, 5 Nov 2012 20:02:53 +0000 (12:02 -0800)
committerJason Newton <jason@utopiacompression.com>
Mon, 5 Nov 2012 20:02:53 +0000 (12:02 -0800)
modules/imgproc/doc/structural_analysis_and_shape_descriptors.rst
modules/imgproc/include/opencv2/imgproc/imgproc.hpp
modules/imgproc/src/connectedcomponents.cpp
modules/python/src2/cv2.cpp

index 55cea58..ad5c22c 100644 (file)
@@ -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<ConnectedComponentStats> &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
 ----------------
index 0cb761b..3c9d787 100644 (file)
@@ -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<ConnectedComponentStats> &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<ConnectedComponentStats> &statsv, int connectivity = 8);
 
 
 //! mode of the contour retrieval algorithm
index 50a1ca1..8e75ce7 100644 (file)
 #include "precomp.hpp"
 #include <vector>
 
+//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<ConnectedComponentStats> &statsv, int connectivity){
+uint64_t connectedComponentsWithStats(Mat &L, const Mat &I, std::vector<ConnectedComponentStats> &statsv, int connectivity){
     int lDepth = L.depth();
     if(lDepth == CV_8U){
         connectedcomponents::CCStatsOp<uint8_t> sop(statsv); return connectedComponents_sub1(L, I, connectivity, sop);
index 28cf00e..5dbbbb4 100644 (file)
@@ -123,6 +123,7 @@ typedef Ptr<FeatureDetector> Ptr_FeatureDetector;
 typedef Ptr<DescriptorExtractor> Ptr_DescriptorExtractor;
 typedef Ptr<Feature2D> Ptr_Feature2D;
 typedef Ptr<DescriptorMatcher> Ptr_DescriptorMatcher;
+typedef vector<ConnectedComponentStats> vector_ConnectedComponentStats;
 
 typedef SimpleBlobDetector::Params SimpleBlobDetector_Params;
 
@@ -410,7 +411,7 @@ static bool pyopencv_to(PyObject* obj, bool& value, const char* name = "<unknown
 
 static PyObject* pyopencv_from(size_t value)
 {
-    return PyLong_FromUnsignedLong((unsigned long)value);
+    return PyLong_FromSize_t(value);
 }
 
 static bool pyopencv_to(PyObject* obj, size_t& value, const char* name = "<unknown>")
@@ -497,9 +498,16 @@ static bool pyopencv_to(PyObject* obj, float& value, const char* name = "<unknow
 
 static PyObject* pyopencv_from(int64 value)
 {
-    return PyFloat_FromDouble((double)value);
+    return PyLong_FromLongLong(value);
 }
 
+#if !defined(__LP64__)
+static PyObject* pyopencv_from(uint64 value)
+{
+    return PyLong_FromUnsignedLongLong(value);
+}
+#endif
+
 static PyObject* pyopencv_from(const string& value)
 {
     return PyString_FromString(value.empty() ? "" : value.c_str());