7-7 Instance Segmentation

Learning Objectives

Instance segmentation is a more fine-grained task in computer vision than object detection. It not only identifies what an object is and where it is, but also determines the precise shape of each object.

For a single image, the model outputs:

1. Object Category (What)

For example: person, car, dog

2. Instance Mask

Each object has a pixel-level binary mask

→ accurately outlines the shape of the object

We will use Python and a pre-trained model to perform instance segmentation.

Perform instance segmentation on an image and print the results.

 

from ultralytics import YOLO

model = YOLO("yolo11n-seg.pt")

results = model.predict("https://ultralytics.com/images/bus.jpg")
for result in results:
    print(f'mask: {result.masks.data}') # Mask data (Number of objects x Height x Width)

The results are as follows: you will obtain a mask array with dimensions number of objects × height × width, where 0 indicates that the pixel does not belong to the object, and 1 indicates that the pixel belongs to the object.

Perform instance segmentation using a webcam and visualize the results.

import cv2
from ultralytics import YOLO

model = YOLO("yolo11n-seg.pt")

video_path = 0
cap = cv2.VideoCapture(video_path)

while cap.isOpened():
    success, frame = cap.read()
    if success:
        results = model.predict(frame)
        annotated_frame = results[0].plot()
        cv2.imshow("YOLO Inference", annotated_frame)

        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        break

cap.release()
cv2.destroyAllWindows()

The results are as follows.

 

 

Copyright © 2026 YUAN High-Tech Development Co., Ltd.
All rights reserved.