Repository: ultralytics/ultralytics
Issue category: Bug Report

Training labels not applied properly to training data
### Search before asking

- [X] I have searched the Ultralytics YOLO [issues](https://github.com/ultralytics/ultralytics/issues) and found no similar bug report.


### Ultralytics YOLO Component

Train

### Bug

# Bug
Labels are not included in the generated train_batch**X**.jpg images during training of a segmentation model.
Code to reproduce at bottom of section including the example training data.

## Likely cause of bug
I am not familiar with how the training label images are generated, however I highly suspect the issue is that if there are no points that define the polygon (label) in the image. This is caused when Yolo performs augmentation such as crop, resize, stretch, etc as it can morph the label such that all points defining the label are outside the image. This causes the mask to encompress up to the entire image but still not be included
### I do not know if this affects anything other than segmentation!
### This may actually affect the training data itself and not just the generated image examples, but I am not sure!

## Examples
- All white parts of the images are included in the label, thus if they are unlabelled the bug has occured
![train_batch41](https://github.com/user-attachments/assets/ff8243c4-badb-4ea9-a5c0-64b9c28fbef6)
![train_batch42](https://github.com/user-attachments/assets/17895e1b-a967-4c6d-8a18-39b59962893d)

### Code to reproduce, instuctions in other section
[GitIssues.zip](https://github.com/user-attachments/files/17916419/GitIssues.zip)


### Environment

```
Ultralytics 8.3.29 🚀 Python-3.10.12 torch-2.4.1+cu121 CUDA:0 (NVIDIA GeForce RTX 4090, 24564MiB)
Setup complete ✅ (32 CPUs, 15.5 GB RAM, 23.5/251.0 GB disk)

OS                  Linux-5.10.102.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Environment         Linux
Python              3.10.12
Install             pip
RAM                 15.47 GB
Disk                23.5/251.0 GB
CPU                 13th Gen Intel Core(TM) i9-13900
CPU count           32
GPU                 NVIDIA GeForce RTX 4090, 24564MiB
GPU count           1
CUDA                12.1

numpy               ✅ 2.1.2>=1.23.0
matplotlib          ✅ 3.9.2>=3.3.0
opencv-python       ✅ 4.10.0.84>=4.6.0
pillow              ✅ 10.4.0>=7.1.2
pyyaml              ✅ 5.4.1>=5.3.1
requests            ✅ 2.32.3>=2.23.0
scipy               ✅ 1.14.1>=1.4.1
torch               ✅ 2.4.1>=1.8.0
torchvision         ✅ 0.19.1>=0.9.0
tqdm                ✅ 4.66.5>=4.64.0
psutil              ✅ 6.0.0
py-cpuinfo          ✅ 9.0.0
pandas              ✅ 2.2.3>=1.1.4
seaborn             ✅ 0.13.2>=0.11.0
ultralytics-thop    ✅ 2.0.11>=2.0.0
numpy               ✅ 2.1.2<2.0.0; sys_platform == "darwin"
torch               ✅ 2.4.1!=2.4.0,>=1.8.0; sys_platform == "win32"
```

### Minimal Reproducible Example

# How to reproduce
1. Download & Extract provided training images, config (.yaml) and test_yolo.py file 
2. Edit .yaml file such that the folder path is correct
3. Run test_yolo.py 
4. Examine the generated train_batch**X**.jpg images to see if the bug occured (You may need to train more than once)

## What to look for
- Any part that is white is labelled, so if any white pixels are unlabelled this bug has occured

### Examples
![train_batch0](https://github.com/user-attachments/assets/fe7f5b3f-1b00-4004-beb1-a50b5d5413b0)
- In this case the bottom left image is clearly white, but unlabelled

![train_batch2](https://github.com/user-attachments/assets/25cd0a90-8e46-48e8-ba99-0d15cf620719)
- Top right image does has white, but it isn't labelled


### Additional

_No response_

### Are you willing to submit a PR?

- [ ] Yes I'd like to help by submitting a PR!


Functions to edit:
ultralytics/utils/ops.py:segment2box

Patch:
diff --git a/ultralytics/utils/ops.py b/ultralytics/utils/ops.py
index 25e83c61c3a..ac53546ed1b 100644
--- a/ultralytics/utils/ops.py
+++ b/ultralytics/utils/ops.py
@@ -75,9 +75,8 @@ def segment2box(segment, width=640, height=640):
         (np.ndarray): the minimum and maximum x and y values of the segment.
     """
     x, y = segment.T  # segment xy
-    inside = (x >= 0) & (y >= 0) & (x <= width) & (y <= height)
-    x = x[inside]
-    y = y[inside]
+    x = x.clip(0, width)
+    y = y.clip(0, height)
     return (
         np.array([x.min(), y.min(), x.max(), y.max()], dtype=segment.dtype)
         if any(x)