layout-parser icon indicating copy to clipboard operation
layout-parser copied to clipboard

Intersect operation

Open lkluo opened this issue 4 years ago • 9 comments

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

lkluo avatar Apr 21 '21 09:04 lkluo

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

lolipopshock avatar Apr 22 '21 05:04 lolipopshock

@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.

lkluo avatar Apr 22 '21 05:04 lkluo

@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)

lolipopshock avatar Apr 23 '21 03:04 lolipopshock

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.

lkluo avatar Apr 23 '21 03:04 lkluo

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

lolipopshock avatar Apr 23 '21 03:04 lolipopshock

Thanks, it works.

lkluo avatar Apr 23 '21 03:04 lkluo

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 avatar Apr 23 '21 04:04 lkluo

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

lkluo avatar Apr 23 '21 06:04 lkluo

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

lolipopshock avatar Apr 23 '21 15:04 lolipopshock