Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

square elongation for cropmodel #820

Open
bw4sz opened this issue Oct 22, 2024 · 5 comments
Open

square elongation for cropmodel #820

bw4sz opened this issue Oct 22, 2024 · 5 comments
Labels
API This tag is used for small improvements to the readability and usability of the python API. good first issue Good for newcomers Ideas for Machine Learning! These are machine learning ideas and papers that could be useful for DeepForest models. High level.

Comments

@bw4sz
Copy link
Collaborator

bw4sz commented Oct 22, 2024

From an unpublished paper, which I can later link.

Instead of cropping tightly around the object, we
convert the bounding boxes into square shapes by extending the shorter side to match the length of the longer side.
Another way to think of it is that we are doing padding
with natural pixels. Our box expansion approach maintains
the original aspect ratios of objects without introducing artifacts, reducing the likelihood of misidentification.

We could do this for the cropmodel workflow.

The relevant code is here:

xmin, ymin, xmax, ymax = box

@bw4sz bw4sz added good first issue Good for newcomers API This tag is used for small improvements to the readability and usability of the python API. Ideas for Machine Learning! These are machine learning ideas and papers that could be useful for DeepForest models. High level. labels Oct 22, 2024
@ethanwhite
Copy link
Member

This is a really cool idea!

@NaganathanM
Copy link

Hi! If my understanding is correct, you want to adjust the xmin, xmax or ymin, ymax depending on the longer side, to make the box square. Shall I take up and work on this if it's still open?

@bw4sz
Copy link
Collaborator Author

bw4sz commented Oct 23, 2024

For sure, let me know if you need help.

@NaganathanM
Copy link

NaganathanM commented Oct 29, 2024

Hello! Sorry I took long time. So, I've implemented a function that takes boxes - a list of coordinates and return the adjusted coordinates. Here, if length on x-axis is longer, then length on y-axis is adjusted, keeping the ymin the same. Similarly, xmin is kept the same and x-axis length is elongated if y-axis length is longer. Go through the code and give me your feedback:

def square_bounding_box(boxes):
    """
    The square_bounding_box function takes a list of x and y coordniates as arguments from the 'boxes' liat; calculates the longer side,
    and adjust the shorter side such that both lengths are equal to form a square, and return the coordniates
    boxes (list): A list of bounding box coordinates in the format [xmin, ymin, xmax, ymax]
    """
    if abs(boxes[1])-abs(boxes[0]) > abs(boxes[3])-abs(boxes[2]):
        boxes[3] = boxes[2] + abs(boxes[1])-abs(boxes[0])
    if abs(boxes[3])-abs(boxes[2]) > abs(boxes[1])-abs(boxes[0]):  
        boxes[1] = boxes[0] +  abs(boxes[3])-abs(boxes[2])         
        
    return boxes

#help(square_bounding_box)
if __name__ == "__main__":
    print(square_bounding_box([3,8,4,7]))
    print(square_bounding_box([6,9,2,9]))

@bw4sz
Copy link
Collaborator Author

bw4sz commented Oct 29, 2024

good start, cleaning up code readability

def square_bounding_box(boxes: list) -> list:
    """
    Adjusts the bounding box coordinates to form a square.

    Args:
        boxes (list): A list of bounding box coordinates in the format [xmin, ymin, xmax, ymax]

    Returns:
        list: Adjusted bounding box coordinates to form a square
    """
    xmin, ymin, xmax, ymax = boxes

    width = xmax - xmin
    height = ymax - ymin

    if width > height:
        # Adjust height to match width
        ymax = ymin + width
    elif height > width:
        # Adjust width to match height
        xmax = xmin + height

    return [xmin, ymin, xmax, ymax]

if __name__ == "__main__":
    print(square_bounding_box([3, 8, 4, 7]))  # Example 1
    print(square_bounding_box([6, 9, 2, 9]))  # Example 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API This tag is used for small improvements to the readability and usability of the python API. good first issue Good for newcomers Ideas for Machine Learning! These are machine learning ideas and papers that could be useful for DeepForest models. High level.
Projects
None yet
Development

No branches or pull requests

3 participants