You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect upper bound for the number of components in ConnectedComponents algorithms #7607
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>constintROWS = 1;
constintCOLS = 500;
int sum = 0;
voidtestConnectedComponents() {
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);
}
intmain() {
int t = 1000000;
for (int i = 0; i < t; ++i) {
testConnectedComponents();
}
std::cerr << "Sum: " << sum << std::endl;
return0;
}
Solution (Updated)
After @kinchungwong comment, issue changed. Upper bounds for both LabelingGrana and LabelingWu was incorrect.
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:
Whereas for 4-connectivity, the upper bound should be ((width * height + 1) / 2), precisely floor((width * height + 1) / 2), using a pattern like this:
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.
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).
changed the title [-]LabelingGrana: Incorrect upper bound for the number of components [/-][+]Incorrect upper bound for the number of components[/+]on Nov 5, 2016
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
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.
Activity
kinchungwong commentedon Nov 5, 2016
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:Whereas for 4-connectivity, the upper bound should be ((width * height + 1) / 2), precisely
floor((width * height + 1) / 2), using a pattern like this: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.
sokian commentedon Nov 5, 2016
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).
[-]LabelingGrana: Incorrect upper bound for the number of components [/-][+]Incorrect upper bound for the number of components[/+][-]Incorrect upper bound for the number of components[/-][+]Incorrect upper bound for the number of components in ConnectedComponents algorithms[/+]sokian commentedon Nov 5, 2016
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