SQL Server – Partitioning by column when the value is not continuous from one record to the other

How can I partition by the PreviousScanIndex column when the value is not continuous from one record to the next (sorted by the RowIndex column), in order to be able to classify each continuous group of records with the same ID?

For example. I want to give the same ID to the group of lines in red, then the group of lines in green to have the next ID, then the blue line group the next ID after that, and so on.

Results set