Giter Club home page Giter Club logo

Comments (11)

vrabaud avatar vrabaud commented on July 19, 2024 1

@mshabunin , it is still not working exactly the same for me. Here is a new rows=12,cols=16 example:

0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

There are 7 old contours, 8 new ones.

from opencv.

vrabaud avatar vrabaud commented on July 19, 2024 1

I think the wrong behavior should not be forward ported, let's keep the bugfix for 4.10.
BTW, as the old contours.cpp will be removed at some point, how about renaming contours.cpp to contours_old.cppright now and havecontours.cpp` be the new code ? It will make things easier to kill old code for 4.11.

from opencv.

mshabunin avatar mshabunin commented on July 19, 2024

This test passes for me on Linux, GCC 12, current 4.x branch:

[ RUN      ] Imgproc_FindContours.regression
[16, 0][15, 3][14, 6][15, 10][13, 12][10, 12][8, 9][5, 7][2, 4][0, 4][0, 31][31, 31][31, 0]

[16, 0][15, 3][14, 6][15, 10][13, 12][10, 12][8, 9][5, 7][2, 4][0, 4][0, 31][31, 31][31, 0]
[       OK ] Imgproc_FindContours.regression (0 ms)

What is your platform?

from opencv.

vrabaud avatar vrabaud commented on July 19, 2024

Thx @mshabunin for the quick response. Did you patch

findContours(_image, _contours, noArray(), mode, method, offset);
like I mentioned ? (so that it calls findContours_legacy). I get:

[16, 0][15, 3][14, 6][15, 10][13, 12][10, 12][8, 9][5, 7][2, 4][0, 4][0, 31][31, 31][31, 0]

[16, 0][16, 2][15, 5][14, 6][15, 10][13, 12][10, 12][8, 9][5, 7][2, 4][0, 4][0, 31][31, 31][31, 0]

from opencv.

mshabunin avatar mshabunin commented on July 19, 2024

No I didn't 😕

With this patch I've reproduced this issue. You're right, findContours_legacy should call findContours_legacy. I'll take closer look.

from opencv.

vrabaud avatar vrabaud commented on July 19, 2024

I fixed the overload bug in #25668.

from opencv.

mshabunin avatar mshabunin commented on July 19, 2024

@vrabaud , could you please take a look at the fix in #25672?

from opencv.

mshabunin avatar mshabunin commented on July 19, 2024

I took another look at the code and came to conclusion that there is an issue in the original implementation which has been fixed in refactored function. I'm not sure if we should try to port this behavior to new function or leave it as-is (revert #25672), i.e. different. @vrabaud , @asmorkalov , what do you think?

Details

Issue lies in the last, 4-th pass of the algorithm (page 685). Basically we check all groups of consecutive elements (groups) and remove:

  • first or second element (for groups with 2 elements)
  • all elements between first and last (for groups with > 2 elements)

I reverted changes from my previous PR and took original example from this ticket (32x32), also added some logging (4.x...mshabunin:fix-approx-2)

Results

Original implementation returns following result (element indexes):

Result: 0, 2, 5, 6, 10, 12, 15, 18, 21, 25, 27, 54, 85, 116, 

Note consecutive elements 5, 6 being left after last pass - it should not happen.
old_version
Refactored:

Result: 0, 2, 6, 10, 12, 15, 18, 21, 25, 27, 54, 85, 116, 

new_version

Explanation

Log from the original implementation:

first = 0, prev = 2, curr = 3, next = 5
** remove (=2i) 3
first = 3, prev = 3, curr = 5, next = 6
first = 3, prev = 5, curr = 6, next = 10
** remove (=2e) 5

Elements are stored in a linked list, removed element should not be in the list (but are kept in the array which stores list elements). prev and curr are two elements being checked (for groups of 2), first is an element before current group, next is element after group. Here we first process group of 2 elements 2, 3 and remove 3 by changing pointer in linked list 2 -> 5. Also we change first to 3 which is wrong. Then we meet group of 2 elements 5, 6 and need to remove 5 - in order to do it we set pointer 3 -> 6, but 3 is not in the list anymore, so it does not take effect. Thus element 5 stays in the list.

Refactored implementation does not use linked list, but sets flags in the array, so it does not have this issue.

Original code:

do
{
if( current->next == 0 || current->next - current != 1 )
{
if( count >= 2 )
{
if( count == 2 )
{
int s1 = prev_current->s;
int s2 = current->s;
if( s1 > s2 || (s1 == s2 && prev_current->k <= current->k) )
/* remove second */
prev_current->next = current->next;
else
/* remove first */
first->next = current;
}
else
first->next->next = current;
}
first = current;
count = 1;
}
else
count++;
prev_current = current;
current = current->next;
}
while( current != 0 );

from opencv.

mshabunin avatar mshabunin commented on July 19, 2024

I believe we do not plan to remove old implementation in 4.11, only in 5.x. Do you think we should drop it?

Originally I moved new function to a new file in order to simplify 4.x->5.x merges. Theoretically we can rename contours.cpp->contours_old.cpp and contours_new.cpp->contours.cpp and git should be able to handle it, but I'm not sure. Let's leave this decision to @asmorkalov.

from opencv.

vrabaud avatar vrabaud commented on July 19, 2024

Well, the functions are not public anymore so we might as well drop them at some point in 4.x no ?

from opencv.

mshabunin avatar mshabunin commented on July 19, 2024

Technically they are public, but somewhat hidden 🙂 https://github.com/opencv/opencv/blob/4.x/modules/imgproc/include/opencv2/imgproc/detail/legacy.hpp

We can remove #ifdef __OPENCV_BUILD guard to simplify access, I added it with assumption that those who really need old functions can define this macro and include legacy header.

objdump -TC ./lib/libopencv_imgproc.so | grep legacy
00000000001a9630 g    DF .text	0000000000000b65  Base        cv::findContours_legacy(cv::_InputArray const&, cv::_OutputArray const&, cv::_OutputArray const&, int, int, cv::Point_<int>)
00000000001e1930 g    DF .text	000000000000050e  Base        cv::EMD_legacy(cv::_InputArray const&, cv::_InputArray const&, int, cv::_InputArray const&, float*, cv::_OutputArray const&)
00000000001e1e40 g    DF .text	000000000000000c  Base        cv::wrapperEMD_legacy(cv::_InputArray const&, cv::_InputArray const&, int, cv::_InputArray const&, cv::Ptr<float>, cv::_OutputArray const&)
00000000001aa1a0 g    DF .text	00000000000000a3  Base        cv::findContours_legacy(cv::_InputArray const&, cv::_OutputArray const&, int, int, cv::Point_<int>)

from opencv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.