Skip to content

Incorrect upper bound for the number of components in ConnectedComponents algorithms #7607

Open

Description

@sokian
System information (version)
  • OpenCV => master
  • Operating System / Platform => OSX 10.12.1
  • Compiler => Apple LLVM version 8.0.0 (clang-800.0.42.1)
Detailed description

New implemented algorithm LablelingGrana has incorrect upper bound for the number of components. And now it's default algorithm for this common case of usage:

cv::connectedComponents(image, labels);

This cause an BAD_ACCESS issue.

Steps to reproduce

Here is an example code that may reproduce an issue.
Here number of components is ROWS * COLS / 3,
But in implementation it's ROWS * COLS / 4.

#include <iostream>
#include <opencv2/imgproc.hpp>

const int ROWS = 1;
const int COLS = 500;
int sum = 0;

void testConnectedComponents() {
 cv::Mat img(cv::Size(COLS, ROWS), CV_8U);
 for (int i = 0; i < COLS * ROWS; i += 3) {
     img.data[i] = 1;
 }
 cv::Mat lab;
 sum += cv::connectedComponents(img, lab);
}

int main() {
 int t = 1000000;
 for (int i = 0; i < t; ++i) {
     testConnectedComponents();
 }
 std::cerr << "Sum: " << sum << std::endl;
 return 0;
}
Solution (Updated)

After @kinchungwong comment, issue changed. Upper bounds for both LabelingGrana and LabelingWu was incorrect.

Correct formulas for both cases are:
4-connectivity: floor((rows * cols + 1) / 2) + 1
8-connectivity: floor((rows + 1) / 2) * floor((cols + 1) / 2) + 1

Activity

  1. kinchungwong commented on Nov 5, 2016

    @kinchungwong
    Contributor

    I personally think that, for 8-connectivity, the upper bound should be (width + 1) * (height + 1) / 4, more precisely floor((width + 1) / 2) * floor((height + 1) / 2), using a pattern like this:

    1 0 1 0 1 ...
    0 0 0 0 0 ...
    1 0 1 0 1 ...
    0 0 0 0 0 ...
    1 0 1 0 1 ...
    ...
    ...
    

    Whereas for 4-connectivity, the upper bound should be ((width * height + 1) / 2), precisely floor((width * height + 1) / 2), using a pattern like this:

    1 0 1 0 1 ...
    0 1 0 1 0 ...
    1 0 1 0 1 ...
    0 1 0 1 0 ...
    1 0 1 0 1 ...
    ...
    ...
    

    In the two examples above, I intentionally make pixel(0, 0) to be a foreground, as well as choosing an odd width and height, to highlight the edge case. (Edge case arises when either width or height is odd, and doubly so when both are odd.)

    Please kindly let me know if there is any disagreement or counter-example.

  2. sokian commented on Nov 5, 2016

    @sokian
    Author

    Hi, @kinchungwong. Yes you are right.
    I have already updated my PR, now it fixes Assert statement in LabelingGrana, so that it can be called only in 8-connectivity case, and upper bound now (width + 1) * (height + 1) / 4 + 1.
    (Plus one is for background).

  3. changed the title [-]LabelingGrana: Incorrect upper bound for the number of components [/-] [+]Incorrect upper bound for the number of components[/+] on Nov 5, 2016
  4. changed the title [-]Incorrect upper bound for the number of components[/-] [+]Incorrect upper bound for the number of components in ConnectedComponents algorithms[/+] on Nov 5, 2016
  5. sokian commented on Nov 5, 2016

    @sokian
    Author

    I have updated my PR, now it fixes upper bounds for both algorithms.

    Do I have to commit to other branches as it turned out to be an old problem?

    Also, I made simple test for it. And compile it and opencv with AddressSanitizer, to check that no other BAD_ACCESS problems exists in connectedComponents.

    Thank you, @kinchungwong.

    Code of my test attached (as txt file)
    connectivity_upperbound_test.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions