# HG changeset patch
# User Avinoam
# Date 1479937298 -7200
#      Wed Nov 23 23:41:38 2016 +0200
# Node ID 3bf37f2fdb197ae1dc7eab65aba0529da102582c
# Parent  6cd0d6b5cc1e788c9f7d89cb9e0f2a2fe726868a
Fix a bug in computing majoraxislength/minoraxislength im regionprops (bug #49613)
* regionprops.m: 
   changing the value of c_idx during the loop 
   adding a BIST
   changing tolerance in some BISTs

diff -r 6cd0d6b5cc1e -r 3bf37f2fdb19 inst/regionprops.m
--- a/inst/regionprops.m	Sun Nov 13 12:23:11 2016 +0000
+++ b/inst/regionprops.m	Wed Nov 23 23:41:38 2016 +0200
@@ -868,7 +868,7 @@
     sel = c_idx:(c_idx + area(idx) -1);
     X = pixellist(sel, 2);
     Y = pixellist(sel, 1);
-
+    c_idx += area(idx);
     ## calculate (centralised) second moment of region with pixels [X, Y]
 
     ## This is equivalent to "cov ([X(:) Y(:)], 1)" but will work as
@@ -969,16 +969,16 @@
 
 %!assert (regionprops (bw2d, "Centroid"),
 %!        struct ("Centroid", {get_2d_centroid_for([6 7 8 12 16 21 22 27])
-%!                             get_2d_centroid_for([15 19 20 24 29 30])}))
+%!                             get_2d_centroid_for([15 19 20 24 29 30])}), 1.e-15)
 
 %!test
 %! props = struct ("Area", {8; 6},
 %!                 "Centroid", {get_2d_centroid_for([6 7 8 12 16 21 22 27])
 %!                              get_2d_centroid_for([15 19 20 24 29 30])},
 %!                 "BoundingBox", {[1.5 0.5 5 3]; [2.5 3.5 4 2]});
-%! assert (regionprops (bw2d, "basic"), props)
-%! assert (regionprops (bwconncomp (bw2d, 8), "basic"), props)
-%! assert (regionprops (bwlabeln (bw2d, 8), "basic"), props)
+%! assert (regionprops (bw2d, "basic"), props, 1.e-15)
+%! assert (regionprops (bwconncomp (bw2d, 8), "basic"), props, 1.e-15)
+%! assert (regionprops (bwlabeln (bw2d, 8), "basic"), props, 1.e-15)
 
 %!test
 %! props = struct ("Area", {4; 6; 4},
@@ -986,8 +986,8 @@
 %!                              get_2d_centroid_for([15 19 20 24 29 30])
 %!                              get_2d_centroid_for([16 21 22 27])},
 %!                 "BoundingBox", {[1.5 0.5 2 3]; [2.5 3.5 4 2]; [3.5 0.5 3 2]});
-%! assert (regionprops (bwconncomp (bw2d, 4), "basic"), props)
-%! assert (regionprops (bwlabeln (bw2d, 4), "basic"), props)
+%! assert (regionprops (bwconncomp (bw2d, 4), "basic"), props, 1.e-15)
+%! assert (regionprops (bwlabeln (bw2d, 4), "basic"), props, 1.e-15)
 
 ## This it is treated as labeled image with a single discontiguous region.
 %!assert (regionprops (double (bw2d), "basic"),
@@ -995,8 +995,8 @@
 %!                "Centroid", get_2d_centroid_for (find (bw2d)),
 %!                "BoundingBox", [1.5 0.5 5 5]), eps*1000)
 
-%!assert (regionprops ([0 0 1], "Centroid").Centroid, [3 1])
-%!assert (regionprops ([0 0 1; 0 0 0], "Centroid").Centroid, [3 1])
+%!assert (regionprops ([0 0 1], "Centroid").Centroid, [3 1], 1.e-15)
+%!assert (regionprops ([0 0 1; 0 0 0], "Centroid").Centroid, [3 1], 1.e-15)
 
 ## bug #39701
 %!assert (regionprops ([0 1 1], "Centroid").Centroid, [2.5 1])
@@ -1028,7 +1028,7 @@
 %!                             {sum([2 1; 2 2; 3 2; 2 3; 4 1; 5 1; 5 2; 6 2]
 %!                              .* ([4; 0; 4; 5; 7; 5; 3; 7] / 35))
 %!                              sum([3 5; 4 4; 4 5; 5 4; 6 4; 6 5]
-%!                                  .* ([7; 5; 2; 8; 6; 5] / 33))}))
+%!                                  .* ([7; 5; 2; 8; 6; 5] / 33))}), 1.e-15)
 
 %!test
 %! img = zeros (3, 9);
@@ -1438,6 +1438,14 @@
 %! assert (size (props), [0 1])
 %! assert (sort (all_props), sort (fieldnames (props)))
 
+## test for MajorAxisLength','MinorAxisLength' (bug #49613)
+%!test
+%! BW = [0 1 1 1 1; 0 1 1 0 0; 0 0 0 0 0; 0 0 0 1 0; 0 1 1 1 0];
+%! L = bwlabel(BW);
+%! props = regionprops(L,'MajorAxisLength','MinorAxisLength');
+%! assert ([props.MajorAxisLength] ,[4.51354115 3.65148372], 1.e-8);
+%! assert ([props.MinorAxisLength], [2.01801654 1.82574186], 1.e-8);
+
 ## Test warnings about invalid props for nd images and missing grayscale
 %!warning <ignoring perimeter, extrema properties for non 2 dimensional image>
 %!        regionprops (rand (5, 5, 5) > 0.5, {"perimeter", "extrema"});
@@ -1452,6 +1460,7 @@
 %!error <L must be non-negative integers only>
 %!      regionprops (int8 ([0 -1 3 4; 0 -1 3 4]))
 
+
 %!error <not yet implemented> regionprops (rand (5, 5) > 0.5, "ConvexArea")
 %!error <not yet implemented> regionprops (rand (5, 5) > 0.5, "ConvexHull")
 %!error <not yet implemented> regionprops (rand (5, 5) > 0.5, "ConvexImage")
