Layout-Parser / layout-parser

A Unified Toolkit for Deep Learning Based Document Image Analysis
https://layout-parser.github.io/
Apache License 2.0
4.78k stars 459 forks source link

Intersect operation #34

Open lkluo opened 3 years ago

lkluo commented 3 years ago

The intersect operation always returns results, however, it is not true when two blocks are not overlapped.

lolipopshock commented 3 years ago

Thanks for reporting. Yes, that's a bug - should be fix in the next update.

lkluo commented 3 years ago

@lolipopshock By the way, I think the is_in operation is too strict. Would it be better to allow some flexibility? This is especially useful for some blocks which are either labelled as text or title.

lolipopshock commented 3 years ago

@lolipopshock By the way, I think the is_in operation is too strict. Would it be better to allow some flexibility? This is especially useful for some blocks which are either labelled as text or title.

Does the soft_margin and center parameters in the is_in function help? (See in the documentation)

lkluo commented 3 years ago

Thanks @lolipopshock for your prompt reply. What you mention could be an instant solution. On the other hand, correct me if I am wrong, it requires to provide absolute value for soft_margin which is case-by-case. Would it better to use block intersection as a criterion? If block A is in block B, both of them shall intersect with each other. In other words, intersect_area/block_a_area shall be close to 1.

lolipopshock commented 3 years ago

What you mention could be an instant solution. On the other hand, correct me if I am wrong, it requires to provide absolute value for soft_margin which is case-by-case. Would it better to use block intersection as a criterion? If block A is in block B, both of them shall intersect with each other. In other words, intersect_area/block_a_area shall be close to 1.

Ahh if you set center=True, then if block A's center is within block B's boundary, then block_A.is_in(block_B, center=True) ==True. For example,

import layoutparser as lp
import numpy as np

block_A = lp.Rectangle(50, 50, 150, 150)
block_B = lp.Rectangle(40, 60, 160, 130)

lp.draw_box(np.ones((200,200,3), dtype='uint8')*255, [block_A, block_B])

assert block_A.is_in(block_B) == False
assert block_A.is_in(block_B, center=True) == True

image

lkluo commented 3 years ago

Thanks, it works.

lkluo commented 3 years ago

Sorry, there is still exception even though setting center=True. In your example, when block_A contains block_B, block_A.is_in(block_B, center=True) is true. Then is_in is a little bit confused.

lkluo commented 3 years ago

You may be also interested in this example: block_A = lp.Rectangle(40, 50, 150, 150) block_B = lp.Rectangle(40, 120, 140, 151)

lolipopshock commented 3 years ago

You may be also interested in this example: block_A = lp.Rectangle(40, 50, 150, 150) block_B = lp.Rectangle(40, 120, 140, 151)

Good point. In this case, you might want to use soft_margin:

block_A = lp.Rectangle(40, 50, 150, 150) 
block_B = lp.Rectangle(40, 120, 140, 151)

assert block_B.is_in(block_A, soft_margin={'bottom':5})
assert block_B.is_in(block_A, soft_margin={'bottom':1}) # this also works